Class: Elastictastic::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/elastictastic/search.rb

Constant Summary collapse

KEYS =
%w(query filter from size sort highlight fields script_fields
preference facets)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Search

Returns a new instance of Search.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Attribute Details

#facetsObject (readonly)

Returns the value of attribute facets.



6
7
8
# File 'lib/elastictastic/search.rb', line 6

def facets
  @facets
end

#fieldsObject (readonly)

Returns the value of attribute fields.



6
7
8
# File 'lib/elastictastic/search.rb', line 6

def fields
  @fields
end

#fromObject (readonly)

Returns the value of attribute from.



6
7
8
# File 'lib/elastictastic/search.rb', line 6

def from
  @from
end

#preferenceObject (readonly)

Returns the value of attribute preference.



6
7
8
# File 'lib/elastictastic/search.rb', line 6

def preference
  @preference
end

#script_fieldsObject (readonly)

Returns the value of attribute script_fields.



6
7
8
# File 'lib/elastictastic/search.rb', line 6

def script_fields
  @script_fields
end

#sizeObject (readonly)

Returns the value of attribute size.



6
7
8
# File 'lib/elastictastic/search.rb', line 6

def size
  @size
end

#sortObject (readonly)

Returns the value of attribute sort.



6
7
8
# File 'lib/elastictastic/search.rb', line 6

def sort
  @sort
end

Instance Method Details

#filterObject



72
73
74
75
76
# File 'lib/elastictastic/search.rb', line 72

def filter
  maybe_array(filters) do
    { 'and' => filters }
  end
end

#highlightObject



78
79
80
81
82
# File 'lib/elastictastic/search.rb', line 78

def highlight
  if @highlight_fields
    @highlight_settings.merge('fields' => @highlight_fields)
  end
end

#initialize_copy(other) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



84
85
86
# File 'lib/elastictastic/search.rb', line 84

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

#merge!(other) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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

#paramsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

#queryObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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