elasticsearch-dsl 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 055ca5865485c8f28e21edaab8e8cdf51d3f066b
4
- data.tar.gz: 12b1be97400b1f7b4f0bca1ddb8b77731a7c6ef4
3
+ metadata.gz: c2037d3abda2b5b40dab3cd2618751d20c0fcc06
4
+ data.tar.gz: b31c3b77ac4628ea87e96f85b9e06b2f8a592b91
5
5
  SHA512:
6
- metadata.gz: 091cda1600fb6848d0f1ebfc7be9fde2220f2600d073b896b37a091887527212f2506860f7dc9b1a6bf02e0271479dc46ab4cb8df9e8e7db9e806aa006cc2c36
7
- data.tar.gz: f6afafb234bbad0efbd2f82a7da7d02766206d8b757e824f8dd17de2c1438db2e8d146c15ea424dc73fdf4609a59bab927720b6d6a3619baf4536f9cad1f1e04
6
+ metadata.gz: 8c7b217d8851ccb1f90263a48f05dff3517dca17d57296acbb750d2fd5232f35c7ed8f6cd5e060e6a30eb93937975c03c04dacd85006ca9fbe9827478471fab8
7
+ data.tar.gz: 1c8efb817f44b4ce94d08c56aaa43b4eb74b6a348e2b55582d70a3325f437271a1bb602c7fec4a5d0e4124ab3d2987fb2176b9aa34ad95c4ed1f8b9f37f76c28
data/README.md CHANGED
@@ -108,7 +108,11 @@ All Elasticsearch DSL features are supported, namely:
108
108
  * [Pagination](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-from-size.html)
109
109
  * [Options](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-body.html) (source filtering, highlighting, etc)
110
110
 
111
- An example of a complex search definition would look like this:
111
+ An example of a complex search definition is below.
112
+
113
+ **NOTE:** In order to run the example, you have to allow restoring from the `data.elasticsearch.org`
114
+ repository by adding this configuration to your `elasticsearch.yml`:
115
+ `repositories.url.allowed_urls: ["https://s3.amazonaws.com/data.elasticsearch.org/*"]`
112
116
 
