search_flip 2.0.0.beta6 → 2.0.0.beta7

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
  SHA256:
3
- metadata.gz: 47ca9a2ae9454e232a82a2d0ca91d58cb0f8d79e087795c0e9dbae5ad9ac6216
4
- data.tar.gz: 721e6a0b0cc6abbf8d501e09903a1c9a3cb0ba526f1c9a403461ec38c3607f15
3
+ metadata.gz: cf6b3a5f6bfc602628bc76ea55253357e36fde027c3b203c4755c7521694241d
4
+ data.tar.gz: de3a3de47e38a52bf780cc0d51f3dbf22287f57864ac99888832145b22d556ba
5
5
  SHA512:
6
- metadata.gz: d0b4d33d00bb54b2d0b16fd1df450f93a412097312ac8726ab5f3d76bfd5c4c89a1374d6f61cf62920ebd4d86f209da330b20dd61a446c0d3bf5b25036b4cee3
7
- data.tar.gz: 4d18ae080aa3dbdbdd024e5a3ce33529c002162bf9b5057848c7813546aab64dc82d2cd0ff91de26b155898559834bb8aabec961bea51488abe829ca5782dc52
6
+ metadata.gz: 7ec3e29db16f930294bd138196c12dbb52be7ceb5ab6198964a7910ad88dd791d4c6ff689758587c29e113d20b05fa5a75d717b0524e482052ee1cca629d466a
7
+ data.tar.gz: f0cd3bf68475fbd49d2e977e503029022104425f909bf21299319cd70e78a8cceddef102523d9b3c563954f9294eb0da704c8bc5ed7b9946b8e7abf370f3b5a2
data/.travis.yml CHANGED
@@ -13,5 +13,5 @@ before_install:
13
13
  - sleep 10
14
14
  install:
15
15
  - travis_retry bundle install
16
- script: rake test --trace
16
+ script: rspec
17
17
 
data/README.md CHANGED
@@ -207,6 +207,8 @@ CommentIndex.create_index
207
207
  CommentIndex.index_exists?
208
208
  CommentIndex.delete_index
209
209
  CommentIndex.update_mapping
