meilisearch 0.11.1 → 0.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/meilisearch/client.rb +9 -14
- data/lib/meilisearch/error.rb +45 -30
- data/lib/meilisearch/http_request.rb +45 -37
- data/lib/meilisearch/index.rb +3 -22
- data/lib/meilisearch/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1861bef27e914a71fc706d4b0758a585842cc33421fa8a7ecacb0a45f146903
|
4
|
+
data.tar.gz: 33b66d0151cc205fd7d964b78608cd5f6641f2d9740d3b28d3fd2f52ee36d98c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7600f98e465ddf28304b52f5ee12c199d7f6486b2230b2e1bea3d988bc41b3936875fbbf6f5a03ee5a6ac416d40e5dcddd8abc906415b6be44f67ba52c79a56d
|
7
|
+
data.tar.gz: d3e8eecfdd30d26a7bbb2dac5c7e216406093782015d232765315897a24744c02e2fac8113c169e4dfc7f5bd2cbe2cdbd3042c5fcb1aaab3e1d5212626b485ee
|
data/lib/meilisearch/client.rb
CHANGED
@@ -23,6 +23,15 @@ module MeiliSearch
|
|
23
23
|
index_object(res['uid'])
|
24
24
|
end
|
25
25
|
|
26
|
+
def get_or_create_index(index_uid, options = {})
|
27
|
+
begin
|
28
|
+
create_index(index_uid, options)
|
29
|
+
rescue ApiError => e
|
30
|
+
raise e unless e.code == 'index_already_exists'
|
31
|
+
end
|
32
|
+
index_object(index_uid)
|
33
|
+
end
|
34
|
+
|
26
35
|
def delete_index(index_uid)
|
27
36
|
index_object(index_uid).delete
|
28
37
|
end
|
@@ -30,8 +39,6 @@ module MeiliSearch
|
|
30
39
|
# Usage:
|
31
40
|
# client.index('indexUID')
|
32
41
|
def index(index_uid)
|
33
|
-
raise IndexUidError if index_uid.nil?
|
34
|
-
|
35
42
|
index_object(index_uid)
|
36
43
|
end
|
37
44
|
alias get_index index
|
@@ -56,24 +63,12 @@ module MeiliSearch
|
|
56
63
|
http_get '/health'
|
57
64
|
end
|
58
65
|
|
59
|
-
def update_health(bool)
|
60
|
-
http_put '/health', health: bool
|
61
|
-
end
|
62
|
-
|
63
66
|
### STATS
|
64
67
|
|
65
68
|
def version
|
66
69
|
http_get '/version'
|
67
70
|
end
|
68
71
|
|
69
|
-
def sysinfo
|
70
|
-
http_get '/sys-info'
|
71
|
-
end
|
72
|
-
|
73
|
-
def pretty_sysinfo
|
74
|
-
http_get '/sys-info/pretty'
|
75
|
-
end
|
76
|
-
|
77
72
|
def stats
|
78
73
|
http_get '/stats'
|
79
74
|
end
|
data/lib/meilisearch/error.rb
CHANGED
@@ -1,45 +1,60 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module MeiliSearch
|
4
|
-
class
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
class ApiError < StandardError
|
5
|
+
# :http_code # e.g. 400, 404...
|
6
|
+
# :http_message # e.g. Bad Request, Not Found...
|
7
|
+
# :http_body # The response body received from the MeiliSearch API
|
8
|
+
# :ms_code # The error code given by the MeiliSearch API
|
9
|
+
# :ms_type # The error type given by the MeiliSearch API
|
10
|
+
# :ms_link # The documentation link given by the MeiliSearch API
|
11
|
+
# :ms_message # The error message given by the MeiliSearch API
|
12
|
+
# :message # The detailed error message of this error class
|
8
13
|
|
9
|
-
|
10
|
-
|
14
|
+
attr_reader :http_code, :http_message, :http_body, :ms_code, :ms_type, :ms_link, :ms_message, :message
|
15
|
+
|
16
|
+
alias code ms_code
|
17
|
+
alias type ms_type
|
18
|
+
alias link ms_link
|
19
|
+
|
20
|
+
def initialize(http_code, http_message, http_body)
|
21
|
+
get_meilisearch_error_info(http_body) unless http_body.nil? || http_body.empty?
|
22
|
+
@http_code = http_code
|
23
|
+
@http_message = http_message
|
24
|
+
@ms_message ||= 'MeiliSearch API has not returned any error message'
|
25
|
+
@ms_link ||= '<no documentation link found>'
|
26
|
+
@message = "#{http_code} #{http_message} - #{@ms_message}. See #{ms_link}."
|
27
|
+
super(details)
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_meilisearch_error_info(http_body)
|
31
|
+
@http_body = JSON.parse(http_body)
|
32
|
+
@ms_code = @http_body['errorCode']
|
33
|
+
@ms_message = @http_body['message']
|
34
|
+
@ms_type = @http_body['errorType']
|
35
|
+
@ms_link = @http_body['errorLink']
|
11
36
|
end
|
12
37
|
|
13
|
-
def
|
14
|
-
"#{@message}
|
38
|
+
def details
|
39
|
+
"MeiliSearch::ApiError - code: #{@ms_code} - type: #{ms_type} - message: #{@ms_message} - link: #{ms_link}"
|
15
40
|
end
|
16
41
|
end
|
17
42
|
|
18
|
-
class
|
19
|
-
attr_reader :status
|
43
|
+
class CommunicationError < StandardError
|
20
44
|
attr_reader :message
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
alias code status
|
26
|
-
alias body http_body
|
27
|
-
alias body_message http_body_message
|
28
|
-
|
29
|
-
def initialize(status, message, http_body, details = nil)
|
30
|
-
@status = status
|
31
|
-
unless http_body.nil? || http_body.empty?
|
32
|
-
@http_body = JSON.parse(http_body)
|
33
|
-
@http_body_message = @http_body['message']
|
34
|
-
end
|
35
|
-
@message = message.capitalize
|
36
|
-
@message = "#{@message} - #{@http_body_message.capitalize}" unless @http_body_message.nil?
|
37
|
-
@details = details
|
45
|
+
|
46
|
+
def initialize(message)
|
47
|
+
@message = "An error occurred while trying to connect to the MeiliSearch instance: #{message}"
|
48
|
+
super(@message)
|
38
49
|
end
|
50
|
+
end
|
39
51
|
|
40
|
-
|
41
|
-
|
42
|
-
|
52
|
+
class TimeoutError < StandardError
|
53
|
+
attr_reader :message
|
54
|
+
|
55
|
+
def initialize
|
56
|
+
@message = 'The update was not processed in the expected time'
|
57
|
+
super(@message)
|
43
58
|
end
|
44
59
|
end
|
45
60
|
end
|
@@ -7,66 +7,74 @@ module MeiliSearch
|
|
7
7
|
class HTTPRequest
|
8
8
|
include HTTParty
|
9
9
|
|
10
|
-
def initialize(url, api_key = nil)
|
10
|
+
def initialize(url, api_key = nil, options = {})
|
11
11
|
@base_url = url
|
12
12
|
@api_key = api_key
|
13
|
+
@options = options
|
13
14
|
@headers = {
|
14
15
|
'Content-Type' => 'application/json',
|
15
16
|
'X-Meili-API-Key' => api_key
|
16
17
|
}.compact
|
17
18
|
end
|
18
19
|
|
19
|
-
def http_get(
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
timeout: 1
|
20
|
+
def http_get(relative_path = '', query_params = {})
|
21
|
+
send_request(
|
22
|
+
proc { |path, config| self.class.get(path, config) },
|
23
|
+
relative_path,
|
24
|
+
query_params
|
25
25
|
)
|
26
|
-
validate(response)
|
27
26
|
end
|
28
27
|
|
29
|
-
def http_post(
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
query: query_params,
|
36
|
-
headers: @headers,
|
37
|
-
timeout: 1
|
38
|
-
}.compact
|
28
|
+
def http_post(relative_path = '', body = nil, query_params = nil)
|
29
|
+
send_request(
|
30
|
+
proc { |path, config| self.class.post(path, config) },
|
31
|
+
relative_path,
|
32
|
+
query_params,
|
33
|
+
body
|
39
34
|
)
|
40
|
-
validate(response)
|
41
35
|
end
|
42
36
|
|
43
|
-
def http_put(
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
query: query_params,
|
50
|
-
headers: @headers,
|
51
|
-
timeout: 1
|
52
|
-
}.compact
|
37
|
+
def http_put(relative_path = '', body = nil, query_params = nil)
|
38
|
+
send_request(
|
39
|
+
proc { |path, config| self.class.put(path, config) },
|
40
|
+
relative_path,
|
41
|
+
query_params,
|
42
|
+
body
|
53
43
|
)
|
54
|
-
validate(response)
|
55
44
|
end
|
56
45
|
|
57
|
-
def http_delete(
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
timeout: 1
|
46
|
+
def http_delete(relative_path = '')
|
47
|
+
send_request(
|
48
|
+
proc { |path, config| self.class.delete(path, config) },
|
49
|
+
relative_path
|
62
50
|
)
|
63
|
-
validate(response)
|
64
51
|
end
|
65
52
|
|
66
53
|
private
|
67
54
|
|
55
|
+
def send_request(http_method, relative_path, query_params = nil, body = nil)
|
56
|
+
config = http_config(query_params, body)
|
57
|
+
begin
|
58
|
+
response = http_method.call(@base_url + relative_path, config)
|
59
|
+
rescue Errno::ECONNREFUSED => e
|
60
|
+
raise CommunicationError, e.message
|
61
|
+
end
|
62
|
+
validate(response)
|
63
|
+
end
|
64
|
+
|
65
|
+
def http_config(query_params, body)
|
66
|
+
body = body.to_json unless body.nil?
|
67
|
+
{
|
68
|
+
headers: @headers,
|
69
|
+
query: query_params,
|
70
|
+
body: body,
|
71
|
+
timeout: @options[:timeout] || 1,
|
72
|
+
max_retries: @options[:max_retries] || 0
|
73
|
+
}.compact
|
74
|
+
end
|
75
|
+
|
68
76
|
def validate(response)
|
69
|
-
raise
|
77
|
+
raise ApiError.new(response.code, response.message, response.body) unless response.success?
|
70
78
|
|
71
79
|
response.parsed_response
|
72
80
|
end
|
data/lib/meilisearch/index.rb
CHANGED
@@ -81,16 +81,8 @@ module MeiliSearch
|
|
81
81
|
### SEARCH
|
82
82
|
|
83
83
|
def search(query, options = {})
|
84
|
-
parsed_options = options.
|
85
|
-
|
86
|
-
[k, v.inspect]
|
87
|
-
elsif v.is_a?(Array)
|
88
|
-
[k, v.join(',')]
|
89
|
-
else
|
90
|
-
[k, v]
|
91
|
-
end
|
92
|
-
end.to_h
|
93
|
-
http_get "/indexes/#{@uid}/search", { q: query }.merge(parsed_options)
|
84
|
+
parsed_options = options.compact
|
85
|
+
http_post "/indexes/#{@uid}/search", { q: query }.merge(parsed_options)
|
94
86
|
end
|
95
87
|
|
96
88
|
### UPDATES
|
@@ -113,7 +105,7 @@ module MeiliSearch
|
|
113
105
|
end
|
114
106
|
end
|
115
107
|
rescue Timeout::Error
|
116
|
-
raise MeiliSearch::
|
108
|
+
raise MeiliSearch::TimeoutError
|
117
109
|
end
|
118
110
|
|
119
111
|
### STATS
|
@@ -244,17 +236,6 @@ module MeiliSearch
|
|
244
236
|
http_delete "/indexes/#{@uid}/settings/displayed-attributes"
|
245
237
|
end
|
246
238
|
|
247
|
-
### SETTINGS - ACCEPT NEW FIELS VALUE
|
248
|
-
|
249
|
-
def accept_new_fields
|
250
|
-
http_get "/indexes/#{@uid}/settings/accept-new-fields"
|
251
|
-
end
|
252
|
-
alias get_accept_new_fields accept_new_fields
|
253
|
-
|
254
|
-
def update_accept_new_fields(accept_new_fields)
|
255
|
-
http_post "/indexes/#{@uid}/settings/accept-new-fields", accept_new_fields
|
256
|
-
end
|
257
|
-
|
258
239
|
### SETTINGS - ATTRIBUTES FOR FACETING
|
259
240
|
|
260
241
|
def attributes_for_faceting
|
data/lib/meilisearch/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: meilisearch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.14.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Meili
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-11-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -61,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
61
|
- !ruby/object:Gem::Version
|
62
62
|
version: '0'
|
63
63
|
requirements: []
|
64
|
-
rubygems_version: 3.
|
64
|
+
rubygems_version: 3.1.4
|
65
65
|
signing_key:
|
66
66
|
specification_version: 4
|
67
67
|
summary: An easy-to-use ruby client for Meilisearch API
|