meilisearch 0.17.3 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3c90df98b1b08039b339c49b47bfd052639e1301e9e7e3b25989b9ce6e3d1e67
4
- data.tar.gz: 3f200a9de09db7a47780b01aa20a8ce5b2b1d0d0579954cd8842cb208b06b4dc
3
+ metadata.gz: df541b398ac4b031ec615d5e20c6a19cd7c370cdeaf83dcf63d789374827f012
4
+ data.tar.gz: 9277554fe2d6f388491d506cbfda456e71a1cc90b1945d933cfa72a2147ee52c
5
5
  SHA512:
6
- metadata.gz: b3b38312517176303e9fdc49af5c38647ea4d719d8988cb1e1fd60839e7c593226fd9369febd24a326d214dbb606acd201464fd6590f46438383cf25f3e7e934
7
- data.tar.gz: 494fa30322be3d624851117e460c31046c51ea3ef4d00326f4be969a76f5f056a15147c065b1ef02a56f3a589643e137ac4af1a05a088bab606cdf6d31bc8abc
6
+ metadata.gz: bc0446c08444d6bf48d796c62c9b92e5f008835463b08c916e52f0ac9ce81156bcb835cd645fd78c31b98907a06230cf60fe2dbcfdcf0102b9c3fe8f9dfc2732
7
+ data.tar.gz: ac4f2136062699556c365e296db97fe54c6085a228c2b6831b963a72557918c100b83f7e5241183b5d9d173927e127b1e2ad402045dc64ec71056c7b57663fb2
data/README.md CHANGED
@@ -17,7 +17,7 @@
17
17
  <a href="https://badge.fury.io/rb/meilisearch"><img src="https://badge.fury.io/rb/meilisearch.svg" alt="Latest Stable Version"></a>
18
18
  <a href="https://github.com/meilisearch/meilisearch-ruby/actions"><img src="https://github.com/meilisearch/meilisearch-ruby/workflows/Tests/badge.svg" alt="Test"></a>
19
19
  <a href="https://github.com/meilisearch/meilisearch-ruby/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-informational" alt="License"></a>
20
- <a href="https://app.bors.tech/repositories/28781"><img src="https://bors.tech/images/badge_small.svg" alt="Bors enabled"></a>
20
+ <a href="https://ms-bors.herokuapp.com/repositories/6"><img src="https://bors.tech/images/badge_small.svg" alt="Bors enabled"></a>
21
21
  </p>
22
22
 
23
23
  <p align="center">⚡ The MeiliSearch API client written for Ruby 💎</p>
@@ -92,10 +92,10 @@ documents = [
92
92
  { id: 6, title: 'Philadelphia', genres: ['Drama'] },
93
93
  ]
94
94
  # If the index 'movies' does not exist, MeiliSearch creates it when you first add the documents.
