Class: Chewy::Query::Criteria

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

Constant Summary collapse

ARRAY_STORAGES =
[:queries, :filters, :post_filters, :sort, :fields, :types, :scores]
HASH_STORAGES =
[:options, :request_options, :facets, :aggregations, :suggest, :script_fields]
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



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

def merge other
  clone.merge!(other)
end

#merge!(other) ⇒ Object



96
97
98
99
100
101
# File 'lib/chewy/query/criteria.rb', line 96

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/chewy/query/criteria.rb', line 107

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

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

    body = _boost_query(body)

    { body: body.merge!(request_options) }
  end
end

#update_aggregations(modifier) ⇒ Object



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

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

#update_facets(modifier) ⇒ Object



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

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



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

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

#update_script_fields(modifier) ⇒ Object



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

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

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



79
80
81
82
83
84
85
# File 'lib/chewy/query/criteria.rb', line 79

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



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

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