Class: ChewyQuery::Builder::Criteria

Inherits:
Object
  • Object
show all
Includes:
Compose
Defined in:
lib/chewy_query/builder/criteria.rb

Constant Summary collapse

ARRAY_STORAGES =
[:queries, :filters, :post_filters, :sort, :fields, :types, :scores]
HASH_STORAGES =
[:options, :request_options, :facets, :aggregations, :suggest]
STORAGES =
ARRAY_STORAGES + HASH_STORAGES

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Criteria

Returns a new instance of Criteria.



12
13
14
15
# File 'lib/chewy_query/builder/criteria.rb', line 12

def initialize(options = {})
  @options = { query_mode: :must, filter_mode: :and, post_filter_mode: nil }.merge(options)
  @options[:post_filter_mode] = @options[:filter_mode] unless @options[:post_filter_mode]
end

Instance Method Details

#==(other) ⇒ Object



17
18
19
# File 'lib/chewy_query/builder/criteria.rb', line 17

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

#delete_all_request_bodyObject



117
118
119
120
# File 'lib/chewy_query/builder/criteria.rb', line 117

def delete_all_request_body
  filtered_query = _filtered_query(_request_query, _request_filter, options.slice(:strategy))
  { body: filtered_query.presence || { query: { match_all: {} } } }
end

#merge(other) ⇒ Object



97
98
99
# File 'lib/chewy_query/builder/criteria.rb', line 97

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

#merge!(other) ⇒ Object



90
91
92
93
94
95
# File 'lib/chewy_query/builder/criteria.rb', line 90

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

#none?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/chewy_query/builder/criteria.rb', line 37

def none?
  !!options[:none]
end

#request_bodyObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/chewy_query/builder/criteria.rb', line 101

def request_body
  body = {}

  body.merge!(_filtered_query(_request_query, _request_filter, options.slice(:strategy)))
  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 = _boost_query(body)

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

#update_aggregations(modifer) ⇒ Object



57
58
59
# File 'lib/chewy_query/builder/criteria.rb', line 57

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

#update_facets(modifer) ⇒ Object



49
50
51
# File 'lib/chewy_query/builder/criteria.rb', line 49

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

#update_options(modifer) ⇒ Object



41
42
43
# File 'lib/chewy_query/builder/criteria.rb', line 41

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

#update_request_options(modifer) ⇒ Object



45
46
47
# File 'lib/chewy_query/builder/criteria.rb', line 45

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

#update_scores(modifer) ⇒ Object



53
54
55
# File 'lib/chewy_query/builder/criteria.rb', line 53

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

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



73
74
75
76
77
78
79
# File 'lib/chewy_query/builder/criteria.rb', line 73

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

#update_suggest(modifier) ⇒ Object



61
62
63
# File 'lib/chewy_query/builder/criteria.rb', line 61

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