Skip to content

Commit 523d4b6

Browse files
authored
Update httprb (#21)
1 parent 4900131 commit 523d4b6

File tree

16 files changed

+134
-122
lines changed

16 files changed

+134
-122
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
- uses: actions/setup-ruby@v1
2929
with:
3030
ruby-version: ${{ matrix.ruby }}
31+
- run: gem install bundler
3132
- run: bundle
3233
- run: sleep 10
3334
- run: bundle exec rspec

.rubocop.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
AllCops:
22
NewCops: enable
3-
TargetRubyVersion: 2.4
3+
TargetRubyVersion: 2.5
4+
SuggestExtensions: false
45

56
Layout/EmptyLineBetweenDefs:
67
EmptyLineBetweenClassDefs: false

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11

22
# CHANGELOG
33

4+
## v3.3.0
5+
6+
* Update httprb
7+
* Changed oj default options
8+
* Allow to set oj json options
9+
410
## v3.2.1
511

612
* Fix `refresh` having a empty body breaking in elasticsearch 7.11

README.md

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
**Full-Featured Elasticsearch Ruby Client with a Chainable DSL**
55

6-
[![Build Status](https://secure.travis-ci.org/mrkamel/search_flip.svg?branch=master)](http://travis-ci.org/mrkamel/search_flip)
6+
[![Build](https://github.com/mrkamel/search_flip/workflows/test/badge.svg)](https://github.com/mrkamel/search_flip/actions?query=workflow%3Atest+branch%3Amaster)
77
[![Gem Version](https://badge.fury.io/rb/search_flip.svg)](http://badge.fury.io/rb/search_flip)
88

99
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
5151

5252
```
5353

54-
Finally, SearchFlip comes with a minimal set of dependencies (http-rb, hashie
55-
and oj only).
54+
Finally, SearchFlip comes with a minimal set of dependencies.
5655

5756
## Reference Docs
5857

@@ -883,52 +882,46 @@ Thus, if your ORM supports `.find_each`, `#id` and `#where` you are already
883882
good to go. Otherwise, simply add your custom implementation of those methods
884883
that work with whatever ORM you use.
885884

886-
## Date and Timestamps in JSON
885+
## JSON
887886

888-
Elasticsearch requires dates and timestamps to have one of the formats listed
889-
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).
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:
892889

893890
```ruby
894-
JSON.generate(time: Time.now.utc)
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)
896892
```
897893

898-
This format is not compatible with Elasticsearch by default. If you're on
899-
Rails, ActiveSupport adds its own `#to_json` methods to `Time`, `Date`, etc.
900-
However, ActiveSupport checks whether they are used in combination with
901-
`JSON.generate` or not and adapt:
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:
902898

903899
```ruby
904-
Time.now.utc.to_json
905-
=> "\"2018-02-22T18:18:22.088Z\""
906-
907-
JSON.generate(time: Time.now.utc)
908-
=> "{\"time\":\"2018-02-22 18:18:59 UTC\"}"
900+
SearchFlip::Config[:json_options] = {
901+
mode: :custom,
902+
use_to_json: false,
903+
time_format: :xmlschema,
904+
bigdecimal_as_decimal: false
905+
}
909906
```
910907

911-
SearchFlip is using the [Oj gem](https://github.com/ohler55/oj) to generate
912-
JSON. More concretely, SearchFlip is using:
908+
However, you then have to convert timestamps manually for indexation via e.g.:
913909

914910
```ruby
915-
Oj.dump({ key: "value" }, mode: :custom, use_to_json: true)
916-
```
911+
class MyIndex
912+
# ...
917913

918-
This mitigates the issues if you're on Rails:
914+
def self.serialize(model)
915+
{
916+
# ...
919917

920-
```ruby
921-
Oj.dump(Time.now, mode: :custom, use_to_json: true)
922-
# => "\"2018-02-22T18:21:21.064Z\""
918+
created_at: model.created_at.to_time
919+
}
920+
end
921+
end
923922
```
924923

925-
However, if you're not on Rails, you need to add `#to_json` methods to `Time`,
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.
932925

933926
## Feature Support
934927

lib/search_flip.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "http"
44
require "hashie"
55
require "thread"
6+
require "json"
67
require "oj"
78
require "set"
89

lib/search_flip/bulk.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def upload
143143

144144
return if options[:raise] == false
145145

146-
parsed_response = response.parse
146+
parsed_response = SearchFlip::JSON.parse(response.to_s)
147147

148148
return unless parsed_response["errors"]
149149

lib/search_flip/config.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ module SearchFlip
55
bulk_limit: 1_000,
66
bulk_max_mb: 100,
77
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+
}
915
}
1016
end

lib/search_flip/connection.rb

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ def initialize(options = {})
2828

2929
def version
3030
@version_mutex.synchronize do
31-
@version ||= http_client.headers(accept: "application/json").get("#{base_url}/").parse["version"]["number"]
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
3236
end
3337
end
3438

@@ -40,7 +44,9 @@ def version
4044
# @return [Hash] The raw response
4145

4246
def cluster_health
43-
http_client.headers(accept: "application/json").get("#{base_url}/_cluster/health").parse
47+
response = http_client.headers(accept: "application/json").get("#{base_url}/_cluster/health")
48+
49+
SearchFlip::JSON.parse(response.to_s)
4450
end
4551

4652
# Uses the Elasticsearch Multi Search API to execute multiple search requests
@@ -71,7 +77,7 @@ def msearch(criterias)
7177
.headers(accept: "application/json", content_type: "application/x-ndjson")
7278
.post("#{base_url}/_msearch", body: payload)
7379

74-
raw_response.parse["responses"].map.with_index do |response, index|
80+
SearchFlip::JSON.parse(raw_response.to_s)["responses"].map.with_index do |response, index|
7581
SearchFlip::Response.new(criterias[index], response)
7682
end
7783
end
@@ -90,10 +96,11 @@ def msearch(criterias)
9096
# @return [Hash] The raw response
9197

9298
def update_aliases(payload)
93-
http_client
99+
response = http_client
94100
.headers(accept: "application/json", content_type: "application/json")
95101
.post("#{base_url}/_aliases", body: SearchFlip::JSON.generate(payload))
96-
.parse
102+
103+
SearchFlip::JSON.parse(response.to_s)
97104
end
98105

99106
# Sends an analyze request to Elasticsearch. Raises
@@ -105,10 +112,11 @@ def update_aliases(payload)
105112
# @return [Hash] The raw response
106113

107114
def analyze(request, params = {})
108-
http_client
115+
response = http_client
109116
.headers(accept: "application/json")
110117
.post("#{base_url}/_analyze", json: request, params: params)
111-
.parse
118+
119+
SearchFlip::JSON.parse(response.to_s)
112120
end
113121

114122
# Fetches information about the specified index aliases. Raises
@@ -124,10 +132,11 @@ def analyze(request, params = {})
124132
# @return [Hash] The raw response
125133

126134
def get_aliases(index_name: "*", alias_name: "*")
127-
http_client
135+
response = http_client
128136
.headers(accept: "application/json", content_type: "application/json")
129137
.get("#{base_url}/#{index_name}/_alias/#{alias_name}")
130-
.parse
138+
139+
SearchFlip::JSON.parse(response.to_s)
131140
end
132141

133142
# Returns whether or not the associated Elasticsearch alias already
@@ -159,10 +168,11 @@ def alias_exists?(alias_name)
159168
# @return [Array] The raw response
160169

161170
def get_indices(name = "*", params: {})
162-
http_client
171+
response = http_client
163172
.headers(accept: "application/json", content_type: "application/json")
164173
.get("#{base_url}/_cat/indices/#{name}", params: params)
165-
.parse
174+
175+
SearchFlip::JSON.parse(response.to_s)
166176
end
167177

168178
alias_method :cat_indices, :get_indices
@@ -259,10 +269,11 @@ def update_index_settings(index_name, index_settings)
259269
# @return [Hash] The index settings
260270

261271
def get_index_settings(index_name)
262-
http_client
272+
response = http_client
263273
.headers(accept: "application/json")
264274
.get("#{index_url(index_name)}/_settings")
265-
.parse
275+
276+
SearchFlip::JSON.parse(response.to_s)
266277
end
267278

268279
# Sends a refresh request to Elasticsearch. Raises
@@ -310,7 +321,9 @@ def get_mapping(index_name, type_name: nil)
310321
url = type_name ? type_url(index_name, type_name) : index_url(index_name)
311322
params = type_name && version.to_f >= 6.7 ? { include_type_name: true } : {}
312323

313-
http_client.headers(accept: "application/json").get("#{url}/_mapping", params: params).parse
324+
response = http_client.headers(accept: "application/json").get("#{url}/_mapping", params: params)
325+
326+
SearchFlip::JSON.parse(response.to_s)
314327
end
315328

316329
# Deletes the specified index from Elasticsearch. Raises

lib/search_flip/criteria.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ def execute!
608608
http_request.post("#{target.type_url}/_search", params: request_params, json: request)
609609
end
610610

611-
SearchFlip::Response.new(self, http_response.parse)
611+
SearchFlip::Response.new(self, SearchFlip::JSON.parse(http_response.to_s))
612612
rescue SearchFlip::ConnectionError, SearchFlip::ResponseError => e
613613
raise e unless failsafe_value
614614

lib/search_flip/index.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,9 @@ def include_type_name?
455455
# @return [Hash] The specified document
456456

457457
def get(id, params = {})
458-
connection.http_client.headers(accept: "application/json").get("#{type_url}/#{id}", params: params).parse
458+
response = connection.http_client.headers(accept: "application/json").get("#{type_url}/#{id}", params: params)
459+
460+
SearchFlip::JSON.parse(response.to_s)
459461
end
460462

461463
# Retrieves the documents specified by ids from elasticsearch.
@@ -471,7 +473,9 @@ def get(id, params = {})
471473
# @return [Hash] The raw response
472474

473475
def mget(request, params = {})
474-
connection.http_client.headers(accept: "application/json").post("#{type_url}/_mget", json: request, params: params).parse
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)
475479
end
476480

477481
# Sends an analyze request to Elasticsearch. Raises
@@ -483,7 +487,9 @@ def mget(request, params = {})
483487
# @return [Hash] The raw response
484488

485489
def analyze(request, params = {})
486-
connection.http_client.headers(accept: "application/json").post("#{index_url}/_analyze", json: request, params: params).parse
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)
487493
end
488494

489495
# Sends a index refresh request to Elasticsearch. Raises

0 commit comments

Comments
 (0)