113
117
  ```ruby
114
118
  require 'awesome_print'
@@ -120,18 +124,17 @@ include Elasticsearch::DSL
120
124
 
121
125
  client = Elasticsearch::Client.new transport_options: { request: { timeout: 3600, open_timeout: 3600 } }
122
126
 
123
- # Restore an index from a snapshot
124
- #
127
+ puts "Recovering the 'bicycles.stackexchange.com' index...".yellow
128
+
125
129
  client.indices.delete index: 'bicycles.stackexchange.com', ignore: 404
126
130
 
127
- puts "Recovering the 'bicycles.stackexchange.com' index...".gray
128
131
  client.snapshot.create_repository repository: 'data.elasticsearch.org', body: { type: 'url', settings: { url: 'https://s3.amazonaws.com/data.elasticsearch.org/bicycles.stackexchange.com/' } }
129
132
  client.snapshot.restore repository: 'data.elasticsearch.org', snapshot: 'bicycles.stackexchange.com', body: { indices: 'bicycles.stackexchange.com' }
130
133
  until client.cluster.health(level: 'indices')['indices']['bicycles.stackexchange.com']['status'] == 'green'
131
- r = client.indices.status(index: 'bicycles.stackexchange.com', human: true, recovery: true)['indices']['bicycles.stackexchange.com']['shards']['0'][0]
132
- print "\r#{r['index']['size']} of #{r['gateway_recovery']['index']['expected_recovered_size']}".ljust(52).gray
134
+ r = client.indices.recovery(index: 'bicycles.stackexchange.com', human: true)['bicycles.stackexchange.com']['shards'][0] rescue nil
135
+ print "\r#{r['index']['size']['recovered'] rescue '0b'} of #{r['index']['size']['total'] rescue 'N/A'}".ljust(52).gray
133
136
  sleep 1
134
- end
137
+ end; puts
135
138
 
136
139
  # The search definition
137
140
  #
@@ -164,9 +167,9 @@ definition = search {
164
167
  end
165
168
  end
166
169
 
167
- # Multiply the default `_score` by a (slightly normalized) document rating
170
+ # Multiply the default `_score` by the document rating
168
171
  #
169
- functions << { script_score: { script: '_score * log10( doc["rating"].value )' } }
172
+ functions << { script_score: { script: '_score * doc["rating"].value' } }
170
173
  end
171
174
  end
172
175
 
@@ -175,6 +178,12 @@ definition = search {
175
178
  aggregation :tags do
176
179
  terms do
177
180
  field 'tags'
181
+
182
+ # Calculate average view count per tag (inner aggregation)
183
+ #
184
+ aggregation :avg_view_count do
185
+ avg field: 'view_count'
186
+ end
178
187
  end
179
188
  end
180
189
 
@@ -185,6 +194,12 @@ definition = search {
185
194
  field 'creation_date'
186
195
  interval 'month'
187
196
  format 'yyyy-MM'
197
+
198
+ # Calculate the statistics on comment count per day (inner aggregation)
199
+ #
200
+ aggregation :comments do
201
+ stats field: 'comment_count'
202
+ end
188
203
  end
189
204
  end
190
205
 
@@ -206,14 +221,14 @@ definition = search {
206
221
  source ['title', 'tags', 'creation_date', 'rating', 'user.location', 'user.display_name']
207
222
  }
208
223
 
209
- puts "Search definition #{'-'*63}\n".gray
224
+ puts "Search definition #{'-'*63}\n".yellow
210
225
  ap definition.to_hash
211
226
 
212
227
  # Execute the search request
213
228
  #
214
229
  response = client.search index: 'bicycles.stackexchange.com', type: ['question','answer'], body: definition
215
230
 
216
- puts "\nSearch results #{'-'*66}\n".gray
231
+ puts "\nSearch results #{'-'*66}\n".yellow
217
232
  ap response
218
233
  ```
219
234
 
@@ -28,7 +28,7 @@ module Elasticsearch
28
28
  end
29
29
  end
30
30
 
31
- # Defines a nested aggregation
31
+ # Defines an aggregation nested in another one
32
32
  #
33
33
  def aggregation(*args, &block)
34
34
  call
@@ -9,14 +9,18 @@ module Elasticsearch
9
9
  #
10
10
  # search do
11
11
  # aggregation :all_documents do
12
- # global
12
+ # global do
13
+ # aggregation :avg_clicks do
14
+ # avg field: 'clicks'
15
+ # end
16
+ # end
13
17
  # end
14
18
  # end
15
19
  #
16
- # @see http://elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-global-aggregation.html
20
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-global-aggregation.html
17
21
  #
18
22
  class Global
19
- include BaseComponent
23
+ include BaseAggregationComponent
20
24
  end
21
25
 
22
26
  end
@@ -5,7 +5,7 @@ module Elasticsearch
5
5
 
6
6
  # A multi-value metrics aggregation which returns statistical information on numeric values
7
7
  #
8
- # @example
8
+ # @example Passing the options as a Hash
9
9
  #
10
10
  # search do
11
11
  # aggregation :clicks_stats do
@@ -13,10 +13,23 @@ module Elasticsearch
13
13
  # end
14
14
  # end
15
15
  #
16
- # @see http://elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-stats-aggregation.html
16
+ # @example Passing the options as a block
17
+ #
18
+ # search do
19
+ # aggregation :clicks_stats do
20
+ # stats do
21
+ # field 'clicks'
22
+ # end
23
+ # end
24
+ # end
25
+ #
26
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-stats-aggregation.html
17
27
  #
18
28
  class Stats
19
29
  include BaseComponent
30
+
31
+ option_method :field
32
+ option_method :script
20
33
  end
21
34
 
22
35
  end
@@ -5,13 +5,23 @@ module Elasticsearch
5
5
 
