class Elastictastic::Search

Constants

KEYS

Attributes

facets[R]
fields[R]
filters[R]
from[R]
highlight_fields[R]
highlight_settings[R]
preference[R]
queries[R]
query_filters[R]
script_fields[R]
size[R]
sort[R]

Public Class Methods

new(params = {}) click to toggle source
# File lib/elastictastic/search.rb, line 9
def initialize(params = {})
  params = Util.deep_stringify(params) # this creates a copy
  @queries, @query_filters = extract_queries_and_query_filters(params['query'])
  @filters = extract_filters(params['filter'])
  @from = params.delete('from')
  @size = params.delete('size')
  @sort = Util.ensure_array(params.delete('sort'))
  highlight = params.delete('highlight')
  if highlight
    @highlight_fields = highlight.delete('fields')
    @highlight_settings = highlight
  end
  @fields = Util.ensure_array(params.delete('fields'))
  @script_fields = params.delete('script_fields')
  @preference = params.delete('preference')
  @facets = params.delete('facets')
end

Public Instance Methods

filter() click to toggle source
# File lib/elastictastic/search.rb, line 72
def filter
  maybe_array(filters) do
    { 'and' => filters }
  end
end
highlight() click to toggle source
# File lib/elastictastic/search.rb, line 78
def highlight
  if @highlight_fields
    @highlight_settings.merge('fields' => @highlight_fields)
  end
end
initialize_copy(other) click to toggle source
# File lib/elastictastic/search.rb, line 27
def initialize_copy(other)
  @queries = deep_copy(other.queries)
  @query_filters = deep_copy(other.query_filters)
  @filters = deep_copy(other.filters)
  @sort = deep_copy(other.sort)
  @highlight = deep_copy(other.highlight)
  @fields = other.fields.dup if other.fields
  @script_fields = deep_copy(other.script_fields)
  @facets = deep_copy(other.facets)
end
merge(other) click to toggle source
# File lib/elastictastic/search.rb, line 84
def merge(other)
  dup.merge!(other)
end
merge!(other) click to toggle source
# File lib/elastictastic/search.rb, line 88
def merge!(other)
  @queries = combine(@queries, other.queries)
  @query_filters = combine(@query_filters, other.query_filters)
  @filters = combine(@filters, other.filters)
  @from = other.from  || @from
  @size = other.size || @size
  @sort = combine(@sort, other.sort)
  if @highlight_fields && other.highlight_fields
    @highlight_fields = combine(highlight_fields_with_settings, other.highlight_fields_with_settings)
    @highlight_settings = {}
  else
    @highlight_settings = combine(@highlight_settings, other.highlight_settings)
    @highlight_fields = combine(@highlight_fields, other.highlight_fields)
  end
  @fields = combine(@fields, other.fields)
  @script_fields = combine(@script_fields, other.script_fields)
  @preference = other.preference || @preference
  @facets = combine(@facets, other.facets)
  self
end
params() click to toggle source
# File lib/elastictastic/search.rb, line 38
def params
  {}.tap do |params|
    params['query'] = query
    params['filter'] = filter
    params['from'] = from
    params['size'] = size
    params['sort'] = maybe_array(sort)
    params['highlight'] = highlight
    params['fields'] = maybe_array(fields)
    params['script_fields'] = script_fields
    params['preference'] = preference
    params['facets'] = facets
    params.reject! { |k, v| v.blank? }
  end
end
query() click to toggle source
# File lib/elastictastic/search.rb, line 54
def query
  query_query = maybe_array(queries) do
    { 'bool' => { 'must' => queries }}
  end
  query_filter = maybe_array(query_filters) do
    { 'and' => query_filters }
  end
  if query_query
    if query_filter
      { 'filtered' => { 'query' => query_query, 'filter' => query_filter }}
    else
      query_query
    end
  elsif query_filter
    { 'constant_score' => { 'filter' => query_filter }}
  end
end

Protected Instance Methods

highlight_fields_with_settings() click to toggle source
# File lib/elastictastic/search.rb, line 113
def highlight_fields_with_settings
  if @highlight_fields
    {}.tap do |fields_with_settings|
      @highlight_fields.each_pair do |field, settings|
        fields_with_settings[field] = @highlight_settings.merge(settings)
      end
    end
  end
end

Private Instance Methods

combine(object1, object2) click to toggle source
# File lib/elastictastic/search.rb, line 136
def combine(object1, object2)
  if object1.nil? then object2
  elsif object2.nil? then object1
  else 
    case object1
    when Array then object1 + object2
    when Hash then object1.merge(object2)
    else raise ArgumentError, "Don't know how to combine #{object1.inspect} with #{object2.inspect}"
    end
  end
end
deep_copy(object) click to toggle source
# File lib/elastictastic/search.rb, line 176
def deep_copy(object)
  Marshal.load(Marshal.dump(object)) unless object.nil?
end
extract_filters(params) click to toggle source
# File lib/elastictastic/search.rb, line 167
def extract_filters(params)
  if params.nil? then []
  elsif params.keys == %w(and)
    params['and']
  else
    [params]
  end
end
extract_queries(params) click to toggle source
# File lib/elastictastic/search.rb, line 159
def extract_queries(params)
  if params.nil? then []
  elsif params.keys == %w(bool) && params['bool'].keys == %w(must)
    params['bool']['must']
  else [params]
  end
end
extract_queries_and_query_filters(params) click to toggle source
# File lib/elastictastic/search.rb, line 148
def extract_queries_and_query_filters(params)
  if params.nil? then [[], []]
  elsif params.keys == %w(filtered)
    [extract_queries(params['filtered']['query']), extract_filters(params['filtered']['filter'])]
  elsif params.keys == %w(constant_score) && params['constant_score'].keys == %w(filter)
    [[], extract_filters(params['constant_score']['filter'])]
  else
    [extract_queries(params), []]
  end
end
maybe_array(array) { || ... } click to toggle source
# File lib/elastictastic/search.rb, line 125
def maybe_array(array)
  case array.length
  when 0 then nil
  when 1 then array.first
  else
    if block_given? then yield
    else array
    end
  end
end