search_flip 3.5.0 → 4.0.0.beta
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 +2 -3
- data/.rubocop.yml +4 -5
- data/CHANGELOG.md +6 -22
- data/README.md +35 -49
- data/UPDATING.md +40 -0
- data/lib/search_flip/bulk.rb +1 -1
- data/lib/search_flip/config.rb +1 -7
- data/lib/search_flip/connection.rb +14 -27
- data/lib/search_flip/criteria.rb +7 -28
- data/lib/search_flip/filterable.rb +0 -16
- data/lib/search_flip/http_client.rb +5 -30
- data/lib/search_flip/index.rb +5 -7
- data/lib/search_flip/json.rb +3 -3
- data/lib/search_flip/response.rb +6 -7
- data/lib/search_flip/result.rb +45 -19
- data/lib/search_flip/to_json.rb +29 -1
- data/lib/search_flip/version.rb +1 -1
- data/lib/search_flip.rb +3 -10
- data/search_flip.gemspec +0 -2
- data/spec/search_flip/aggregation_spec.rb +17 -17
- data/spec/search_flip/criteria_spec.rb +7 -42
- data/spec/search_flip/http_client_spec.rb +3 -7
- data/spec/search_flip/index_spec.rb +5 -18
- data/spec/search_flip/json_spec.rb +4 -18
- data/spec/search_flip/null_instrumenter_spec.rb +2 -2
- data/spec/search_flip/result_spec.rb +23 -6
- data/spec/search_flip/to_json_spec.rb +28 -0
- metadata +7 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b023bc6411647eaecfcb8f5b0fe535379974a0824d49699dd74ef867a67c69eb
|
4
|
+
data.tar.gz: 5dc4a0439f9442811f3966e86b0c6b09d09c54450a04234c3738ae0ab7935900
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5fc637e8c016531df22af678adcae60a4f4169e5f1d2215d52be0a0dff1111b849cb4d79ca36d6cbf315c8516bc51d543852ab5026339d59cb1f5d08ba356f7
|
7
|
+
data.tar.gz: 7cf9d55a9aca5d6d63401750d40bc656fcc345101d8ce92a300c24649e38810d399d43ff225611bbdd91a257d6cb15eee26a6c811974b987e92e72e67eded704
|
data/.github/workflows/test.yml
CHANGED
@@ -11,11 +11,11 @@ jobs:
|
|
11
11
|
- elasticsearch:5.4
|
12
12
|
- docker.elastic.co/elasticsearch/elasticsearch:6.7.0
|
13
13
|
- docker.elastic.co/elasticsearch/elasticsearch:7.0.0
|
14
|
-
- docker.elastic.co/elasticsearch/elasticsearch:7.
|
14
|
+
- docker.elastic.co/elasticsearch/elasticsearch:7.9.0
|
15
15
|
ruby:
|
16
|
+
- 2.5
|
16
17
|
- 2.6
|
17
18
|
- 2.7
|
18
|
-
- 3.0
|
19
19
|
services:
|
20
20
|
elasticsearch:
|
21
21
|
image: ${{ matrix.elasticsearch }}
|
@@ -28,7 +28,6 @@ jobs:
|
|
28
28
|
- uses: actions/setup-ruby@v1
|
29
29
|
with:
|
30
30
|
ruby-version: ${{ matrix.ruby }}
|
31
|
-
- run: gem install bundler
|
32
31
|
- run: bundle
|
33
32
|
- run: sleep 10
|
34
33
|
- run: bundle exec rspec
|
data/.rubocop.yml
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
AllCops:
|
2
2
|
NewCops: enable
|
3
|
-
TargetRubyVersion: 2.
|
4
|
-
SuggestExtensions: false
|
5
|
-
|
6
|
-
Layout/EmptyLineBetweenDefs:
|
7
|
-
EmptyLineBetweenClassDefs: false
|
3
|
+
TargetRubyVersion: 2.4
|
8
4
|
|
9
5
|
Gemspec/RequiredRubyVersion:
|
10
6
|
Enabled: false
|
11
7
|
|
8
|
+
Style/CaseLikeIf:
|
9
|
+
Enabled: false
|
10
|
+
|
12
11
|
Style/ExplicitBlockArgument:
|
13
12
|
Enabled: false
|
14
13
|
|
data/CHANGELOG.md
CHANGED
@@ -1,29 +1,13 @@
|
|
1
1
|
|
2
2
|
# CHANGELOG
|
3
3
|
|
4
|
-
##
|
4
|
+
## v4.0.0
|
5
5
|
|
6
|
-
*
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
* Expose `Http#timeout` via `SearchFlip::HTTPClient`
|
12
|
-
|
13
|
-
## v3.3.0
|
14
|
-
|
15
|
-
* Update httprb
|
16
|
-
* Changed oj default options
|
17
|
-
* Allow to set oj json options
|
18
|
-
|
19
|
-
## v3.2.1
|
20
|
-
|
21
|
-
* Fix `refresh` having a empty body breaking in elasticsearch 7.11
|
22
|
-
|
23
|
-
## v3.2.0
|
24
|
-
|
25
|
-
* Fix `index_scope` not being passed in `each_record` without block
|
26
|
-
* Added `SearchFlip::Criteria#match_none`
|
6
|
+
* [BREAKING] For performance reasons, `SearchFlip::Result` now no longer
|
7
|
+
inherits `Hashie::Mash`
|
8
|
+
* It no longer supports symbol based access like `result[:id]`
|
9
|
+
* It no longer supports question mark methods like `result.title?`
|
10
|
+
* It no longer supports method based assignment like `result.some_key = "value"`
|
27
11
|
|
28
12
|
## v3.1.2
|
29
13
|
|
data/README.md
CHANGED
@@ -51,7 +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.
|
54
|
+
Finally, SearchFlip comes with a minimal set of dependencies (http-rb and oj only).
|
55
55
|
|
56
56
|
## Reference Docs
|
57
57
|
|
@@ -418,14 +418,6 @@ Simply matches all documents:
|
|
418
418
|
CommentIndex.match_all
|
419
419
|
```
|
420
420
|
|
421
|
-
* `match_none`
|
422
|
-
|
423
|
-
Simply matches none documents at all:
|
424
|
-
|
425
|
-
```ruby
|
426
|
-
CommentIndex.match_none
|
427
|
-
```
|
428
|
-
|
429
421
|
* `all`
|
430
422
|
|
431
423
|
Simply returns the criteria as is or an empty criteria when called on the index
|
@@ -482,8 +474,8 @@ end
|
|
482
474
|
```
|
483
475
|
|
484
476
|
Generally, aggregation results returned by Elasticsearch are returned as a
|
485
|
-
`SearchFlip::Result`, which basically is a
|
486
|
-
access them via:
|
477
|
+
`SearchFlip::Result`, which basically is a Hash with method-like access, such
|
478
|
+
that you can access them via:
|
487
479
|
|
488
480
|
```ruby
|
489
481
|
query.aggregations(:username)["mrkamel"].revenue.value
|
@@ -698,14 +690,6 @@ Specify a timeout to limit query processing time:
|
|
698
690
|
CommentIndex.timeout("3s").execute
|
699
691
|
```
|
700
692
|
|
701
|
-
* `http_timeout`
|
702
|
-
|
703
|
-
Specify a http timeout for the request which will be send to Elasticsearch:
|
704
|
-
|
705
|
-
```ruby
|
706
|
-
CommentIndex.http_timeout(3).execute
|
707
|
-
```
|
708
|
-
|
709
693
|
* `terminate_after`
|
710
694
|
|
711
695
|
Activate early query termination to stop query processing after the specified
|
@@ -764,7 +748,7 @@ end
|
|
764
748
|
This allows to use different clusters per index e.g. when migrating indices to
|
765
749
|
new versions of Elasticsearch.
|
766
750
|
|
767
|
-
You can specify basic auth, additional headers,
|
751
|
+
You can specify basic auth, additional headers, etc via:
|
768
752
|
|
769
753
|
```ruby
|
770
754
|
http_client = SearchFlip::HTTPClient.new
|
@@ -781,9 +765,6 @@ http_client = http_client.via("proxy.host", 8080)
|
|
781
765
|
# Custom headers
|
782
766
|
http_client = http_client.headers(key: "value")
|
783
767
|
|
784
|
-
# Timeouts
|
785
|
-
http_client = http_client.timeout(20)
|
786
|
-
|
787
768
|
SearchFlip::Connection.new(base_url: "...", http_client: http_client)
|
788
769
|
```
|
789
770
|
|
@@ -893,52 +874,57 @@ Thus, if your ORM supports `.find_each`, `#id` and `#where` you are already
|
|
893
874
|
good to go. Otherwise, simply add your custom implementation of those methods
|
894
875
|
that work with whatever ORM you use.
|
895
876
|
|
896
|
-
## JSON
|
877
|
+
## Date and Timestamps in JSON
|
897
878
|
|
898
|
-
|
899
|
-
|
879
|
+
Elasticsearch requires dates and timestamps to have one of the formats listed
|
880
|
+
here: [https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#strict-date-time](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#strict-date-time).
|
881
|
+
|
882
|
+
However, `JSON.generate` in ruby by default outputs something like:
|
900
883
|
|
901
884
|
```ruby
|
902
|
-
|
885
|
+
JSON.generate(time: Time.now.utc)
|
886
|
+
# => "{\"time\":\"2018-02-22 18:19:33 UTC\"}"
|
903
887
|
```
|
904
888
|
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
889
|
+
This format is not compatible with Elasticsearch by default. If you're on
|
890
|
+
Rails, ActiveSupport adds its own `#to_json` methods to `Time`, `Date`, etc.
|
891
|
+
However, ActiveSupport checks whether they are used in combination with
|
892
|
+
`JSON.generate` or not and adapt:
|
909
893
|
|
910
894
|
```ruby
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
}
|
895
|
+
Time.now.utc.to_json
|
896
|
+
=> "\"2018-02-22T18:18:22.088Z\""
|
897
|
+
|
898
|
+
JSON.generate(time: Time.now.utc)
|
899
|
+
=> "{\"time\":\"2018-02-22 18:18:59 UTC\"}"
|
917
900
|
```
|
918
901
|
|
919
|
-
|
902
|
+
SearchFlip is using the [Oj gem](https://github.com/ohler55/oj) to generate
|
903
|
+
JSON. More concretely, SearchFlip is using:
|
920
904
|
|
921
905
|
```ruby
|
922
|
-
|
923
|
-
|
906
|
+
Oj.dump({ key: "value" }, mode: :custom, use_to_json: true)
|
907
|
+
```
|
924
908
|
|
925
|
-
|
926
|
-
{
|
927
|
-
# ...
|
909
|
+
This mitigates the issues if you're on Rails:
|
928
910
|
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
end
|
911
|
+
```ruby
|
912
|
+
Oj.dump(Time.now, mode: :custom, use_to_json: true)
|
913
|
+
# => "\"2018-02-22T18:21:21.064Z\""
|
933
914
|
```
|
934
915
|
|
935
|
-
|
916
|
+
However, if you're not on Rails, you need to add `#to_json` methods to `Time`,
|
917
|
+
`Date` and `DateTime` to get proper serialization. You can either add them on
|
918
|
+
your own, via other libraries or by simply using:
|
919
|
+
|
920
|
+
```ruby
|
921
|
+
require "search_flip/to_json"
|
922
|
+
```
|
936
923
|
|
937
924
|
## Feature Support
|
938
925
|
|
939
926
|
* for Elasticsearch 2.x, the delete-by-query plugin is required to delete
|
940
927
|
records via queries
|
941
|
-
* `#match_none` is only available with Elasticsearch >= 5
|
942
928
|
* `#track_total_hits` is only available with Elasticsearch >= 7
|
943
929
|
|
944
930
|
## Keeping your Models and Indices in Sync
|
data/UPDATING.md
CHANGED
@@ -1,6 +1,46 @@
|
|
1
1
|
|
2
2
|
# Updating from previous SearchFlip versions
|
3
3
|
|
4
|
+
## Update 3.x to 4.x
|
5
|
+
|
6
|
+
**[BREAKING]** For performance reasons, `SearchFlip::Result` no longer
|
7
|
+
inherits `Hashie::Mash`
|
8
|
+
|
9
|
+
* It no longer supports symbol based access like `result[:id]`
|
10
|
+
|
11
|
+
2.x:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
CommentIndex.match_all.results.first[:id]
|
15
|
+
CommentIndex.aggregate(:tags).aggregations(:tags).values.first[:doc_count]
|
16
|
+
```
|
17
|
+
|
18
|
+
3.x
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
CommentIndex.match_all.results.first["id"] # or .id
|
22
|
+
CommentIndex.aggregate(:tags).aggregations(:tags).values.first["doc_count"] # or .doc_count
|
23
|
+
```
|
24
|
+
|
25
|
+
* It no longer supports question mark methods like `result.title?`
|
26
|
+
|
27
|
+
2.x:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
CommentIndex.match_all.results.first.is_published?
|
31
|
+
```
|
32
|
+
|
33
|
+
3.x
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
CommentIndex.match_all.results.first.is_published == true
|
37
|
+
```
|
38
|
+
|
39
|
+
* It no longer supports method based assignment like `result.some_key = "value"`.
|
40
|
+
|
41
|
+
However, this should not have any practical implications, as changing the
|
42
|
+
results is not considered to be a common use case.
|
43
|
+
|
4
44
|
## Update 2.x to 3.x
|
5
45
|
|
6
46
|
* **[BREAKING]** No longer pass multiple arguments to `#must`, `#must_not`,
|
data/lib/search_flip/bulk.rb
CHANGED
data/lib/search_flip/config.rb
CHANGED
@@ -5,12 +5,6 @@ module SearchFlip
|
|
5
5
|
bulk_limit: 1_000,
|
6
6
|
bulk_max_mb: 100,
|
7
7
|
auto_refresh: false,
|
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
|
-
}
|
8
|
+
instrumenter: NullInstrumenter.new
|
15
9
|
}
|
16
10
|
end
|
@@ -28,11 +28,7 @@ module SearchFlip
|
|
28
28
|
|
29
29
|
def version
|
30
30
|
@version_mutex.synchronize do
|
31
|
-
@version ||=
|
32
|
-
response = http_client.headers(accept: "application/json").get("#{base_url}/")
|
33
|
-
|
34
|
-
SearchFlip::JSON.parse(response.to_s)["version"]["number"]
|
35
|
-
end
|
31
|
+
@version ||= http_client.headers(accept: "application/json").get("#{base_url}/").parse["version"]["number"]
|
36
32
|
end
|
37
33
|
end
|
38
34
|
|
@@ -44,9 +40,7 @@ module SearchFlip
|
|
44
40
|
# @return [Hash] The raw response
|
45
41
|
|
46
42
|
def cluster_health
|
47
|
-
|
48
|
-
|
49
|
-
SearchFlip::JSON.parse(response.to_s)
|
43
|
+
http_client.headers(accept: "application/json").get("#{base_url}/_cluster/health").parse
|
50
44
|
end
|
51
45
|
|
52
46
|
# Uses the Elasticsearch Multi Search API to execute multiple search requests
|
@@ -96,11 +90,10 @@ module SearchFlip
|
|
96
90
|
# @return [Hash] The raw response
|
97
91
|
|
98
92
|
def update_aliases(payload)
|
99
|
-
|
93
|
+
http_client
|
100
94
|
.headers(accept: "application/json", content_type: "application/json")
|
101
95
|
.post("#{base_url}/_aliases", body: SearchFlip::JSON.generate(payload))
|
102
|
-
|
103
|
-
SearchFlip::JSON.parse(response.to_s)
|
96
|
+
.parse
|
104
97
|
end
|
105
98
|
|
106
99
|
# Sends an analyze request to Elasticsearch. Raises
|
@@ -112,11 +105,10 @@ module SearchFlip
|
|
112
105
|
# @return [Hash] The raw response
|
113
106
|
|
114
107
|
def analyze(request, params = {})
|
115
|
-
|
108
|
+
http_client
|
116
109
|
.headers(accept: "application/json")
|
117
110
|
.post("#{base_url}/_analyze", json: request, params: params)
|
118
|
-
|
119
|
-
SearchFlip::JSON.parse(response.to_s)
|
111
|
+
.parse
|
120
112
|
end
|
121
113
|
|
122
114
|
# Fetches information about the specified index aliases. Raises
|
@@ -132,11 +124,10 @@ module SearchFlip
|
|
132
124
|
# @return [Hash] The raw response
|
133
125
|
|
134
126
|
def get_aliases(index_name: "*", alias_name: "*")
|
135
|
-
|
127
|
+
http_client
|
136
128
|
.headers(accept: "application/json", content_type: "application/json")
|
137
129
|
.get("#{base_url}/#{index_name}/_alias/#{alias_name}")
|
138
|
-
|
139
|
-
SearchFlip::JSON.parse(response.to_s)
|
130
|
+
.parse
|
140
131
|
end
|
141
132
|
|
142
133
|
# Returns whether or not the associated Elasticsearch alias already
|
@@ -168,11 +159,10 @@ module SearchFlip
|
|
168
159
|
# @return [Array] The raw response
|
169
160
|
|
170
161
|
def get_indices(name = "*", params: {})
|
171
|
-
|
162
|
+
http_client
|
172
163
|
.headers(accept: "application/json", content_type: "application/json")
|
173
164
|
.get("#{base_url}/_cat/indices/#{name}", params: params)
|
174
|
-
|
175
|
-
SearchFlip::JSON.parse(response.to_s)
|
165
|
+
.parse
|
176
166
|
end
|
177
167
|
|
178
168
|
alias_method :cat_indices, :get_indices
|
@@ -269,11 +259,10 @@ module SearchFlip
|
|
269
259
|
# @return [Hash] The index settings
|
270
260
|
|
271
261
|
def get_index_settings(index_name)
|
272
|
-
|
262
|
+
http_client
|
273
263
|
.headers(accept: "application/json")
|
274
264
|
.get("#{index_url(index_name)}/_settings")
|
275
|
-
|
276
|
-
SearchFlip::JSON.parse(response.to_s)
|
265
|
+
.parse
|
277
266
|
end
|
278
267
|
|
279
268
|
# Sends a refresh request to Elasticsearch. Raises
|
@@ -283,7 +272,7 @@ module SearchFlip
|
|
283
272
|
# @return [Boolean] Returns true or raises SearchFlip::ResponseError
|
284
273
|
|
285
274
|
def refresh(index_names = nil)
|
286
|
-
http_client.post("#{index_names ? index_url(Array(index_names).join(",")) : base_url}/_refresh")
|
275
|
+
http_client.post("#{index_names ? index_url(Array(index_names).join(",")) : base_url}/_refresh", json: {})
|
287
276
|
|
288
277
|
true
|
289
278
|
end
|
@@ -321,9 +310,7 @@ module SearchFlip
|
|
321
310
|
url = type_name ? type_url(index_name, type_name) : index_url(index_name)
|
322
311
|
params = type_name && version.to_f >= 6.7 ? { include_type_name: true } : {}
|
323
312
|
|
324
|
-
|
325
|
-
|
326
|
-
SearchFlip::JSON.parse(response.to_s)
|
313
|
+
http_client.headers(accept: "application/json").get("#{url}/_mapping", params: params).parse
|
327
314
|
end
|
328
315
|
|
329
316
|
# Deletes the specified index from Elasticsearch. Raises
|
data/lib/search_flip/criteria.rb
CHANGED
@@ -26,8 +26,7 @@ module SearchFlip
|
|
26
26
|
|
27
27
|
attr_accessor :target, :profile_value, :source_value, :suggest_values, :includes_values,
|
28
28
|
:eager_load_values, :preload_values, :failsafe_value, :scroll_args, :terminate_after_value,
|
29
|
-
:timeout_value, :preference_value, :search_type_value, :routing_value, :track_total_hits_value
|
30
|
-
:http_timeout_value
|
29
|
+
:timeout_value, :preference_value, :search_type_value, :routing_value, :track_total_hits_value
|
31
30
|
|
32
31
|
# Creates a new criteria while merging the attributes (constraints,
|
33
32
|
# settings, etc) of the current criteria with the attributes of another one
|
@@ -48,7 +47,7 @@ module SearchFlip
|
|
48
47
|
[
|
49
48
|
:profile_value, :failsafe_value, :terminate_after_value, :timeout_value, :offset_value,
|
50
49
|
:limit_value, :scroll_args, :source_value, :preference_value, :search_type_value,
|
51
|
-
:routing_value, :track_total_hits_value, :explain_value
|
50
|
+
:routing_value, :track_total_hits_value, :explain_value
|
52
51
|
].each do |name|
|
53
52
|
criteria.send(:"#{name}=", other.send(name)) unless other.send(name).nil?
|
54
53
|
end
|
@@ -149,22 +148,6 @@ module SearchFlip
|
|
149
148
|
end
|
150
149
|
end
|
151
150
|
|
152
|
-
# Specifies a http timeout, such that a SearchFlip::TimeoutError will be
|
153
|
-
# thrown when the request times out.
|
154
|
-
#
|
155
|
-
# @example
|
156
|
-
# ProductIndex.http_timeout(3).search("hello world")
|
157
|
-
#
|
158
|
-
# @param value [Fixnum] The timeout value
|
159
|
-
#
|
160
|
-
# @return [SearchFlip::Criteria] A newly created extended criteria
|
161
|
-
|
162
|
-
def http_timeout(value)
|
163
|
-
fresh.tap do |criteria|
|
164
|
-
criteria.http_timeout_value = value
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
151
|
# Specifies early query termination, such that the processing will be
|
169
152
|
# stopped after the specified number of results has been accumulated.
|
170
153
|
#
|
@@ -347,13 +330,10 @@ module SearchFlip
|
|
347
330
|
dupped_request.delete(:from)
|
348
331
|
dupped_request.delete(:size)
|
349
332
|
|
350
|
-
http_request = connection.http_client
|
351
|
-
http_request = http_request.timeout(http_timeout_value) if http_timeout_value
|
352
|
-
|
353
333
|
if connection.version.to_i >= 5
|
354
|
-
|
334
|
+
connection.http_client.post("#{target.type_url}/_delete_by_query", params: request_params.merge(params), json: dupped_request)
|
355
335
|
else
|
356
|
-
|
336
|
+
connection.http_client.delete("#{target.type_url}/_query", params: request_params.merge(params), json: dupped_request)
|
357
337
|
end
|
358
338
|
|
359
339
|
target.refresh if SearchFlip::Config[:auto_refresh]
|
@@ -521,8 +501,8 @@ module SearchFlip
|
|
521
501
|
end
|
522
502
|
|
523
503
|
# Executes the search request for the current criteria, ie sends the
|
524
|
-
# request to Elasticsearch and returns the response. Connection
|
525
|
-
#
|
504
|
+
# request to Elasticsearch and returns the response. Connection and
|
505
|
+
# response errors will be rescued if you specify the criteria to be
|
526
506
|
# #failsafe, such that an empty response is returned instead.
|
527
507
|
#
|
528
508
|
# @example
|
@@ -610,7 +590,6 @@ module SearchFlip
|
|
610
590
|
|
611
591
|
def execute!
|
612
592
|
http_request = connection.http_client.headers(accept: "application/json")
|
613
|
-
http_request = http_request.timeout(http_timeout_value) if http_timeout_value
|
614
593
|
|
615
594
|
http_response =
|
616
595
|
if scroll_args && scroll_args[:id]
|
@@ -630,7 +609,7 @@ module SearchFlip
|
|
630
609
|
end
|
631
610
|
|
632
611
|
SearchFlip::Response.new(self, SearchFlip::JSON.parse(http_response.to_s))
|
633
|
-
rescue SearchFlip::ConnectionError, SearchFlip::
|
612
|
+
rescue SearchFlip::ConnectionError, SearchFlip::ResponseError => e
|
634
613
|
raise e unless failsafe_value
|
635
614
|
|
636
615
|
SearchFlip::Response.new(self, "took" => 0, "hits" => { "total" => 0, "hits" => [] })
|
@@ -235,22 +235,6 @@ module SearchFlip
|
|
235
235
|
filter(match_all: options)
|
236
236
|
end
|
237
237
|
|
238
|
-
# Adds a match none filter to the criteria, which simply matches none
|
239
|
-
# documents at all. Check out the Elasticsearch docs for further details.
|
240
|
-
#
|
241
|
-
# @example Basic usage
|
242
|
-
# CommentIndex.match_none
|
243
|
-
#
|
244
|
-
# @example Filter chaining
|
245
|
-
# query = CommentIndex.search("...")
|
246
|
-
# query = query.match_none unless current_user.admin?
|
247
|
-
#
|
248
|
-
# @return [SearchFlip::Criteria] A newly created extended criteria
|
249
|
-
|
250
|
-
def match_none
|
251
|
-
filter(match_none: {})
|
252
|
-
end
|
253
|
-
|
254
238
|
# Adds an exists filter to the criteria, which selects all documents for
|
255
239
|
# which the specified field has a non-null value.
|
256
240
|
#
|
@@ -1,28 +1,7 @@
|
|
1
1
|
module SearchFlip
|
2
|
-
# The SearchFlip::HTTPClient class wraps the http gem
|
3
|
-
# http request/response handling, ie communicating
|
4
|
-
#
|
5
|
-
# Elasticsearch or if you want to set some custom http settings.
|
6
|
-
#
|
7
|
-
# @example
|
8
|
-
# http_client = SearchFlip::HTTPClient.new
|
9
|
-
#
|
10
|
-
# # Basic Auth
|
11
|
-
# http_client = http_client.basic_auth(user: "username", pass: "password")
|
12
|
-
#
|
13
|
-
# # Raw Auth Header
|
14
|
-
# http_client = http_client.auth("Bearer VGhlIEhUVFAgR2VtLCBST0NLUw")
|
15
|
-
#
|
16
|
-
# # Proxy Settings
|
17
|
-
# http_client = http_client.via("proxy.host", 8080)
|
18
|
-
#
|
19
|
-
# # Custom headers
|
20
|
-
# http_client = http_client.headers(key: "value")
|
21
|
-
#
|
22
|
-
# # Timeouts
|
23
|
-
# http_client = http_client.timeout(20)
|
24
|
-
#
|
25
|
-
# SearchFlip::Connection.new(base_url: "...", http_client: http_client)
|
2
|
+
# The SearchFlip::HTTPClient class wraps the http gem, is for internal use
|
3
|
+
# and responsible for the http request/response handling, ie communicating
|
4
|
+
# with Elasticsearch.
|
26
5
|
|
27
6
|
class HTTPClient
|
28
7
|
attr_accessor :request, :plugins
|
@@ -35,11 +14,11 @@ module SearchFlip
|
|
35
14
|
class << self
|
36
15
|
extend Forwardable
|
37
16
|
|
38
|
-
def_delegators :new, :headers, :via, :basic_auth, :auth
|
17
|
+
def_delegators :new, :headers, :via, :basic_auth, :auth
|
39
18
|
def_delegators :new, :get, :post, :put, :delete, :head
|
40
19
|
end
|
41
20
|
|
42
|
-
[:headers, :via, :basic_auth, :auth
|
21
|
+
[:headers, :via, :basic_auth, :auth].each do |method|
|
43
22
|
define_method method do |*args|
|
44
23
|
dup.tap do |client|
|
45
24
|
client.request = request.send(method, *args)
|
@@ -66,10 +45,6 @@ module SearchFlip
|
|
66
45
|
response
|
67
46
|
rescue HTTP::ConnectionError => e
|
68
47
|
raise SearchFlip::ConnectionError, e.message
|
69
|
-
rescue HTTP::TimeoutError => e
|
70
|
-
raise SearchFlip::TimeoutError, e.message
|
71
|
-
rescue HTTP::Error => e
|
72
|
-
raise SearchFlip::HttpError, e.message
|
73
48
|
end
|
74
49
|
end
|
75
50
|
end
|
data/lib/search_flip/index.rb
CHANGED
@@ -153,7 +153,7 @@ module SearchFlip
|
|
153
153
|
# scope to be applied to the scope
|
154
154
|
|
155
155
|
def each_record(scope, index_scope: false)
|
156
|
-
return enum_for(:each_record, scope
|
156
|
+
return enum_for(:each_record, scope) unless block_given?
|
157
157
|
|
158
158
|
if scope.respond_to?(:find_each)
|
159
159
|
(index_scope ? self.index_scope(scope) : scope).find_each do |record|
|
@@ -247,14 +247,14 @@ module SearchFlip
|
|
247
247
|
SearchFlip::Criteria.new(target: self)
|
248
248
|
end
|
249
249
|
|
250
|
-
def_delegators :criteria, :all, :profile, :where, :where_not, :filter, :range, :match_all, :
|
251
|
-
:
|
250
|
+
def_delegators :criteria, :all, :profile, :where, :where_not, :filter, :range, :match_all, :exists,
|
251
|
+
:exists_not, :post_where, :post_where_not, :post_range, :post_exists, :post_exists_not,
|
252
252
|
:post_filter, :post_must, :post_must_not, :post_should, :aggregate, :scroll, :source,
|
253
253
|
:includes, :eager_load, :preload, :sort, :resort, :order, :reorder, :offset, :limit, :paginate,
|
254
254
|
:page, :per, :search, :highlight, :suggest, :custom, :find_in_batches, :find_results_in_batches,
|
255
255
|
:find_each, :find_each_result, :failsafe, :total_entries, :total_count, :timeout, :terminate_after,
|
256
256
|
:records, :results, :must, :must_not, :should, :preference, :search_type, :routing,
|
257
|
-
:track_total_hits, :explain
|
257
|
+
:track_total_hits, :explain
|
258
258
|
|
259
259
|
# Override to specify the type name used within Elasticsearch. Recap,
|
260
260
|
# this gem uses an individual index for each index class, because
|
@@ -487,9 +487,7 @@ module SearchFlip
|
|
487
487
|
# @return [Hash] The raw response
|
488
488
|
|
489
489
|
def analyze(request, params = {})
|
490
|
-
|
491
|
-
|
492
|
-
SearchFlip::JSON.parse(response.to_s)
|
490
|
+
connection.http_client.headers(accept: "application/json").post("#{index_url}/_analyze", json: request, params: params).parse
|
493
491
|
end
|
494
492
|
|
495
493
|
# Sends a index refresh request to Elasticsearch. Raises
|
data/lib/search_flip/json.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
module SearchFlip
|
2
2
|
class JSON
|
3
3
|
def self.generate(obj)
|
4
|
-
Oj.dump(obj,
|
4
|
+
Oj.dump(obj, mode: :custom, use_to_json: true)
|
5
5
|
end
|
6
6
|
|
7
|
-
def self.parse(
|
8
|
-
|
7
|
+
def self.parse(str)
|
8
|
+
Oj.load(str)
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|