95
- index.add_documents(documents) # => { "updateId": 0 }
95
+ index.add_documents(documents) # => { "uid": 0 }
96
96
  ```
97
97
 
98
- With the `updateId`, you can check the status (`enqueued`, `processing`, `processed` or `failed`) of your documents addition using the [update endpoint](https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status).
98
+ With the `uid`, you can check the status (`enqueued`, `processing`, `succeeded` or `failed`) of your documents addition using the [task](https://docs.meilisearch.com/reference/api/tasks.html#get-task).
99
99
 
100
100
  💡 To customize the `Client`, for example, increasing the default timeout, please check out [this section](https://github.com/meilisearch/meilisearch-ruby/wiki/Client-Options) of the Wiki.
101
101
 
@@ -165,7 +165,7 @@ index.update_filterable_attributes([
165
165
 
166
166
  You only need to perform this operation once.
167
167
 
168
- Note that MeiliSearch will rebuild your index whenever you update `filterableAttributes`. Depending on the size of your dataset, this might take time. You can track the process using the [update status](https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status).
168
+ Note that MeiliSearch will rebuild your index whenever you update `filterableAttributes`. Depending on the size of your dataset, this might take time. You can track the process using the [tasks](https://docs.meilisearch.com/reference/api/tasks.html#get-task)).
169
169
 
170
170
  Then, you can perform the search:
171
171
 
@@ -198,7 +198,7 @@ JSON output:
198
198
 
199
199
  ## 🤖 Compatibility with MeiliSearch
200
200
 
201
- This package only guarantees the compatibility with the [version v0.24.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.24.0).
201
+ This package only guarantees the compatibility with the [version v0.25.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.25.0).
202
202
 
203
203
  ## 💡 Learn More
204
204
 
@@ -22,36 +22,20 @@ module MeiliSearch
22
22
  def create_index(index_uid, options = {})
23
23
  body = Utils.transform_attributes(options.merge(uid: index_uid))
24
24
 
25
- index_hash = http_post '/indexes', body
26
- index_object(index_hash['uid'], index_hash['primaryKey'])
25
+ http_post '/indexes', body
27
26
  end
28
27
 
29
- def get_or_create_index(index_uid, options = {})
30
- begin
31
- index_instance = fetch_index(index_uid)
32
- rescue ApiError => e
33
- raise e unless e.code == 'index_not_found'
34
-
35
- index_instance = create_index(index_uid, options)
36
- end
37
- index_instance
28
+ # Synchronous version of create_index.
29
+ # Waits for the task to be achieved, be careful when using it.
30
+ def create_index!(index_uid, options = {})
31
+ task = create_index(index_uid, options)
32
+ wait_for_task(task['uid'])
38
33
  end
39
34
 
40
35
  def delete_index(index_uid)
41
36
  index_object(index_uid).delete
42
37
  end
43
38
 
44
- # Usage:
45
- # client.delete_index_if_exists('indexUID')
46
- def delete_index_if_exists(index_uid)
47
- index_object(index_uid).delete
48
- true
49
- rescue ApiError => e
50
- raise e if e.code != 'index_not_found'
51
-
52
- false
53
- end
54
-
55
39
  # Usage:
56
40
  # client.index('indexUID')
57
41
  def index(index_uid)
@@ -71,7 +55,26 @@ module MeiliSearch
71
55
  def keys
72
56
  http_get '/keys'
73
57
  end
74
- alias get_keys keys
58
+
59
+ def key(key_uid)
60
+ http_get "/keys/#{key_uid}"
61
+ end
62
+
63
+ def create_key(key_options)
64
+ body = Utils.transform_attributes(key_options)
65
+
66
+ http_post '/keys', body
67
+ end
68
+
69
+ def update_key(key_uid, key_options)
70
+ body = Utils.transform_attributes(key_options)
71
+
72
+ http_patch "/keys/#{key_uid}", body
73
+ end
74
+
75
+ def delete_key(key_uid)
76
+ http_delete "/keys/#{key_uid}"
77
+ end
75
78
 
76
79
  ### HEALTH
77
80
 
@@ -107,10 +110,28 @@ module MeiliSearch
107
110
  end
108
111
  alias get_dump_status dump_status
109
112
 
113
+ ### TASKS
114
+
115
+ def tasks
116
+ task_endpoint.task_list
117
+ end
118
+
119
+ def task(task_uid)
120
+ task_endpoint.task(task_uid)
121
+ end
122
+
123
+ def wait_for_task(task_uid, timeout_in_ms = 5000, interval_in_ms = 50)
124
+ task_endpoint.wait_for_task(task_uid, timeout_in_ms, interval_in_ms)
125
+ end
126
+
110
127
  private
111
128
 
112
129
  def index_object(uid, primary_key = nil)
113
130
  Index.new(uid, @base_url, @api_key, primary_key, @options)
114
131
  end
132
+
133
+ def task_endpoint
134
+ @task_endpoint ||= Task.new(@base_url, @api_key, @options)
135
+ end
115
136
  end
116
137
  end
@@ -49,6 +49,16 @@ module MeiliSearch
49
49
  )
50
50
  end
51
51
 
52
+ def http_patch(relative_path = '', body = nil, query_params = nil)
53
+ send_request(
54
+ proc { |path, config| self.class.patch(path, config) },
55
+ relative_path,
56
+ query_params: query_params,
57
+ body: body,
58
+ options: @options
59
+ )
60
+ end
61
+
52
62
  def http_delete(relative_path = '')
53
63
  send_request(
54
64
  proc { |path, config| self.class.delete(path, config) },
@@ -60,10 +70,11 @@ module MeiliSearch
60
70
  private
61
71
 
62
72
  def build_default_options_headers(api_key = nil)
63
- {
64
- 'Content-Type' => 'application/json',
65
- 'X-Meili-API-Key' => api_key
66
- }.compact
73
+ header = {
74
+ 'Content-Type' => 'application/json'
75
+ }
76
+ header = header.merge('Authorization' => "Bearer #{api_key}") unless api_key.nil?
77
+ header
67
78
  end
68
79
 
69
80
  def merge_options(default_options, added_options = {})
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'meilisearch/http_request'
4
- require 'timeout'
5
4
 
6
5
  module MeiliSearch
7
6
  class Index < HTTPRequest
@@ -31,10 +30,7 @@ module MeiliSearch
31
30
  end
32
31
 
33
32
  def update(body)
34
- index_hash = http_put indexes_path(id: @uid), Utils.transform_attributes(body)
35
- set_base_properties index_hash
36
-
37
- self
33
+ http_put indexes_path(id: @uid), Utils.transform_attributes(body)
38
34
  end
39
35
 
40
36
  alias update_index update
@@ -78,8 +74,8 @@ module MeiliSearch
78
74
  alias add_or_replace_documents add_documents
79
75
 
80
76
  def add_documents!(documents, primary_key = nil)
81
- update = add_documents(documents, primary_key)
82
- wait_for_pending_update(update['updateId'])
77
+ task = add_documents(documents, primary_key)
78
+ wait_for_task(task['uid'])
83
79
  end
84
80
  alias replace_documents! add_documents!
85
81
  alias add_or_replace_documents! add_documents!
@@ -112,41 +108,41 @@ module MeiliSearch
112
108
  alias add_or_update_documents update_documents
113
109
 
114
110
  def update_documents!(documents, primary_key = nil)
115
- update = update_documents(documents, primary_key)
116
- wait_for_pending_update(update['updateId'])
111
+ task = update_documents(documents, primary_key)
112
+ wait_for_task(task['uid'])
117
113
  end
118
114
  alias add_or_update_documents! update_documents!
119
115
 
120
116
  def add_documents_in_batches(documents, batch_size = 1000, primary_key = nil)
121
- update_ids = []
117
+ tasks = []
122
118
  documents.each_slice(batch_size) do |batch|
123
- update_ids.append(add_documents(batch, primary_key))
119
+ tasks.append(add_documents(batch, primary_key))
124
120
  end
125
- update_ids
121
+ tasks
126
122
  end
127
123
 
128
124
  def add_documents_in_batches!(documents, batch_size = 1000, primary_key = nil)
129
- update_ids = add_documents_in_batches(documents, batch_size, primary_key)
125
+ tasks = add_documents_in_batches(documents, batch_size, primary_key)
130
126
  responses = []
131
- update_ids.each do |update_object|
132
- responses.append(wait_for_pending_update(update_object['updateId']))
127
+ tasks.each do |task_obj|
128
+ responses.append(wait_for_task(task_obj['uid']))
133
129
  end
134
130
  responses
135
131
  end
136
132
 
137
133
  def update_documents_in_batches(documents, batch_size = 1000, primary_key = nil)
138
- update_ids = []
134
+ tasks = []
139
135
  documents.each_slice(batch_size) do |batch|
140
- update_ids.append(update_documents(batch, primary_key))
136
+ tasks.append(update_documents(batch, primary_key))
141
137
  end
142
- update_ids
138
+ tasks
143
139
  end
144
140
 
145
141
  def update_documents_in_batches!(documents, batch_size = 1000, primary_key = nil)
146
- update_ids = update_documents_in_batches(documents, batch_size, primary_key)
142
+ tasks = update_documents_in_batches(documents, batch_size, primary_key)
147
143
  responses = []
148
- update_ids.each do |update_object|
149
- responses.append(wait_for_pending_update(update_object['updateId']))
144
+ tasks.each do |task_obj|
145
+ responses.append(wait_for_task(task_obj['uid']))
150
146
  end
151
147
  responses
152
148
  end
@@ -161,8 +157,8 @@ module MeiliSearch
161
157
  alias delete_multiple_documents delete_documents
162
158
 
163
159
  def delete_documents!(documents_ids)
164
- update = delete_documents(documents_ids)
165
- wait_for_pending_update(update['updateId'])
160
+ task = delete_documents(documents_ids)
161
+ wait_for_task(task['uid'])
166
162
  end
167
163
  alias delete_multiple_documents! delete_documents!
168
164
 
@@ -173,8 +169,8 @@ module MeiliSearch
173
169
  alias delete_one_document delete_document
174
170
 
175
171
  def delete_document!(document_id)
176
- update = delete_document(document_id)
177
- wait_for_pending_update(update['updateId'])
172
+ task = delete_document(document_id)
173
+ wait_for_task(task['uid'])
178
174
  end
179
175
  alias delete_one_document! delete_document!
180
176
 
@@ -183,8 +179,8 @@ module MeiliSearch
183
179
  end
184
180
 
185
181
  def delete_all_documents!
186
- update = delete_all_documents
187
- wait_for_pending_update(update['updateId'])
182
+ task = delete_all_documents
183
+ wait_for_task(task['uid'])
188
184
  end
189
185
 
190
186
  ### SEARCH
@@ -195,31 +191,23 @@ module MeiliSearch
195
191
  http_post "/indexes/#{@uid}/search", parsed_options
196
192
  end
197
193
 
198
- ### UPDATES
194
+ ### TASKS
199
195
 
200
- def get_update_status(update_id)
201
- http_get "/indexes/#{@uid}/updates/#{update_id}"
196
+ def task_endpoint
197
+ @task_endpoint ||= Task.new(@base_url, @api_key, @options)
202
198
  end
199
+ private :task_endpoint
203
200
 
204
- def get_all_update_status
205
- http_get "/indexes/#{@uid}/updates"
201
+ def task(task_uid)
202
+ task_endpoint.index_task(@uid, task_uid)
206
203
  end
207
204
 
208
- def achieved_upate?(update)
209
- update['status'] != 'enqueued' && update['status'] != 'processing'
205
+ def tasks
206
+ task_endpoint.index_tasks(@uid)
210
207
  end
211
208
 
212
- def wait_for_pending_update(update_id, timeout_in_ms = 5000, interval_in_ms = 50)
213
- Timeout.timeout(timeout_in_ms.to_f / 1000) do
214
- loop do
215
- get_update = get_update_status(update_id)
216
- return get_update if achieved_upate?(get_update)
217
-
218
- sleep interval_in_ms.to_f / 1000
219
- end
220
- end
221
- rescue Timeout::Error
222
- raise MeiliSearch::TimeoutError
209
+ def wait_for_task(task_uid, timeout_in_ms = 5000, interval_in_ms = 50)
210
+ task_endpoint.wait_for_task(task_uid, timeout_in_ms, interval_in_ms)
223
211
  end
224
212
 
225
213
  ### STATS
@@ -236,10 +224,6 @@ module MeiliSearch
236
224
  stats['isIndexing']
237
225
  end
238
226
 
239
- def last_update
240
- stats['lastUpdate']
241
- end
242
-
243
227
  def field_distribution
244
228
  stats['fieldDistribution']
245
229
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'meilisearch/http_request'
4
+ require 'timeout'
5
+
6
+ module MeiliSearch
7
+ class Task < HTTPRequest
8
+ def task_list
9
+ http_get '/tasks/'
10
+ end
11
+
12
+ def task(task_uid)
13
+ http_get "/tasks/#{task_uid}"
14
+ end
15
+
16
+ def index_tasks(index_uid)
17
+ http_get "/indexes/#{index_uid}/tasks"
18
+ end
19
+
20
+ def index_task(index_uid, task_uid)
21
+ http_get "/indexes/#{index_uid}/tasks/#{task_uid}"
22
+ end
23
+
24
+ def wait_for_task(task_uid, timeout_in_ms = 5000, interval_in_ms = 50)
25
+ Timeout.timeout(timeout_in_ms.to_f / 1000) do
26
+ loop do
27
+ task = task(task_uid)
28
+ return task if achieved_task?(task)
29
+
30
+ sleep interval_in_ms.to_f / 1000
31
+ end
32
+ end
33
+ rescue Timeout::Error
34
+ raise MeiliSearch::TimeoutError
35
+ end
36
+
37
+ private
38
+
39
+ def achieved_task?(task)
40
+ task['status'] != 'enqueued' && task['status'] != 'processing'
41
+ end
42
+ end
43
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MeiliSearch
4
- VERSION = '0.17.3'
4
+ VERSION = '0.18.0'
5
5
  end
data/lib/meilisearch.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'meilisearch/version'
4
4
  require 'meilisearch/utils'
5
+ require 'meilisearch/task'
5
6
  require 'meilisearch/client'
6
7
  require 'meilisearch/index'
7
8
 
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.17.3
4
+ version: 0.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Meili
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-13 00:00:00.000000000 Z
11
+ date: 2022-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -43,6 +43,7 @@ files:
43
43
  - lib/meilisearch/error.rb
44
44
  - lib/meilisearch/http_request.rb
45
45
  - lib/meilisearch/index.rb
46
+ - lib/meilisearch/task.rb
46
47
  - lib/meilisearch/utils.rb
47
48
  - lib/meilisearch/version.rb
48
49
  homepage: https://github.com/meilisearch/meilisearch-ruby