210
+ CommentIndex.close_index
211
+ CommentIndex.open_index
210
212
  ```
211
213
 
212
214
  index records (automatically uses the bulk API):
@@ -30,6 +30,17 @@ module SearchFlip
30
30
  @version ||= http_client.headers(accept: "application/json").get("#{base_url}/").parse["version"]["number"]
31
31
  end
32
32
 
33
+ # Queries and returns the ElasticSearch cluster health.
34
+ #
35
+ # @example
36
+ # connection.cluster_health # => { "status" => "green", ... }
37
+ #
38
+ # @return [Hash] The raw response
39
+
40
+ def cluster_health
41
+ http_client.headers(accept: "application/json").get("#{base_url}/_cluster/health").parse
42
+ end
43
+
33
44
  # Uses the ElasticSearch Multi Search API to execute multiple search requests
34
45
  # within a single request. Raises SearchFlip::ResponseError in case any
35
46
  # errors occur.
@@ -83,6 +94,18 @@ module SearchFlip
83
94
  .parse
84
95
  end
85
96
 
97
+ # Sends an analyze request to ElasticSearch. Raises
98
+ # SearchFlip::ResponseError in case any errors occur.
99
+ #
100
+ # @example
101
+ # connection.analyze(analyzer: "standard", text: "this is a test")
102
+ #
103
+ # @return [Hash] The raw response
104
+
105
+ def analyze(request, params = {})
106
+ http_client.headers(accept: "application/json").post("#{base_url}/_analyze", json: request, params: params).parse
107
+ end
108
+
86
109
  # Fetches information about the specified index aliases. Raises
87
110
  # SearchFlip::ResponseError in case any errors occur.
88
111
  #
@@ -157,6 +180,32 @@ module SearchFlip
157
180
  true
158
181
  end
159
182
 
183
+ # Closes the specified index within ElasticSearch. Raises
184
+ # SearchFlip::ResponseError in case any errors occur
185
+ #
186
+ # @param index_name [String] The index name
187
+ #
188
+ # @return [Boolean] Returns true or raises SearchFlip::ResponseError
189
+
190
+ def close_index(index_name)
191
+ http_client.post("#{index_url(index_name)}/_close")
192
+
193
+ true
194
+ end
195
+
196
+ # Opens the specified index within ElasticSearch. Raises
197
+ # SearchFlip::ResponseError in case any errors occur
198
+ #
199
+ # @param index_name [String] The index name
200
+ #
201
+ # @return [Boolean] Returns true or raises SearchFlip::ResponseError
202
+
203
+ def open_index(index_name)
204
+ http_client.post("#{index_url(index_name)}/_open")
205
+
206
+ true
207
+ end
208
+
160
209
  # Updates the index settings within ElasticSearch according to the index
161
210
  # settings specified. Raises SearchFlip::ResponseError in case any
162
211
  # errors occur.
@@ -338,6 +338,24 @@ module SearchFlip
338
338
  connection.create_index(index_name_with_prefix, json)
339
339
  end
340
340
 
341
+ # Closes the index within ElasticSearch. Raises SearchFlip::ResponseError
342
+ # in case any errors occur.
343
+ #
344
+ # @return [Boolean] Returns true or raises SearchFlip::ResponseError
345
+
346
+ def close_index
347
+ connection.close_index(index_name_with_prefix)
348
+ end
349
+
350
+ # Opens the index within ElasticSearch. Raises SearchFlip::ResponseError
351
+ # in case any errors occur.
352
+ #
353
+ # @return [Boolean] Returns true or raises SearchFlip::ResponseError
354
+
355
+ def open_index
356
+ connection.open_index(index_name_with_prefix)
357
+ end
358
+
341
359
  # Updates the index settings within ElasticSearch according to the index
342
360
  # settings specified. Raises SearchFlip::ResponseError in case any
343
361
  # errors occur.
@@ -396,6 +414,10 @@ module SearchFlip
396
414
  # SearchFlip::ResponseError specific exceptions in case any errors
397
415
  # occur.
398
416
  #
417
+ # @example
418
+ # UserIndex.get(1)
419
+ # UserIndex.get(1, routing: "key")
420
+ #
399
421
  # @param id [String, Fixnum] The id to get
400
422
  # @param params [Hash] Optional params for the request
401
423
  #
@@ -405,6 +427,34 @@ module SearchFlip
405
427
  connection.http_client.headers(accept: "application/json").get("#{type_url}/#{id}", params: params).parse
406
428
  end
407
429
 
430
+ # Retrieves the documents specified by ids from elasticsearch.
431
+ #
432
+ # @example
433
+ # UserIndex.mget(ids: [1, 2, 3])
434
+ # UserIndex.mget(docs: [{ _id: 1, routing: "key1" }, { _id: 2, routing: "key2" }])
435
+ # UserIndex.mget({ ids: [1, 2, 3] }, stored_fields: "username,full_name")
436
+ #
437
+ # @param request [Hash] The raw request
438
+ # @param params [Hash] Optional params for the request
439
+ #
440
+ # @return [Hash] The raw response
441
+
442
+ def mget(request, params = {})
443
+ connection.http_client.headers(accept: "application/json").post("#{type_url}/_mget", json: request, params: params).parse
444
+ end
445
+
446
+ # Sends an analyze request to ElasticSearch. Raises
447
+ # SearchFlip::ResponseError in case any errors occur.
448
+ #
449
+ # @example
450
+ # CommentIndex.analyze(analyzer: "standard", text: "this is a test")
451
+ #
452
+ # @return [Hash] The raw response
453
+
454
+ def analyze(request, params = {})
455
+ connection.http_client.headers(accept: "application/json").post("#{index_url}/_analyze", json: request, params: params).parse
456
+ end
457
+
408
458
  # Sends a index refresh request to ElasticSearch. Raises
409
459
  # SearchFlip::ResponseError in case any errors occur.
410
460
 
@@ -1,5 +1,5 @@
1
1
 
2
2
  module SearchFlip
3
- VERSION = "2.0.0.beta6"
3
+ VERSION = "2.0.0.beta7"
4
4
  end
5
5
 
@@ -82,17 +82,21 @@ RSpec.describe SearchFlip::Bulk do
82
82
 
83
83
  block = lambda do
84
84
  ProductIndex.bulk bulk_max_mb: 1_000 do |bulk|
85
- 100.times do
85
+ 20.times do
86
86
  bulk.index product.id, ProductIndex.serialize(product)
87
87
  end
88
88
  end
89
89
  end
90
90
 
91
- expect(&block).to raise_error(SearchFlip::ResponseError)
91
+ if ProductIndex.connection.version.to_i <= 2
92
+ expect(&block).to raise_error(SearchFlip::ConnectionError)
93
+ else
94
+ expect(&block).to raise_error(SearchFlip::ResponseError)
95
+ end
92
96
 
93
97
  block = lambda do
94
98
  ProductIndex.bulk bulk_max_mb: 100 do |bulk|
95
- 100.times do
99
+ 20.times do
96
100
  bulk.index product.id, ProductIndex.serialize(product)
97
101
  end
98
102
  end
@@ -2,6 +2,18 @@
2
2
  require File.expand_path("../spec_helper", __dir__)
3
3
 
4
4
  RSpec.describe SearchFlip::Connection do
5
+ describe "#version" do
6
+ it "returns the version" do
7
+ expect(SearchFlip::Connection.new.version).to match(/\A[0-9.]+\z/)
8
+ end
9
+ end
10
+
11
+ describe "#cluster_health" do
12
+ it "returns the cluster health" do
13
+ expect(["red", "yellow", "green"]).to include(SearchFlip::Connection.new.cluster_health["status"])
14
+ end
15
+ end
16
+
5
17
  describe "#base_url" do
6
18
  it "returns the correct url" do
7
19
  expect(SearchFlip::Connection.new(base_url: "base url").base_url).to eq("base url")
@@ -112,6 +124,51 @@ RSpec.describe SearchFlip::Connection do
112
124
  end
113
125
  end
114
126
 
127
+ describe "#close_index" do
128
+ it "closes the specified index" do
129
+ begin
130
+ connection = SearchFlip::Connection.new
131
+
132
+ connection.create_index("index_name")
133
+ sleep(0.1) while connection.cluster_health["status"] == "red"
134
+
135
+ connection.close_index("index_name")
136
+
137
+ expect(connection.get_indices("index_name").first["status"]).to eq("close")
138
+ ensure
139
+ connection.delete_index("index_name") if connection.index_exists?("index_name")
140
+ end
141
+ end
142
+ end
143
+
144
+ describe "#open_index" do
145
+ it "opens the specified index" do
146
+ begin
147
+ connection = SearchFlip::Connection.new
148
+
149
+ connection.create_index("index_name")
150
+ sleep(0.1) while connection.cluster_health["status"] == "red"
151
+ connection.close_index("index_name")
152
+
153
+ connection.open_index("index_name")
154
+
155
+ expect(connection.get_indices("index_name").first["status"]).to eq("open")
156
+ ensure
157
+ connection.delete_index("index_name") if connection.index_exists?("index_name")
158
+ end
159
+ end
160
+ end
161
+
162
+ describe ".analyze" do
163
+ it "analyzes the provided request" do
164
+ connection = SearchFlip::Connection.new
165
+
166
+ tokens = connection.analyze(analyzer: "standard", text: "some text")["tokens"].map { |token| token["token"] }
167
+
168
+ expect(tokens).to include("some", "text")
169
+ end
170
+ end
171
+
115
172
  describe "#update_index_settings" do
116
173
  it "updates the index settings" do
117
174
  begin
@@ -82,6 +82,32 @@ RSpec.describe SearchFlip::Index do
82
82
  end
83
83
  end
84
84
 
85
+ describe ".close_index" do
86
+ it "delegates to connection" do
87
+ allow(TestIndex.connection).to receive(:close_index).and_call_original
88
+
89
+ TestIndex.create_index
90
+ sleep(0.1) while TestIndex.connection.cluster_health["status"] == "red"
91
+ TestIndex.close_index
92
+
93
+ expect(TestIndex.connection).to have_received(:close_index).with("test")
94
+ end
95
+ end
96
+
97
+ describe ".open_index" do
98
+ it "delegates to connection" do
99
+ allow(TestIndex.connection).to receive(:open_index).and_call_original
100
+
101
+ TestIndex.create_index
102
+ sleep(0.1) while TestIndex.connection.cluster_health["status"] == "red"
103
+ TestIndex.close_index
104
+
105
+ TestIndex.open_index
106
+
107
+ expect(TestIndex.connection).to have_received(:open_index).with("test")
108
+ end
109
+ end
110
+
85
111
  describe ".index_exists?" do
86
112
  it "delegates to connection" do
87
113
  TestIndex.create_index
@@ -161,6 +187,16 @@ RSpec.describe SearchFlip::Index do
161
187
  end
162
188
  end
163
189
 
190
+ describe ".analyze" do
191
+ it "analyzes the provided request" do
192
+ ProductIndex.import create(:product)
193
+
194
+ tokens = ProductIndex.analyze(analyzer: "standard", text: "some text")["tokens"].map { |token| token["token"] }
195
+
196
+ expect(tokens).to include("some", "text")
197
+ end
198
+ end
199
+
164
200
  describe ".refresh" do
165
201
  it "delegates to connection" do
166
202
  TestIndex.create_index
@@ -389,6 +425,7 @@ RSpec.describe SearchFlip::Index do
389
425
 
390
426
  it "passes params" do
391
427
  product = create(:product)
428
+
392
429
  ProductIndex.import product
393
430
 
394
431
  expect(ProductIndex.get(product.id).keys).to include("_source")
@@ -396,6 +433,25 @@ RSpec.describe SearchFlip::Index do
396
433
  end
397
434
  end
398
435
 
436
+ describe ".mget" do
437
+ it "retrieves the documents" do
438
+ product1, product2, product3 = create_list(:product, 3)
439
+
440
+ ProductIndex.import [product1, product2, product3]
441
+
442
+ expect(ProductIndex.mget(ids: [product1.id, product2.id])["docs"].map { |doc| doc["found"] }).to eq([true, true])
443
+ end
444
+
445
+ it "passes params" do
446
+ product = create(:product)
447
+
448
+ ProductIndex.import product
449
+
450
+ expect(ProductIndex.mget({ ids: [product.id] }, _source: false)["docs"].map { |doc| doc["found"] }).to eq([true])
451
+ expect(ProductIndex.mget({ ids: [product.id] }, _source: false)["docs"].first).not_to include("_source")
452
+ end
453
+ end
454
+
399
455
  describe ".scope" do
400
456
  it "adds a scope" do
401
457
  temp_product_index = Class.new(ProductIndex)
data/spec/spec_helper.rb CHANGED
@@ -186,6 +186,10 @@ class TestIndex
186
186
  def self.index_name
187
187
  "test"
188
188
  end
189
+
190
+ def self.serialize(object)
191
+ { id: object.id }
192
+ end
189
193
  end
190
194
 
191
195
  TestIndex.delete_index if TestIndex.index_exists?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: search_flip
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.beta6
4
+ version: 2.0.0.beta7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Vetter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-19 00:00:00.000000000 Z
11
+ date: 2019-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord