es-elasticity 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e94caa98bd83150c6799ad689e65a7c8e66816e6
4
- data.tar.gz: 1d3114e6576347abf45dac9c41dc449cd0b298e2
3
+ metadata.gz: 72fe553f4fcf606f9a2582cc1563a78905b03c84
4
+ data.tar.gz: 010cfafae1a421bd9dabf6449a7a6108b3938ac0
5
5
  SHA512:
6
- metadata.gz: f4601bdaa5a6d904c6374ae2b733473258e2e5bf8dbc343114a7d8b9e0a230074e2206adb15123371ce7bd9bed3ac633454e6bd8809877fde26175d9adf603c1
7
- data.tar.gz: 37019fb734b5694b722bc6175ab4ebe41835cedcf06eff307e1408c6db3ce74c1c6680d2ebe1be4c8624e6e4ef594c5383d2b642fc0a6bc5ec55afb588114eb2
6
+ metadata.gz: 068104bc862fb6a45c2ebbb1eab66b11429ff6a995353ed5c3f889de3ebc6359d77c83adf3e4fb09a3ef2e175e6421ec0e061b1b73e60ad7c7bbec2e3a1a3a78
7
+ data.tar.gz: 27454507647e48494a1d02566fcefea35ee63e1662fab5307dcf0c019aa88d12b3f47c4b0c56cadfcc20d8fb27a54242f7c5ad09f48a13da5b89d0bae9ea88f0
data/README.md CHANGED
@@ -26,6 +26,21 @@ Or install it yourself as:
26
26
 
27
27
  ## Usage
28
28
 
29
+ ### Configuration
30
+
31
+ It is recommended you use Typhoeus for HTTP connections to Elasticsearch.
32
+
33
+ ```ruby
34
+ # config/initializers/es-elasticity.rb
35
+ require 'typhoeus'
36
+ require 'typhoeus/adapters/faraday'
37
+
38
+ Elasticity.configure do |config|
39
+ config.client = Elasticsearch::Client.new
40
+ config.namespace = Rails.env.to_s.downcase
41
+ end
42
+ ```
43
+
29
44
  ### Document model definition
30
45
 
31
46
  The first thing to do, is setup a model representing your documents. The class level represents the index, while the instance level represents each Document stored in the index. This is similar to how ActiveRecord maps tables vs rows.
@@ -58,21 +73,23 @@ class Search::User < Elasticity::Document
58
73
  def self.adults
59
74
  date = Date.today - 21.years
60
75
 
61
- # This is the query that will be submited to ES, same format ES would
62
- # expect, translated to a Ruby hash.
76
+ # This is the query that will be submited to ES, same format ES would
77
+ # expect, translated to a Ruby hash, note the pagination params.
63
78
  body = {
79
+ from: 0,
80
+ size: 10,
64
81
  filter: {
65
82
  { range: { birthdate: { gte: date.iso8601 }}},
66
83
  },
67
84
  }
68
85
 
69
- # Creates a search object from the body and return it.
70
- # The returned object # is a lazy evaluated search that behaves like a collection, being
86
+ # Creates a search object from the body and return it.
87
+ # The returned object # is a lazy evaluated search that behaves like a collection, being
71
88
  # automatically triggered when data is iterated over.
72
89
  self.search(body)
73
90
  end
74
91
 
75
- # All models automatically have the id attribute but you need to define the
92
+ # All models automatically have the id attribute but you need to define the
76
93
  # other accessors so that they can be set and get properly.
77
94
  attr_accessor :name, :birthdate
78
95
 
@@ -128,12 +145,15 @@ adults = User.adults
128
145
  adults.each do |user|
129
146
  # do something with user
130
147
  end
148
+
149
+ # Or get the count
150
+ adults.count
131
151
  ```
132
152
 
133
153
  It also has some pretty interesting methods that affects the way the query is performed. Here is a list of available search types:
134
154
 
135
155
  ```ruby
136
- # Returns an array of document instances, this is the default and what the
156
+ # Returns an array of document instances, this is the default and what the
137
157
  # enumerable methods will delegate to.
138
158
  adults.documents
139
159
 
@@ -141,7 +161,7 @@ adults.documents
141
161
  adults.document_hashes
142
162
 
143
163
  # Performs the search using scan&scroll. It returns a cursor that will lazily
144
- # fetch all the pages of the search. It can be iterated by batch/page or by
164
+ # fetch all the pages of the search. It can be iterated by batch/page or by
145
165
  # document.
146
166
  cursor = adults.scan_documents
147
167
  cursor.each_batch { |batch| ... }
@@ -170,7 +190,7 @@ Here is what it looks like:
170
190
  | Index |
