Class: Elasticsearch::DSL::Search::Search

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

Overview

Wraps the whole search definition (queries, filters, aggregations, sorting, etc)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Search

Returns a new instance of Search.



46
47
48
49
50
# File 'lib/elasticsearch/dsl/search.rb', line 46

def initialize(*args, &block)
  @options = Options.new *args
  @block = block
  @block.arity < 1 ? self.instance_eval(&@block) : @block.call(self) if @block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Delegates to the methods provided by the Options class



250
251
252
253
254
255
256
257
258
259
# File 'lib/elasticsearch/dsl/search.rb', line 250

def method_missing(name, *args, &block)
  if @options.respond_to? name
    @options.__send__ name, *args, &block
    self
  elsif @block
    @block.binding.eval('self').send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#aggregationsObject

Returns the value of attribute aggregations.



44
45
46
# File 'lib/elasticsearch/dsl/search.rb', line 44

def aggregations
  @aggregations
end

Instance Method Details

#aggregation(*args, &block) ⇒ self

DSL method for building the ‘aggregations` part of a search definition

Returns:

  • (self)


125
126
127
128
129
130
131
132
133
134
135
# File 'lib/elasticsearch/dsl/search.rb', line 125

def aggregation(*args, &block)
  @aggregations ||= AggregationsCollection.new

  if block
    @aggregations.update args.first => Aggregation.new(*args, &block)
  else
    name = args.shift
    @aggregations.update name => args.shift
  end
  self
end

#collapse(*args, &block) ⇒ self, Collapse

DSL method for building the ‘collapse` part of a search definition

Returns:



160
161
162
163
164
165
166
167
# File 'lib/elasticsearch/dsl/search.rb', line 160

def collapse(*args, &block)
  if !args.empty? || block
    @collapse = Collapse.new(*args, &block)
    self
  else
    @collapse
  end
end

#filter(*args, &block) ⇒ self

DSL method for building the ‘filter` part of a search definition

Returns:

  • (self)


79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/elasticsearch/dsl/search.rb', line 79

def filter(*args, &block)
  case
    when block
      @filter = Filter.new(*args, &block)
      self
    when !args.empty?
      @filter = args.first
      self
    else
      @filter
  end
end

#filter=(value) ⇒ Object

Set the filter part of a search definition



94
95
96
# File 'lib/elasticsearch/dsl/search.rb', line 94

def filter=(value)
  filter value
end

#from(value = nil) ⇒ self Also known as: from=

DSL method for building the ‘from` part of a search definition

Returns:

  • (self)


218
219
220
221
222
223
224
225
# File 'lib/elasticsearch/dsl/search.rb', line 218

def from(value=nil)
  if value
    @from = value
    self
  else
    @from
  end
end

#highlight(*args, &block) ⇒ self

DSL method for building the ‘highlight` part of a search definition

Returns:

  • (self)


147
148
149
150
151
152
153
154
# File 'lib/elasticsearch/dsl/search.rb', line 147

def highlight(*args, &block)
  if !args.empty? || block
    @highlight = Highlight.new(*args, &block)
    self
  else
    @highlight
  end
end

#post_filter(*args, &block) ⇒ self

DSL method for building the ‘post_filter` part of a search definition

Returns:

  • (self)


102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/elasticsearch/dsl/search.rb', line 102

def post_filter(*args, &block)
  case
    when block
      @post_filter = Filter.new(*args, &block)
      self
    when !args.empty?
      @post_filter = args.first
      self
    else
      @post_filter
  end
end

#post_filter=(value) ⇒ Object

Set the post_filter part of a search definition



117
118
119
# File 'lib/elasticsearch/dsl/search.rb', line 117

def post_filter=(value)
  post_filter value
end

#query(*args, &block) ⇒ self, {Query}

DSL method for building or accessing the ‘query` part of a search definition

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/elasticsearch/dsl/search.rb', line 56

def query(*args, &block)
  case
    when block
      @query = Query.new(*args, &block)
      self
    when !args.empty?
      @query = args.first
      self
    else
      @query
  end
end

#query=(value) ⇒ Object

Set the query part of a search definition



71
72
73
# File 'lib/elasticsearch/dsl/search.rb', line 71

def query=(value)
  query value
end

#size(value = nil) ⇒ self Also known as: size=

DSL method for building the ‘size` part of a search definition

Returns:

  • (self)


205
206
207
208
209
210
211
212
# File 'lib/elasticsearch/dsl/search.rb', line 205

def size(value=nil)
  if value
    @size = value
    self
  else
    @size
  end
end

#sort(*args, &block) ⇒ self

DSL method for building the ‘sort` part of a search definition

Returns:

  • (self)


173
174
175
176
177
178
179
180
# File 'lib/elasticsearch/dsl/search.rb', line 173

def sort(*args, &block)
  if !args.empty? || block
    @sort = Sort.new(*args, &block)
    self
  else
    @sort
  end
end

#sort=(value) ⇒ Object

Set the sort part of a search definition



184
185
186
# File 'lib/elasticsearch/dsl/search.rb', line 184

def sort=(value)
  @sort = value
end

#stored_fields(value = nil) ⇒ self Also known as: stored_fields=

DSL method for building the ‘stored_fields` part of a search definition

Returns:

  • (self)


192
193
194
195
196
197
198
199
# File 'lib/elasticsearch/dsl/search.rb', line 192

def stored_fields(value=nil)
  if value
    @stored_fields = value
    self
  else
    @stored_fields
  end
end

#suggest(*args, &block) ⇒ self

DSL method for building the ‘suggest` part of a search definition

Returns:

  • (self)


231
232
233
234
235
236
237
238
239
240
# File 'lib/elasticsearch/dsl/search.rb', line 231

def suggest(*args, &block)
  if !args.empty? || block
    @suggest ||= {}
    key, options = args
    @suggest.update key => Suggest.new(key, options, &block)
    self
  else
    @suggest
  end
end

#suggest=(value) ⇒ Object

Set the suggest part of a search definition



244
245
246
# File 'lib/elasticsearch/dsl/search.rb', line 244

def suggest=(value)
  @suggest = value
end

#to_hashHash

Converts the search definition to a Hash

Returns:

  • (Hash)


265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/elasticsearch/dsl/search.rb', line 265

def to_hash
  hash = {}
  hash.update(query: @query.to_hash)   if @query
  hash.update(filter: @filter.to_hash) if @filter
  hash.update(post_filter: @post_filter.to_hash) if @post_filter
  hash.update(aggregations: @aggregations.reduce({}) { |sum,item| sum.update item.first => item.last.to_hash }) if @aggregations
  hash.update(sort: @sort.to_hash) if @sort
  hash.update(size: @size) if @size
  hash.update(stored_fields: @stored_fields) if @stored_fields
  hash.update(from: @from) if @from
  hash.update(suggest: @suggest.reduce({}) { |sum,item| sum.update item.last.to_hash }) if @suggest
  hash.update(highlight: @highlight.to_hash) if @highlight
  hash.update(collapse: @collapse.to_hash) if @collapse
  hash.update(@options) unless @options.empty?
  hash
end