class ChewyQuery::Builder::Criteria

Constants

ARRAY_STORAGES
HASH_STORAGES
STORAGES

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

==(other) click to toggle source
# File lib/chewy_query/builder/criteria.rb, line 17
def ==(other)
  other.is_a?(self.class) && storages == other.storages
end
delete_all_request_body() click to toggle source
# 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) click to toggle source
# File lib/chewy_query/builder/criteria.rb, line 97
def merge(other)
  clone.merge!(other)
end
merge!(other) click to toggle source
# 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?() click to toggle source
# File lib/chewy_query/builder/criteria.rb, line 37
def none?
  !!options[:none]
end
request_body() click to toggle source
# 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) click to toggle source
# File lib/chewy_query/builder/criteria.rb, line 57
def update_aggregations(modifer)
  aggregations.merge!(modifer)
end
update_facets(modifer) click to toggle source
# File lib/chewy_query/builder/criteria.rb, line 49
def update_facets(modifer)
  facets.merge!(modifer)
end
update_options(modifer) click to toggle source
# File lib/chewy_query/builder/criteria.rb, line 41
def update_options(modifer)
  options.merge!(modifer)
end
update_request_options(modifer) click to toggle source
# File lib/chewy_query/builder/criteria.rb, line 45
def update_request_options(modifer)
  request_options.merge!(modifer)
end
update_scores(modifer) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# File lib/chewy_query/builder/criteria.rb, line 61
def update_suggest(modifier)
  suggest.merge!(modifier)
end

Protected Instance Methods

_boost_query(body) click to toggle source
# File lib/chewy_query/builder/criteria.rb, line 135
def _boost_query(body)
  scores? or return body
  query = body.delete(:query)
  filter = body.delete(:filter)

  if query && filter
    query = { filtered: { query: query, filter: filter } }
    filter = nil
  end

  score = { }
  score[:functions] = scores
  score[:boost_mode] = options[:boost_mode] if options[:boost_mode]
  score[:score_mode] = options[:score_mode] if options[:score_mode]
  score[:query] = query if query
  score[:filter] = filter if filter
  body.tap{|b| b[:query] = { function_score: score } }
end
_request_filter() click to toggle source
# File lib/chewy_query/builder/criteria.rb, line 162
def _request_filter
  filter_mode = options[:filter_mode]
  request_filter = if filter_mode == :and
    filters
  else
    [_filters_join(filters, filter_mode)]
  end

  _filters_join([_request_types, *request_filter], :and)
end
_request_options() click to toggle source
# File lib/chewy_query/builder/criteria.rb, line 154
def _request_options
  options.slice(:size, :from, :explain, :highlight, :rescore)
end
_request_post_filter() click to toggle source
# File lib/chewy_query/builder/criteria.rb, line 177
def _request_post_filter
  _filters_join(post_filters, options[:post_filter_mode])
end
_request_query() click to toggle source
# File lib/chewy_query/builder/criteria.rb, line 158
def _request_query
  _queries_join(queries, options[:query_mode])
end
_request_types() click to toggle source
# File lib/chewy_query/builder/criteria.rb, line 173
def _request_types
  _filters_join(types.map{|type| { type: { value: type } } }, :or)
end
initialize_clone(other) click to toggle source
# File lib/chewy_query/builder/criteria.rb, line 128
def initialize_clone(other)
  STORAGES.each do |storage|
    value = other.send(storage)
    instance_variable_set("@#{storage}", value.deep_dup)
  end
end
storages() click to toggle source
# File lib/chewy_query/builder/criteria.rb, line 124
def storages
  STORAGES.map{|storage| send(storage) }
end