171
191
  |¯¯¯¯¯¯¯¯¯¯¯¯¯| |------------> |_____________|
172
192
  | UpdateAlias |---------|
173
- |_____________|
193
+ |_____________|
174
194
  ```
175
195
 
176
196
  Everytime a search operation is performed, it is performed against the main alias; when an update operation is performed, it is performed against the update alias; and, when a delete operation is performed, it is performed against the indexes pointed by both aliases.
@@ -184,7 +204,7 @@ When the mapping needs to change, a hot remapping can be performed by doing the
184
204
  |¯¯¯¯¯¯¯¯¯¯¯¯¯|----------------------> |¯¯¯¯¯¯¯¯¯¯¯¯¯|
185
205
  | MainAlias | | Old Index |
186
206
  |_____________|----------| |_____________|
187
- |
207
+ |
188
208
  |¯¯¯¯¯¯¯¯¯¯¯¯¯| |-----------> |¯¯¯¯¯¯¯¯¯¯¯¯¯|
189
209
  | UpdateAlias |----------------------> | New Index |
190
210
  |_____________| |_____________|
@@ -247,7 +267,7 @@ To extend on the previous example, imagine the `Search::User` class also have th
247
267
  ```ruby
248
268
  def self.adults
249
269
  date = Date.today - 21.years
