@@ -104,134 +104,6 @@ All Elasticsearch DSL features are supported, namely:
104
104
* [ Pagination] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-from-size )
105
105
* [ Options] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html ) (source filtering, highlighting, etc)
106
106
107
- An example of a complex search definition is below.
108
-
109
- ** NOTE:** In order to run the example, you have to allow restoring from the ` data.elasticsearch.org ` repository by adding the following configuration line to your ` elasticsearch.yml ` :
110
-
111
- ``` yaml
112
- repositories.url.allowed_urls : ["https://s3.amazonaws.com/data.elasticsearch.com/*"]
113
- ` ` `
114
-
115
- ` ` ` ruby
116
- require 'awesome_print'
117
-
118
- require 'elasticsearch'
119
- require 'elasticsearch/dsl'
120
-
121
- include Elasticsearch::DSL
122
-
123
- client = Elasticsearch::Client.new transport_options : { request: { timeout: 3600, open_timeout: 3600 } }
124
-
125
- puts "Recovering the 'bicycles.stackexchange.com' index...".yellow
126
-
127
- client.indices.delete index : ' bicycles.stackexchange.com' , ignore: 404
128
-
129
- client.snapshot.create_repository repository : ' data.elasticsearch.com' , body: { type: 'url', settings: { url: 'https://s3.amazonaws.com/data.elasticsearch.com/bicycles.stackexchange.com/' } }
130
- client.snapshot.restore repository : ' data.elasticsearch.com' , snapshot: 'bicycles.stackexchange.com', body: { indices: 'bicycles.stackexchange.com' }
131
- until client.cluster.health(level : ' indices' )['indices']['bicycles.stackexchange.com']['status'] == 'green'
132
- r = client.indices.recovery(index : ' bicycles.stackexchange.com' , human: true)['bicycles.stackexchange.com']['shards'][0] rescue nil
133
- print "\r#{r['index']['size']['recovered'] rescue '0b'} of #{r['index']['size']['total'] rescue 'N/A'}".ljust(52).gray
134
- sleep 1
135
- end; puts
136
-
137
- # The search definition
138
- #
139
- definition = search {
140
- query do
141
-
142
- # Use a `function_score` query to modify the default score
143
- #
144
- function_score do
145
- query do
146
- filtered do
147
-
148
- # Use a `multi_match` query for the fulltext part of the search
149
- #
150
- query do
151
- multi_match do
152
- query 'fixed fixie'
153
- operator 'or'
154
- fields %w[ title^10 body ]
155
- end
156
- end
157
-
158
- # Use a `range` filter on the `creation_date` field
159
- #
160
- filter do
161
- range :creation_date do
162
- gte '2013-01-01'
163
- end
164
- end
165
- end
166
- end
167
-
168
- # Multiply the default `_score` by the document rating
169
- #
170
- functions << { script_score : { script: '_score * doc["rating"].value' } }
171
- end
172
- end
173
-
174
- # Calculate the most frequently used tags
175
- #
176
- aggregation :tags do
177
- terms do
178
- field 'tags'
179
-
180
- # Calculate average view count per tag (inner aggregation)
181
- #
182
- aggregation :avg_view_count do
183
- avg field : ' view_count'
184
- end
185
- end
186
- end
187
-
188
- # Calculate the posting frequency
189
- #
190
- aggregation :frequency do
191
- date_histogram do
192
- field 'creation_date'
193
- interval 'month'
194
- format 'yyyy-MM'
195
-
196
- # Calculate the statistics on comment count per day (inner aggregation)
197
- #
198
- aggregation :comments do
199
- stats field : ' comment_count'
200
- end
201
- end
202
- end
203
-
204
- # Calculate the statistical information about the number of comments
205
- #
206
- aggregation :comment_count_stats do
207
- stats field : ' comment_count'
208
- end
209
-
210
- # Highlight the `title` and `body` fields
211
- #
212
- highlight fields : {
213
- title : { fragment_size: 50 },
214
- body : { fragment_size: 50 }
215
- }
216
-
217
- # Return only a selection of the fields
218
- #
219
- source ['title', 'tags', 'creation_date', 'rating', 'user.location', 'user.display_name']
220
- }
221
-
222
- puts "Search definition #{'-'*63}\n".yellow
223
- ap definition.to_hash
224
-
225
- # Execute the search request
226
- #
227
- response = client.search index : ' bicycles.stackexchange.com' , type: ['question','answer'], body: definition
228
-
229
- puts "\nSearch results #{'-'*66}\n".yellow
230
- ap response
231
- ```
232
-
233
- NOTE: You have to enable dynamic scripting to be able to execute the ` function_score ` query, either
234
- by adding ` script.disable_dynamic: false ` to your elasticsearch.yml or command line parameters.
235
107
236
108
** Please see the extensive RDoc examples in the source code and the integration tests.**
237
109
0 commit comments