search_flip 3.2.1 → 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +1 -0
- data/.rubocop.yml +2 -1
- data/CHANGELOG.md +6 -0
- data/README.md +27 -34
- data/lib/search_flip.rb +1 -0
- data/lib/search_flip/bulk.rb +1 -1
- data/lib/search_flip/config.rb +7 -1
- data/lib/search_flip/connection.rb +27 -14
- data/lib/search_flip/criteria.rb +1 -1
- data/lib/search_flip/index.rb +9 -3
- data/lib/search_flip/json.rb +4 -9
- data/lib/search_flip/to_json.rb +1 -29
- data/lib/search_flip/version.rb +1 -1
- data/search_flip.gemspec +1 -0
- data/spec/search_flip/json_spec.rb +45 -0
- metadata +18 -4
- data/spec/search_flip/to_json_spec.rb +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 021f5dd6d172381d777444631e96f2ebff0759b002f38b9ca555f844184d5dd9
|
4
|
+
data.tar.gz: ded7bbcab491ca187a68214377aa1f6ab2e3774739224744181aca16cf81e504
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: adc9acf973b399aa0595bdc42705516f9a13cb4cfbd18b5ee5db613d8c91261c3bba80834848b8079c4569431dfcbcff0368787b5edeb42f517e864721a54bef
|
7
|
+
data.tar.gz: 0b8769e3c59d1953036b2fc1618cfbd15db7bc4d19beef0a71e24aeb82830b499eff820e061b397ee20c15d4942941df69cc4dff2b20e4b35119193e78af74b7
|
data/.github/workflows/test.yml
CHANGED
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
**Full-Featured Elasticsearch Ruby Client with a Chainable DSL**
|
5
5
|
|
6
|
-
[![Build
|
6
|
+
[![Build](https://github.com/mrkamel/search_flip/workflows/test/badge.svg)](https://github.com/mrkamel/search_flip/actions?query=workflow%3Atest+branch%3Amaster)
|
7
7
|
[![Gem Version](https://badge.fury.io/rb/search_flip.svg)](http://badge.fury.io/rb/search_flip)
|
8
8
|
|
9
9
|
Using SearchFlip it is dead-simple to create index classes that correspond to
|
@@ -51,8 +51,7 @@ CommentIndex.search("hello world").where(available: true).sort(id: "desc").aggre
|
|
51
51
|
|
52
52
|
```
|
53
53
|
|
54
|
-
Finally, SearchFlip comes with a minimal set of dependencies
|
55
|
-
and oj only).
|
54
|
+
Finally, SearchFlip comes with a minimal set of dependencies.
|
56
55
|
|
57
56
|
## Reference Docs
|
58
57
|
|
@@ -883,52 +882,46 @@ Thus, if your ORM supports `.find_each`, `#id` and `#where` you are already
|
|
883
882
|
good to go. Otherwise, simply add your custom implementation of those methods
|
884
883
|
that work with whatever ORM you use.
|
885
884
|
|
886
|
-
##
|
885
|
+
## JSON
|
887
886
|
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
However, `JSON.generate` in ruby by default outputs something like:
|
887
|
+
SearchFlip is using the [Oj gem](https://github.com/ohler55/oj) to generate
|
888
|
+
JSON. More concretely, SearchFlip is using:
|
892
889
|
|
893
890
|
```ruby
|
894
|
-
|
895
|
-
# => "{\"time\":\"2018-02-22 18:19:33 UTC\"}"
|
891
|
+
Oj.dump({ key: "value" }, mode: :custom, use_to_json: true, time_format: :xmlschema, bigdecimal_as_decimal: false)
|
896
892
|
```
|
897
893
|
|
898
|
-
|
899
|
-
|
900
|
-
However,
|
901
|
-
|
894
|
+
The `use_to_json` option is used for maximum compatibility, most importantly
|
895
|
+
when using rails `ActiveSupport::TimeWithZone` timestamps, which `oj` can not
|
896
|
+
serialize natively. However, `use_to_json` adds performance overhead. You can
|
897
|
+
change the json options via:
|
902
898
|
|
903
899
|
```ruby
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
900
|
+
SearchFlip::Config[:json_options] = {
|
901
|
+
mode: :custom,
|
902
|
+
use_to_json: false,
|
903
|
+
time_format: :xmlschema,
|
904
|
+
bigdecimal_as_decimal: false
|
905
|
+
}
|
909
906
|
```
|
910
907
|
|
911
|
-
|
912
|
-
JSON. More concretely, SearchFlip is using:
|
908
|
+
However, you then have to convert timestamps manually for indexation via e.g.:
|
913
909
|
|
914
910
|
```ruby
|
915
|
-
|
916
|
-
|
911
|
+
class MyIndex
|
912
|
+
# ...
|
917
913
|
|
918
|
-
|
914
|
+
def self.serialize(model)
|
915
|
+
{
|
916
|
+
# ...
|
919
917
|
|
920
|
-
|
921
|
-
|
922
|
-
|
918
|
+
created_at: model.created_at.to_time
|
919
|
+
}
|
920
|
+
end
|
921
|
+
end
|
923
922
|
```
|
924
923
|
|
925
|
-
|
926
|
-
`Date` and `DateTime` to get proper serialization. You can either add them on
|
927
|
-
your own, via other libraries or by simply using:
|
928
|
-
|
929
|
-
```ruby
|
930
|
-
require "search_flip/to_json"
|
931
|
-
```
|
924
|
+
Please check out the oj docs for more details.
|
932
925
|
|
933
926
|
## Feature Support
|
934
927
|
|
data/lib/search_flip.rb
CHANGED
data/lib/search_flip/bulk.rb
CHANGED
data/lib/search_flip/config.rb
CHANGED
@@ -5,6 +5,12 @@ module SearchFlip
|
|
5
5
|
bulk_limit: 1_000,
|
6
6
|
bulk_max_mb: 100,
|
7
7
|
auto_refresh: false,
|
8
|
-
instrumenter: NullInstrumenter.new
|
8
|
+
instrumenter: NullInstrumenter.new,
|
9
|
+
json_options: {
|
10
|
+
mode: :custom,
|
11
|
+
use_to_json: true,
|
12
|
+
time_format: :xmlschema,
|
13
|
+
bigdecimal_as_decimal: false
|
14
|
+
}
|
9
15
|
}
|
10
16
|
end
|
@@ -28,7 +28,11 @@ module SearchFlip
|
|
28
28
|
|
29
29
|
def version
|
30
30
|
@version_mutex.synchronize do
|
31
|
-
@version ||=
|
31
|
+
@version ||= begin
|
32
|
+
response = http_client.headers(accept: "application/json").get("#{base_url}/")
|
33
|
+
|
34
|
+
SearchFlip::JSON.parse(response.to_s)["version"]["number"]
|
35
|
+
end
|
32
36
|
end
|
33
37
|
end
|
34
38
|
|
@@ -40,7 +44,9 @@ module SearchFlip
|
|
40
44
|
# @return [Hash] The raw response
|
41
45
|
|
42
46
|
def cluster_health
|
43
|
-
http_client.headers(accept: "application/json").get("#{base_url}/_cluster/health")
|
47
|
+
response = http_client.headers(accept: "application/json").get("#{base_url}/_cluster/health")
|
48
|
+
|
49
|
+
SearchFlip::JSON.parse(response.to_s)
|
44
50
|
end
|
45
51
|
|
46
52
|
# Uses the Elasticsearch Multi Search API to execute multiple search requests
|
@@ -71,7 +77,7 @@ module SearchFlip
|
|
71
77
|
.headers(accept: "application/json", content_type: "application/x-ndjson")
|
72
78
|
.post("#{base_url}/_msearch", body: payload)
|
73
79
|
|
74
|
-
raw_response.
|
80
|
+
SearchFlip::JSON.parse(raw_response.to_s)["responses"].map.with_index do |response, index|
|
75
81
|
SearchFlip::Response.new(criterias[index], response)
|
76
82
|
end
|
77
83
|
end
|
@@ -90,10 +96,11 @@ module SearchFlip
|
|
90
96
|
# @return [Hash] The raw response
|
91
97
|
|
92
98
|
def update_aliases(payload)
|
93
|
-
http_client
|
99
|
+
response = http_client
|
94
100
|
.headers(accept: "application/json", content_type: "application/json")
|
95
101
|
.post("#{base_url}/_aliases", body: SearchFlip::JSON.generate(payload))
|
96
|
-
|
102
|
+
|
103
|
+
SearchFlip::JSON.parse(response.to_s)
|
97
104
|
end
|
98
105
|
|
99
106
|
# Sends an analyze request to Elasticsearch. Raises
|
@@ -105,10 +112,11 @@ module SearchFlip
|
|
105
112
|
# @return [Hash] The raw response
|
106
113
|
|
107
114
|
def analyze(request, params = {})
|
108
|
-
http_client
|
115
|
+
response = http_client
|
109
116
|
.headers(accept: "application/json")
|
110
117
|
.post("#{base_url}/_analyze", json: request, params: params)
|
111
|
-
|
118
|
+
|
119
|
+
SearchFlip::JSON.parse(response.to_s)
|
112
120
|
end
|
113
121
|
|
114
122
|
# Fetches information about the specified index aliases. Raises
|
@@ -124,10 +132,11 @@ module SearchFlip
|
|
124
132
|
# @return [Hash] The raw response
|
125
133
|
|
126
134
|
def get_aliases(index_name: "*", alias_name: "*")
|
127
|
-
http_client
|
135
|
+
response = http_client
|
128
136
|
.headers(accept: "application/json", content_type: "application/json")
|
129
137
|
.get("#{base_url}/#{index_name}/_alias/#{alias_name}")
|
130
|
-
|
138
|
+
|
139
|
+
SearchFlip::JSON.parse(response.to_s)
|
131
140
|
end
|
132
141
|
|
133
142
|
# Returns whether or not the associated Elasticsearch alias already
|
@@ -159,10 +168,11 @@ module SearchFlip
|
|
159
168
|
# @return [Array] The raw response
|
160
169
|
|
161
170
|
def get_indices(name = "*", params: {})
|
162
|
-
http_client
|
171
|
+
response = http_client
|
163
172
|
.headers(accept: "application/json", content_type: "application/json")
|
164
173
|
.get("#{base_url}/_cat/indices/#{name}", params: params)
|
165
|
-
|
174
|
+
|
175
|
+
SearchFlip::JSON.parse(response.to_s)
|
166
176
|
end
|
167
177
|
|
168
178
|
alias_method :cat_indices, :get_indices
|
@@ -259,10 +269,11 @@ module SearchFlip
|
|
259
269
|
# @return [Hash] The index settings
|
260
270
|
|
261
271
|
def get_index_settings(index_name)
|
262
|
-
http_client
|
272
|
+
response = http_client
|
263
273
|
.headers(accept: "application/json")
|
264
274
|
.get("#{index_url(index_name)}/_settings")
|
265
|
-
|
275
|
+
|
276
|
+
SearchFlip::JSON.parse(response.to_s)
|
266
277
|
end
|
267
278
|
|
268
279
|
# Sends a refresh request to Elasticsearch. Raises
|
@@ -310,7 +321,9 @@ module SearchFlip
|
|
310
321
|
url = type_name ? type_url(index_name, type_name) : index_url(index_name)
|
311
322
|
params = type_name && version.to_f >= 6.7 ? { include_type_name: true } : {}
|
312
323
|
|
313
|
-
http_client.headers(accept: "application/json").get("#{url}/_mapping", params: params)
|
324
|
+
response = http_client.headers(accept: "application/json").get("#{url}/_mapping", params: params)
|
325
|
+
|
326
|
+
SearchFlip::JSON.parse(response.to_s)
|
314
327
|
end
|
315
328
|
|
316
329
|
# Deletes the specified index from Elasticsearch. Raises
|
data/lib/search_flip/criteria.rb
CHANGED
@@ -608,7 +608,7 @@ module SearchFlip
|
|
608
608
|
http_request.post("#{target.type_url}/_search", params: request_params, json: request)
|
609
609
|
end
|
610
610
|
|
611
|
-
SearchFlip::Response.new(self, http_response.
|
611
|
+
SearchFlip::Response.new(self, SearchFlip::JSON.parse(http_response.to_s))
|
612
612
|
rescue SearchFlip::ConnectionError, SearchFlip::ResponseError => e
|
613
613
|
raise e unless failsafe_value
|
614
614
|
|
data/lib/search_flip/index.rb
CHANGED
@@ -455,7 +455,9 @@ module SearchFlip
|
|
455
455
|
# @return [Hash] The specified document
|
456
456
|
|
457
457
|
def get(id, params = {})
|
458
|
-
connection.http_client.headers(accept: "application/json").get("#{type_url}/#{id}", params: params)
|
458
|
+
response = connection.http_client.headers(accept: "application/json").get("#{type_url}/#{id}", params: params)
|
459
|
+
|
460
|
+
SearchFlip::JSON.parse(response.to_s)
|
459
461
|
end
|
460
462
|
|
461
463
|
# Retrieves the documents specified by ids from elasticsearch.
|
@@ -471,7 +473,9 @@ module SearchFlip
|
|
471
473
|
# @return [Hash] The raw response
|
472
474
|
|
473
475
|
def mget(request, params = {})
|
474
|
-
connection.http_client.headers(accept: "application/json").post("#{type_url}/_mget", json: request, params: params)
|
476
|
+
response = connection.http_client.headers(accept: "application/json").post("#{type_url}/_mget", json: request, params: params)
|
477
|
+
|
478
|
+
SearchFlip::JSON.parse(response.to_s)
|
475
479
|
end
|
476
480
|
|
477
481
|
# Sends an analyze request to Elasticsearch. Raises
|
@@ -483,7 +487,9 @@ module SearchFlip
|
|
483
487
|
# @return [Hash] The raw response
|
484
488
|
|
485
489
|
def analyze(request, params = {})
|
486
|
-
connection.http_client.headers(accept: "application/json").post("#{index_url}/_analyze", json: request, params: params)
|
490
|
+
response = connection.http_client.headers(accept: "application/json").post("#{index_url}/_analyze", json: request, params: params)
|
491
|
+
|
492
|
+
SearchFlip::JSON.parse(response.to_s)
|
487
493
|
end
|
488
494
|
|
489
495
|
# Sends a index refresh request to Elasticsearch. Raises
|
data/lib/search_flip/json.rb
CHANGED
@@ -1,16 +1,11 @@
|
|
1
1
|
module SearchFlip
|
2
2
|
class JSON
|
3
|
-
|
4
|
-
|
5
|
-
use_to_json: true
|
6
|
-
}
|
7
|
-
|
8
|
-
def self.default_options
|
9
|
-
@default_options
|
3
|
+
def self.generate(obj)
|
4
|
+
Oj.dump(obj, SearchFlip::Config[:json_options])
|
10
5
|
end
|
11
6
|
|
12
|
-
def self.
|
13
|
-
|
7
|
+
def self.parse(json)
|
8
|
+
::JSON.parse(json)
|
14
9
|
end
|
15
10
|
end
|
16
11
|
end
|
data/lib/search_flip/to_json.rb
CHANGED
@@ -1,29 +1 @@
|
|
1
|
-
|
2
|
-
require "date"
|
3
|
-
require "json"
|
4
|
-
|
5
|
-
class Time
|
6
|
-
def to_json(*args)
|
7
|
-
iso8601(6).to_json
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
class Date
|
12
|
-
def to_json(*args)
|
13
|
-
iso8601.to_json
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
class DateTime
|
18
|
-
def to_json(*args)
|
19
|
-
iso8601(6).to_json
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
if defined?(ActiveSupport)
|
24
|
-
class ActiveSupport::TimeWithZone
|
25
|
-
def to_json(*args)
|
26
|
-
iso8601(6).to_json
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
1
|
+
warn "[DEPRECATION] Using search_flip/to_json is not neccessary anymore"
|
data/lib/search_flip/version.rb
CHANGED
data/search_flip.gemspec
CHANGED
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path("../spec_helper", __dir__)
|
2
|
+
|
3
|
+
RSpec.describe SearchFlip::JSON do
|
4
|
+
describe ".generate" do
|
5
|
+
it "encodes timestamps correctly" do
|
6
|
+
Timecop.freeze "2020-06-01 12:00:00 UTC" do
|
7
|
+
expect(described_class.generate(timestamp: Time.now.utc)).to eq('{"timestamp":"2020-06-01T12:00:00.000Z"}')
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
it "encodes bigdecimals as string" do
|
12
|
+
expect(described_class.generate(value: BigDecimal(1))).to eq('{"value":"1.0"}')
|
13
|
+
end
|
14
|
+
|
15
|
+
it "delegates to Oj" do
|
16
|
+
allow(Oj).to receive(:dump)
|
17
|
+
|
18
|
+
payload = { key: "value" }
|
19
|
+
|
20
|
+
described_class.generate(payload)
|
21
|
+
|
22
|
+
expect(Oj).to have_received(:dump).with(payload, mode: :custom, use_to_json: true, time_format: :xmlschema, bigdecimal_as_decimal: false)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "generates json" do
|
26
|
+
expect(described_class.generate(key: "value")).to eq('{"key":"value"}')
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe ".parse" do
|
31
|
+
it "returns the parsed json payload" do
|
32
|
+
expect(described_class.parse('{"key":"value"}')).to eq("key" => "value")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "delegates to JSON" do
|
36
|
+
allow(JSON).to receive(:parse)
|
37
|
+
|
38
|
+
payload = '{"key":"value"}'
|
39
|
+
|
40
|
+
described_class.parse(payload)
|
41
|
+
|
42
|
+
expect(JSON).to have_received(:parse).with(payload)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
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: 3.
|
4
|
+
version: 3.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Vetter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -178,6 +178,20 @@ dependencies:
|
|
178
178
|
- - ">="
|
179
179
|
- !ruby/object:Gem::Version
|
180
180
|
version: '0'
|
181
|
+
- !ruby/object:Gem::Dependency
|
182
|
+
name: json
|
183
|
+
requirement: !ruby/object:Gem::Requirement
|
184
|
+
requirements:
|
185
|
+
- - ">="
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
version: '0'
|
188
|
+
type: :runtime
|
189
|
+
prerelease: false
|
190
|
+
version_requirements: !ruby/object:Gem::Requirement
|
191
|
+
requirements:
|
192
|
+
- - ">="
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: '0'
|
181
195
|
- !ruby/object:Gem::Dependency
|
182
196
|
name: oj
|
183
197
|
requirement: !ruby/object:Gem::Requirement
|
@@ -259,11 +273,11 @@ files:
|
|
259
273
|
- spec/search_flip/criteria_spec.rb
|
260
274
|
- spec/search_flip/http_client_spec.rb
|
261
275
|
- spec/search_flip/index_spec.rb
|
276
|
+
- spec/search_flip/json_spec.rb
|
262
277
|
- spec/search_flip/model_spec.rb
|
263
278
|
- spec/search_flip/null_instrumenter_spec.rb
|
264
279
|
- spec/search_flip/response_spec.rb
|
265
280
|
- spec/search_flip/result_spec.rb
|
266
|
-
- spec/search_flip/to_json_spec.rb
|
267
281
|
- spec/spec_helper.rb
|
268
282
|
homepage: https://github.com/mrkamel/search_flip
|
269
283
|
licenses:
|
@@ -300,9 +314,9 @@ test_files:
|
|
300
314
|
- spec/search_flip/criteria_spec.rb
|
301
315
|
- spec/search_flip/http_client_spec.rb
|
302
316
|
- spec/search_flip/index_spec.rb
|
317
|
+
- spec/search_flip/json_spec.rb
|
303
318
|
- spec/search_flip/model_spec.rb
|
304
319
|
- spec/search_flip/null_instrumenter_spec.rb
|
305
320
|
- spec/search_flip/response_spec.rb
|
306
321
|
- spec/search_flip/result_spec.rb
|
307
|
-
- spec/search_flip/to_json_spec.rb
|
308
322
|
- spec/spec_helper.rb
|
@@ -1,28 +0,0 @@
|
|
1
|
-
require File.expand_path("../spec_helper", __dir__)
|
2
|
-
require "search_flip/to_json"
|
3
|
-
|
4
|
-
RSpec.describe "to_json" do
|
5
|
-
it "uses the correct format for Time" do
|
6
|
-
Timecop.freeze Time.parse("2018-01-01 12:00:00 UTC") do
|
7
|
-
expect(Time.now.utc.to_json).to eq("\"2018-01-01T12:00:00.000000Z\"")
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
it "uses the correct format for Date" do
|
12
|
-
Timecop.freeze Time.parse("2018-01-01 12:00:00 UTC") do
|
13
|
-
expect(Date.today.to_json).to eq("\"2018-01-01\"")
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
it "uses the correct format for DateTime" do
|
18
|
-
Timecop.freeze Time.parse("2018-01-01 12:00:00 UTC") do
|
19
|
-
expect(Time.now.utc.to_json).to eq("\"2018-01-01T12:00:00.000000Z\"")
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
it "uses the correct format for TimeWithZone" do
|
24
|
-
Timecop.freeze Time.parse("2018-01-01 12:00:00 UTC") do
|
25
|
-
expect(Time.find_zone("UTC").now.to_json).to eq("\"2018-01-01T12:00:00.000000Z\"")
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|