Class: Elasticsearch::DSL::Search::Search
- Inherits:
-
Object
- Object
- Elasticsearch::DSL::Search::Search
- Defined in:
- lib/elasticsearch/dsl/search.rb
Overview
Wraps the whole search definition (queries, filters, aggregations, sorting, etc)
Instance Attribute Summary collapse
-
#aggregations ⇒ Object
Returns the value of attribute aggregations.
Instance Method Summary collapse
-
#aggregation(*args, &block) ⇒ self
DSL method for building the ‘aggregations` part of a search definition.
-
#filter(*args, &block) ⇒ self
DSL method for building the ‘filter` part of a search definition.
-
#filter=(value) ⇒ Object
Set the filter part of a search definition.
-
#from(value = nil) ⇒ self
(also: #from=)
DSL method for building the ‘from` part of a search definition.
-
#highlight(*args, &block) ⇒ self
DSL method for building the ‘highlight` part of a search definition.
-
#initialize(*args, &block) ⇒ Search
constructor
A new instance of Search.
-
#method_missing(name, *args, &block) ⇒ Object
Delegates to the methods provided by the Options class.
-
#post_filter(*args, &block) ⇒ self
DSL method for building the ‘post_filter` part of a search definition.
-
#post_filter=(value) ⇒ Object
Set the post_filter part of a search definition.
-
#query(*args, &block) ⇒ self, {Query}
DSL method for building or accessing the ‘query` part of a search definition.
-
#query=(value) ⇒ Object
Set the query part of a search definition.
-
#size(value = nil) ⇒ self
(also: #size=)
DSL method for building the ‘size` part of a search definition.
-
#sort(*args, &block) ⇒ self
DSL method for building the ‘sort` part of a search definition.
-
#suggest(*args, &block) ⇒ self
DSL method for building the ‘suggest` part of a search definition.
-
#suggest=(value) ⇒ Object
Set the suggest part of a search definition.
-
#to_hash ⇒ Hash
Converts the search definition to a Hash.
Constructor Details
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
211 212 213 214 215 216 217 218 |
# File 'lib/elasticsearch/dsl/search.rb', line 211 def method_missing(name, *args, &block) if @options.respond_to? name @options.__send__ name, *args, &block self else super end end |
Instance Attribute Details
#aggregations ⇒ Object
Returns the value of attribute aggregations.
38 39 40 |
# File 'lib/elasticsearch/dsl/search.rb', line 38 def aggregations @aggregations end |
Instance Method Details
#aggregation(*args, &block) ⇒ self
DSL method for building the ‘aggregations` part of a search definition
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/elasticsearch/dsl/search.rb', line 118 def aggregation(*args, &block) @aggregations ||= {} if block @aggregations.update args.first => Aggregation.new(*args, &block) else name = args.shift @aggregations.update name => args.shift end self end |
#filter(*args, &block) ⇒ self
DSL method for building the ‘filter` part of a search definition
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/elasticsearch/dsl/search.rb', line 72 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
87 88 89 |
# File 'lib/elasticsearch/dsl/search.rb', line 87 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
179 180 181 182 183 184 185 186 |
# File 'lib/elasticsearch/dsl/search.rb', line 179 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
140 141 142 143 144 145 146 147 |
# File 'lib/elasticsearch/dsl/search.rb', line 140 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
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/elasticsearch/dsl/search.rb', line 95 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
110 111 112 |
# File 'lib/elasticsearch/dsl/search.rb', line 110 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
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/elasticsearch/dsl/search.rb', line 49 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
64 65 66 |
# File 'lib/elasticsearch/dsl/search.rb', line 64 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
166 167 168 169 170 171 172 173 |
# File 'lib/elasticsearch/dsl/search.rb', line 166 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
153 154 155 156 157 158 159 160 |
# File 'lib/elasticsearch/dsl/search.rb', line 153 def sort(*args, &block) if !args.empty? || block @sort = Sort.new(*args, &block) self else @sort end end |
#suggest(*args, &block) ⇒ self
DSL method for building the ‘suggest` part of a search definition
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/elasticsearch/dsl/search.rb', line 192 def suggest(*args, &block) if !args.empty? || block @suggest ||= {} key, = args @suggest.update key => Suggest.new(key, , &block) self else @suggest end end |
#suggest=(value) ⇒ Object
Set the suggest part of a search definition
205 206 207 |
# File 'lib/elasticsearch/dsl/search.rb', line 205 def suggest=(value) @suggest = value end |
#to_hash ⇒ Hash
Converts the search definition to a Hash
224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/elasticsearch/dsl/search.rb', line 224 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(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(@options) unless @options.empty? hash end |