Module: Caoutsearch::Search::QueryMethods

Included in:
Base
Defined in:
lib/caoutsearch/search/query_methods.rb

Instance Method Summary collapse

Instance Method Details

#apply_dsl_filter(item, value) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/caoutsearch/search/query_methods.rb', line 54

def apply_dsl_filter(item, value)
  return instance_exec(value, &item.block) if item.has_block?

  terms = []
  indexes = item.indexes
  options = item.options.dup
  query = elasticsearch_query

  if options[:nested_query]
    query = nested_query(options[:nested_query])
    options[:nested] = nil
  end

  indexes.each do |index|
    options_index = options.dup
    options_index[:type] = mappings.find_type(index) unless options_index.key?(:type)
    options_index[:nested] = mappings.nested_path(index) unless options_index.key?(:nested)
    options_index[:include_in_parent] = mappings.include_in_parent?(index) unless options_index.key?(:include_in_parent) || !options_index[:nested]

    terms += query.build_terms(index, value, **options_index)
  end

  query.should_filter_on(terms)
end

#apply_dsl_filters(items, value) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/caoutsearch/search/query_methods.rb', line 79

def apply_dsl_filters(items, value)
  terms = []

  items.each do |item|
    terms += isolate_dsl_filter(item, value)
  end

  should_filter_on(terms)
end

#apply_dsl_match_all(value) ⇒ Object

Applying DSL filters items




48
49
50
51
52
# File 'lib/caoutsearch/search/query_methods.rb', line 48

def apply_dsl_match_all(value)
  return unless (block = config[:match_all])

  instance_exec(value, &block)
end

#apply_dsl_sort(item, direction) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/caoutsearch/search/query_methods.rb', line 114

def apply_dsl_sort(item, direction)
  return instance_exec(direction, &item.block) if item.has_block?

  indexes = item.indexes
  indexes.each do |index|
    case index
    when :default, "default"
      sort_by(:default, direction)

    when Hash
      index.map do |key, value|
        key_direction = (value.to_s == direction.to_s) ? :asc : :desc
        add_sort(key, key_direction)
      end

    else
      add_sort(index, direction)
    end
  end
end

#apply_dsl_suggest(item, query, **options) ⇒ Object



150
151
152
153
154
# File 'lib/caoutsearch/search/query_methods.rb', line 150

def apply_dsl_suggest(item, query, **options)
  return instance_exec(query, **options, &item.block) if item.has_block?

  add_suggestion(item.name, query, **options)
end

#elasticsearch_queryObject Also known as: query

Accessors




14
15
16
# File 'lib/caoutsearch/search/query_methods.rb', line 14

def elasticsearch_query
  @elasticsearch_query ||= Caoutsearch::Search::Query::Base.new
end

#filter_by(key, value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/caoutsearch/search/query_methods.rb', line 29

def filter_by(key, value)
  case key
  when Array
    items = key.filter_map { |k| config[:filters][k] }
    apply_dsl_filters(items, value)
  when Caoutsearch::Search::DSL::Item
    apply_dsl_filter(key, value)
  else
    if config[:filters].key?(key)
      apply_dsl_filter(config[:filters][key], value)
    elsif config[:filters].key?(:__undef__)
      block = config[:filters][:__undef__]
      instance_exec(key, value, &block)
    end
  end
end

#isolate_dsl_filter(item, value) ⇒ Object



89
90
91
92
93
# File 'lib/caoutsearch/search/query_methods.rb', line 89

def isolate_dsl_filter(item, value)
  isolated_instance = clone
  isolated_instance.apply_dsl_filter(item, value)
  isolated_instance.elasticsearch_query.dig(:query, :bool, :filter) || []
end

#order_by(keys) ⇒ Object

Applying orders




97
98
99
100
101
102
103
# File 'lib/caoutsearch/search/query_methods.rb', line 97

def order_by(keys)
  case keys
  when Array then keys.each { |key| order_by(key) }
  when Hash then keys.each { |key, direction| sort_by(key, direction) }
  when String, Symbol then sort_by(keys)
  end
end

#search_by(criteria) ⇒ Object

Applying criteria




21
22
23
24
25
26
27
# File 'lib/caoutsearch/search/query_methods.rb', line 21

def search_by(criteria)
  case criteria
  when Array then criteria.each { |criterion| search_by(criterion) }
  when Hash then criteria.each { |key, value| filter_by(key, value) }
  when String then apply_dsl_match_all(criteria)
  end
end

#sort_by(key, direction = nil) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/caoutsearch/search/query_methods.rb', line 105

def sort_by(key, direction = nil)
  if config[:sorts].key?(key)
    apply_dsl_sort(config[:sorts][key], direction)
  elsif config[:sorts].key?(:__undef__)
    block = config[:sorts][:__undef__]
    instance_exec(key, direction, &block)
  end
end

#suggest_with(*args) ⇒ Object

Applying Suggests




137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/caoutsearch/search/query_methods.rb', line 137

def suggest_with(*args)
  args.each do |(hash, options)|
    raise ArgumentError unless hash.is_a?(Hash)

    hash.each do |key, value|
      next unless (item = config[:suggestions][key.to_s])

      options ||= {}
      apply_dsl_suggest(item, value, **options)
    end
  end
end