Class: Chewy::Query::Criteria

Inherits:
Object
  • Object
show all
Includes:
Compose
Defined in:
lib/chewy/query/criteria.rb

Constant Summary collapse

ARRAY_STORAGES =
%i[queries filters post_filters sort fields types scores].freeze
HASH_STORAGES =
%i[options search_options request_options facets aggregations suggest script_fields].freeze
STORAGES =
ARRAY_STORAGES + HASH_STORAGES

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Criteria

Returns a new instance of Criteria.



11
12
13
14
15
16
17
# File 'lib/chewy/query/criteria.rb', line 11

def initialize(options = {})
  @options = options.merge(
    query_mode: Chewy.query_mode,
    filter_mode: Chewy.filter_mode,
    post_filter_mode: Chewy.post_filter_mode || Chewy.filter_mode
  )
end

Instance Method Details

#==(other) ⇒ Object



19
20
21
# File 'lib/chewy/query/criteria.rb', line 19

def ==(other)
  other.is_a?(self.class) && storages == other.storages
end

#merge(other) ⇒ Object



107
108
109
# File 'lib/chewy/query/criteria.rb', line 107

def merge(other)
  clone.merge!(other)
end

#merge!(other) ⇒ Object



100
101
102
103
104
105
# File 'lib/chewy/query/criteria.rb', line 100

def merge!(other)
  STORAGES.each do |storage|
    send("update_#{storage}", other.send(storage))
  end
  self
end

#none?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/chewy/query/criteria.rb', line 39

def none?
  !!options[:none]
end

#request_bodyObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/chewy/query/criteria.rb', line 111

def request_body
  body = _filtered_query(_request_query, _request_filter, options.slice(:strategy))

  if options[:simple]
    {body: body.presence || {query: {match_all: {}}}}
  else
    body[:post_filter] = _request_post_filter if post_filters?
    body[:facets] = facets if facets?
    body[:aggregations] = aggregations if aggregations?
    body[:suggest] = suggest if suggest?
    body[:sort] = sort if sort?
    body[:_source] = fields if fields?
    body[:script_fields] = script_fields if script_fields?

    body = _boost_query(body)

    {body: body.merge!(_request_options)}.merge!(search_options)
  end
end

#update_aggregations(modifier) ⇒ Object



63
64
65
# File 'lib/chewy/query/criteria.rb', line 63

def update_aggregations(modifier)
  aggregations.merge!(modifier)
end

#update_facets(modifier) ⇒ Object



55
56
57
# File 'lib/chewy/query/criteria.rb', line 55

def update_facets(modifier)
  facets.merge!(modifier)
end

#update_options(modifier) ⇒ Object



43
44
45
# File 'lib/chewy/query/criteria.rb', line 43

def update_options(modifier)
  options.merge!(modifier)
end

#update_request_options(modifier) ⇒ Object



47
48
49
# File 'lib/chewy/query/criteria.rb', line 47

def update_request_options(modifier)
  request_options.merge!(modifier)
end

#update_scores(modifier) ⇒ Object



59
60
61
# File 'lib/chewy/query/criteria.rb', line 59

def update_scores(modifier)
  @scores = scores + Array.wrap(modifier).reject(&:blank?)
end

#update_script_fields(modifier) ⇒ Object



71
72
73
# File 'lib/chewy/query/criteria.rb', line 71

def update_script_fields(modifier)
  script_fields.merge!(modifier)
end

#update_search_options(modifier) ⇒ Object



51
52
53
# File 'lib/chewy/query/criteria.rb', line 51

def update_search_options(modifier)
  search_options.merge!(modifier)
end

#update_sort(modifier, options = {}) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/chewy/query/criteria.rb', line 83

def update_sort(modifier, options = {})
  @sort = nil if options[:purge]
  modifier = Array.wrap(modifier).flatten.map do |element|
    element.is_a?(Hash) ? element.map { |k, v| {k => v} } : element
  end.flatten
  @sort = sort + modifier
end

#update_suggest(modifier) ⇒ Object



67
68
69
# File 'lib/chewy/query/criteria.rb', line 67

def update_suggest(modifier)
  suggest.merge!(modifier)
end