module ElasticRecord::Relation::SearchMethods

Public Instance Methods

aggregate(aggregation) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 135
def aggregate(aggregation)
  clone.aggregate! aggregation
end
aggregate!(aggregation) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 130
def aggregate!(aggregation)
  self.aggregation_values += [aggregation]
  self
end
as_elastic() click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 177
def as_elastic
  build_search.as_elastic
end
extending(*modules, &block) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 169
def extending(*modules, &block)
  clone.extending!(*modules, &block)
end
extending!(*modules, &block) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 160
def extending!(*modules, &block)
  modules << Module.new(&block) if block_given?

  self.extending_values += modules.flatten
  extend(*extending_values)

  self
end
filter(opts = :chain, *rest) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 81
def filter(opts = :chain, *rest)
  if opts == :chain
    FilterChain.new(clone)
  else
    clone.filter!(opts, *rest)
  end
end
filter!(*args) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 76
def filter!(*args)
  self.filter_values += args
  self
end
limit(value) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 94
def limit(value)
  clone.limit!(value)
end
limit!(value) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 89
def limit!(value)
  self.limit_value = value
  self
end
none() click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 173
def none
  extending(None)
end
offset(value) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 103
def offset(value)
  clone.offset! value
end
offset!(value) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 98
def offset!(value)
  self.offset_value = value
  self
end
order(*args) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 144
def order(*args)
  clone.order! *args
end
query(value) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 72
def query(value)
  clone.query! value
end
query!(value) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 67
def query!(value)
  self.query_value = value
  self
end
reverse_order() click to toggle source

Reverse the existing order clause on the relation.

User.order('name').reverse_order # generated search has 'sort: {'name' => :desc}
# File lib/elastic_record/relation/search_methods.rb, line 151
def reverse_order
  clone.reverse_order!
end
search_options(options) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 113
def search_options(options)
  clone.search_options!(options)
end
search_options!(options) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 107
def search_options!(options)
  self.search_options_value ||= {}
  self.search_options_value.merge! options
  self
end
search_type(type) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 126
def search_type(type)
  clone.search_type! type
end
search_type!(type) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 117
def search_type!(type)
  if type == :count # TODO: Deprecate support
    limit! 0
  else
    self.search_type_value = type
    self
  end
end

Private Instance Methods

build_aggregations(aggregations) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 263
def build_aggregations(aggregations)
  Arelastic::Searches::Aggregations.new(aggregations) unless aggregations.empty?
end
build_filter(filters) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 218
def build_filter(filters)
  nodes = build_filter_nodes(filters)

  if nodes.size == 1
    nodes.first
  elsif nodes.size > 1
    Arelastic::Queries::Bool.new(must: nodes)
  end
end
build_filter_nodes(filters) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 228
def build_filter_nodes(filters)
  filters.each_with_object([]) do |filter, nodes|
    if filter.is_a?(Arelastic::Nodes::Node)
      nodes << filter
    elsif filter.is_a?(ElasticRecord::Relation)
      nodes << Arelastic::Queries::HasChild.new(filter.elastic_index.mapping_type, filter.as_elastic['query'])
    else
      filter.each do |field, terms|
        case terms
        when Array, Range
          nodes << arelastic[field].in(terms)
        when Hash
          nodes << {field => terms}
        when nil
          nodes << arelastic[field].missing
        else
          nodes << arelastic[field].eq(terms)
        end
      end
    end
  end
end
build_limit(limit) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 251
def build_limit(limit)
  if limit
    Arelastic::Searches::Size.new(limit)
  end
end
build_offset(offset) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 257
def build_offset(offset)
  if offset
    Arelastic::Searches::From.new(offset)
  end
end
build_orders(orders) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 267
def build_orders(orders)
  return if orders.empty?

  orders = orders.map do |order|
    if order.is_a?(Arelastic::Sorts::Sort)
      order
    else
      Arelastic::Sorts::Field.new(order)
    end
  end

  orders = reverse_query_order(orders) if reverse_order_value
  Arelastic::Searches::Sort.new(orders)
end
build_query(query) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 210
def build_query(query)
  if query.is_a?(String)
    query = Arelastic::Queries::QueryString.new query
  end

  query
end
build_query_and_filter(query, filters) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 195
def build_query_and_filter(query, filters)
  query = build_query(query)
  filter = build_filter(filters)

  query_and_filter = if filter
    arelastic.queries.bool(filter: filter, must: query)
  elsif query
    query
  else
    arelastic.queries.match_all
  end

  Arelastic::Searches::Query.new query_and_filter
end
reverse_query_order(orders) click to toggle source
# File lib/elastic_record/relation/search_methods.rb, line 282
def reverse_query_order(orders)
  orders.reverse.map do |order|
    order.reverse
  end
end