250
-
270
+
251
271
  body = {
252
272
  filter: {
253
273
  { range: { birthdate: { gte: date.iso8601 }}},
@@ -30,5 +30,5 @@ Gem::Specification.new do |spec|
30
30
 
31
31
  spec.add_dependency "activesupport", "~> 4.0"
32
32
  spec.add_dependency "activemodel", "~> 4.0"
33
- spec.add_dependency "elasticsearch", "~> 1.0.5"
33
+ spec.add_dependency "elasticsearch", "~> 1.0.12"
34
34
  end
@@ -27,7 +27,7 @@ module Elasticity
27
27
  index_base_name = "#{namespace}_#{@config.index_base_name}"
28
28
  end
29
29
 
30
- @strategy = @config.strategy.new(Elasticity.config.client, index_base_name)
30
+ @strategy = @config.strategy.new(Elasticity.config.client, index_base_name, document_type)
31
31
  end
32
32
 
33
33
  # Document type
@@ -16,7 +16,7 @@ module Elasticity
16
16
  end
17
17
 
18
18
  # Generate wrapper methods for @client
19
- %w(index delete get mget search msearch scroll delete_by_query bulk).each do |method_name|
19
+ %w(index delete get mget search count msearch scroll delete_by_query bulk).each do |method_name|
20
20
  define_method(method_name) do |*args, &block|
21
21
  instrument(method_name, args) do
22
22
  @client.public_send(method_name, *args, &block)
@@ -99,6 +99,10 @@ module Elasticity
99
99
  response["hits"]["suggest"] ||= {}
100
100
  end
101
101
 
102
+ def count(args = {})
103
+ @client.count(@search_definition.to_search_args.reverse_merge(args))["count"]
104
+ end
105
+
102
106
  def search_results
103
107
  return @search_results if defined?(@search_results)
104
108
 
@@ -4,8 +4,8 @@ module Elasticity
4
4
  attr_reader :index_base_name
5
5
 
6
6
  def initialize(index_base_name, message)
7
- @index_name = index_name
8
- super("#{index_name}: #{message}")
7
+ @index_base_name = index_base_name
8
+ super("#{index_base_name}: #{message}")
9
9
  end
10
10
  end
11
11
 
@@ -6,10 +6,11 @@ module Elasticity
6
6
  class AliasIndex
7
7
  STATUSES = [:missing, :ok]
8
8
 
9
- def initialize(client, index_base_name)
9
+ def initialize(client, index_base_name, document_type)
10
10
  @client = client
11
11
  @main_alias = index_base_name
12
12
  @update_alias = "#{index_base_name}_update"
13
+ @document_type = document_type
13
14
  end
14
15
 
15
16
  def ref_index_name
@@ -231,17 +232,13 @@ module Elasticity
231
232
  end
232
233
 
233
234
  def settings
234
- args = { index: @main_alias }
235
- settings = @client.index_get_settings(index: @main_alias)
236
- settings[@main_alias]["settings"]
235
+ @client.index_get_settings(index: @main_alias, type: @document_type).values.first
237
236
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
238
237
  nil
239
238
  end
240
239
 
241
240
  def mappings
242
- args = { index: @main_alias }
243
- mapping = @client.index_get_mapping(index: @main_alias)
244
- mapping[@main_alias]["mappings"]
241
+ @client.index_get_mapping(index: @main_alias, type: @document_type).values.first
245
242
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
246
243
  nil
247
244
  end
@@ -3,9 +3,10 @@ module Elasticity
3
3
  class SingleIndex
4
4
  STATUSES = [:missing, :ok]
5
5
 
6
- def initialize(client, index_name)
7
- @client = client
8
- @index_name = index_name
6
+ def initialize(client, index_name, document_type)
7
+ @client = client
8
+ @index_name = index_name
9
+ @document_type = document_type
9
10
  end
10
11
 
11
12
  def ref_index_name
@@ -78,17 +79,13 @@ module Elasticity
78
79
  end
79
80
 
80
81
  def settings
81
- args = { index: @index_name }
82
- settings = @client.index_get_settings(index: @index_name)
83
- settings[@index_name]["settings"]
82
+ @client.index_get_settings(index: @index_name, type: @document_type).values.first
84
83
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
85
84
  nil
86
85
  end
87
86
 
88
87
  def mappings
89
- args = { index: @index_name }
90
- mapping = @client.index_get_mapping(index: @index_name)
91
- mapping[@index_name]["mappings"]
88
+ @client.index_get_mapping(index: @index_name, type: @document_type).values.first
92
89
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
93
90
  nil
94
91
  end
@@ -1,3 +1,3 @@
1
1
  module Elasticity
2
- VERSION = "0.3.6"
2
+ VERSION = "0.3.7"
3
3
  end
@@ -31,7 +31,7 @@ RSpec.describe "Persistence", elasticsearch: true do
31
31
  subject.delete_index
32
32
  end
33
33
 
34
- it "successfully index, update, search and delete" do
34
+ it "successfully index, update, search, count and delete" do
35
35
  john = subject.new(name: "John", birthdate: "1985-10-31")
36
36
  mari = subject.new(name: "Mari", birthdate: "1986-09-24")
37
37
 
@@ -46,6 +46,8 @@ RSpec.describe "Persistence", elasticsearch: true do
46
46
  expect(results[0]).to eq(john)
47
47
  expect(results[1]).to eq(mari)
48
48
 
49
+ expect(subject.search({query: {filtered: { query: { match_all: {} } } } }).count).to eq(2)
50
+
49
51
  john.update
50
52
  mari.delete
51
53
 
@@ -1,14 +1,14 @@
1
1
  RSpec.describe Elasticity::Strategies::SingleIndex, elasticsearch: true do
2
2
  subject do
3
- described_class.new(Elasticity.config.client, "test_index_name")
3
+ described_class.new(Elasticity.config.client, "test_index_name", "document")
4
4
  end
5
5
 
6
6
  let :index_def do
7
7
  {
8
- mappings: {
9
- document: {
10
- properties: {
11
- name: { type: "string" }
8
+ "mappings" => {
9
+ "document" => {
10
+ "properties" => {
11
+ "name" => { "type" => "string" }
12
12
  }
13
13
  }
14
14
  }
@@ -21,10 +21,10 @@ RSpec.describe Elasticity::Strategies::SingleIndex, elasticsearch: true do
21
21
 
22
22
  it "allows creating, recreating and deleting an index" do
23
23
  subject.create(index_def)
24
- expect(subject.mappings).to eq({"document"=>{"properties"=>{"name"=>{"type"=>"string"}}}})
24
+ expect(subject.mappings).to eq(index_def)
25
25
 
26
26
  subject.recreate(index_def)
27
- expect(subject.mappings).to eq({"document"=>{"properties"=>{"name"=>{"type"=>"string"}}}})
27
+ expect(subject.mappings).to eq(index_def)
28
28
 
29
29
  subject.delete
30
30
  expect(subject.mappings).to be nil
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: es-elasticity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Kochenburger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-22 00:00:00.000000000 Z
11
+ date: 2015-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -156,14 +156,14 @@ dependencies:
156
156
  requirements:
157
157
  - - "~>"
158
158
  - !ruby/object:Gem::Version
159
- version: 1.0.5
159
+ version: 1.0.12
160
160
  type: :runtime
161
161
  prerelease: false
162
162
  version_requirements: !ruby/object:Gem::Requirement
163
163
  requirements:
164
164
  - - "~>"
165
165
  - !ruby/object:Gem::Version
166
- version: 1.0.5
166
+ version: 1.0.12
167
167
  description: Elasticity provides a higher level abstraction on top of [elasticsearch-ruby](https://github.com/elasticsearch/elasticsearch-ruby)
168
168
  gem
169
169
  email:
@@ -222,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
222
222
  version: '0'
223
223
  requirements: []
224
224
  rubyforge_project:
225
- rubygems_version: 2.2.2
225
+ rubygems_version: 2.4.5
226
226
  signing_key:
227
227
  specification_version: 4
228
228
  summary: ActiveModel-based library for working with Elasticsearch