6
6
  # A multi-bucket aggregation which returns the collection of terms and their document counts
7
7
  #
8
- # @example
8
+ # @example Passing the options as a Hash
9
9
  #
10
10
  # aggregation :tags do
11
11
  # terms field: 'tags'
12
12
  # end
13
13
  #
14
- # @see http://elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html
14
+ # @example Passing the options as a block
15
+ #
16
+ # search do
17
+ # aggregation :tags do
18
+ # terms do
19
+ # field 'tags'
20
+ # end
21
+ # end
22
+ # end
23
+ #
24
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html
15
25
  #
16
26
  class Terms
17
27
  include BaseAggregationComponent
@@ -1,5 +1,5 @@
1
1
  module Elasticsearch
2
2
  module DSL
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
@@ -15,7 +15,7 @@ module Elasticsearch
15
15
  mappings: {
16
16
  venue: {
17
17
  properties: {
18
- location: { type: 'geo_point', normalize: false, validate: true }
18
+ location: { type: 'geo_point' }
19
19
  }
20
20
  }
21
21
  }
@@ -36,7 +36,7 @@ module Elasticsearch
36
36
  assert_equal 3, response['aggregations']['tags']['value']
37
37
  end
38
38
 
39
- should "return tag counts per clicks range" do
39
+ should "return tag counts per clicks range as an inner (nested) aggregation" do
40
40
  response = @client.search index: 'test', body: search {
41
41
  aggregation :clicks do
42
42
  range field: 'clicks' do
@@ -95,6 +95,29 @@ module Elasticsearch
95
95
  assert_equal 13, response['aggregations']['avg_clicks']['value'].to_i
96
96
  end
97
97
 
98
+ should "define a global aggregation" do
99
+ response = @client.search index: 'test', body: search {
100
+ query do
101
+ filtered filter: { terms: { tags: ['two'] } }
102
+ end
103
+
104
+ aggregation :avg_clicks do
105
+ avg field: 'clicks'
106
+ end
107
+
108
+ aggregation :all_documents do
109
+ global do
110
+ aggregation :avg_clicks do
111
+ avg field: 'clicks'
112
+ end
113
+ end
114
+ end
115
+ }.to_hash
116
+
117
+ assert_equal 15, response['aggregations']['avg_clicks']['value'].to_i
118
+ assert_equal 13, response['aggregations']['all_documents']['avg_clicks']['value'].to_i
119
+ end
120
+
98
121
  should "return statistics on clicks" do
99
122
  response = @client.search index: 'test', body: search {
100
123
  aggregation :stats_clicks do
@@ -12,7 +12,25 @@ module Elasticsearch
12
12
  should "be converted to a Hash" do
13
13
  assert_equal({ global: {} }, subject.to_hash)
14
14
  end
15
-
15
+
16
+ should "take a block" do
17
+ subject = Global.new do
18
+ end
19
+ assert_equal({global: {} }, subject.to_hash)
20
+ end
21
+
22
+ should "define aggregations" do
23
+ subject = Global.new do
24
+ aggregation :foo do
25
+ terms field: "bar"
26
+ end
27
+ end
28
+ expected = {
29
+ aggregations: { foo: { terms: { field: "bar" } } },
30
+ global: {}
31
+ }
32
+ assert_equal(expected, subject.to_hash)
33
+ end
16
34
  end
17
35
  end
18
36
  end
@@ -17,6 +17,14 @@ module Elasticsearch
17
17
  subject = Stats.new foo: 'bar'
18
18
  assert_equal({ stats: { foo: 'bar' } }, subject.to_hash)
19
19
  end
20
+
21
+ should "take a block" do
22
+ subject = Stats.new do
23
+ field 'bar'
24
+ end
25
+
26
+ assert_equal({stats: { field: 'bar' } }, subject.to_hash)
27
+ end
20
28
  end
21
29
  end
22
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticsearch-dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karel Minarik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-14 00:00:00.000000000 Z
11
+ date: 2015-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler