meilisearch 0.8.1 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4b06c291f3bf2609f0044199d4c35418052ab7cddb3d41216b7ba908bbfe6f61
4
- data.tar.gz: 7fd8ddd39e24e14e5dcb39d3187df368f7cb88d5b46f4ce824e4b90d2670225a
3
+ metadata.gz: 17248185a6d2ba10e2e5fac61b6d9d07a0cac218a2accc4cd5b3bd130517f837
4
+ data.tar.gz: dd20f35451242677ec05ac52d95cbc14915dea6676562ef23c58a58655db24ad
5
5
  SHA512:
6
- metadata.gz: 21248cb8c60cc8875e4280eb8223ad7e48483c9a97d552a4d4a08aa887da54039d6532fd7fe8981199b8d232a25439b8a009250e7f4fe76db0d3b81e8c043885
7
- data.tar.gz: 7d1ce9c9a6d956b5609aa0366435ed8d130b15e7e857aabf127263ce24f76ab5a80a9ee1d7af49153b5c7b21742dbec4ae827a33781c79a7db7e98984f5ead99
6
+ metadata.gz: f0c0c7a6726257c62e14108f33df551cd5ec3d7c5f4b6f8eb3fa975988da3915d7b3167e0074c3e21134f49d1a40d84a55e4145ae7b4ffac45cd1daa8954e00d
7
+ data.tar.gz: 3cb711d0577b92fe428b90ec16d2cbc7fb593aba63f014206c17a88bca2ba4de0fdd58b51e2b014640ae7118996142b9edc4c4d4d00250fc8a6893df117e3527
@@ -1,16 +1,94 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'meilisearch/http_request'
4
- require 'meilisearch/client/keys'
5
- require 'meilisearch/client/stats'
6
- require 'meilisearch/client/health'
7
- require 'meilisearch/client/indexes'
8
4
 
9
5
  module MeiliSearch
10
6
  class Client < HTTPRequest
11
- include MeiliSearch::Client::Keys
12
- include MeiliSearch::Client::Stats
13
- include MeiliSearch::Client::Health
14
- include MeiliSearch::Client::Indexes
7
+ ### INDEXES
8
+
9
+ def indexes
10
+ http_get '/indexes'
11
+ end
12
+
13
+ def show_index(index_uid)
14
+ index_object(index_uid).show
15
+ end
16
+
17
+ # Usage:
18
+ # client.create_index('indexUID')
19
+ # client.create_index('indexUID', primaryKey: 'id')
20
+ def create_index(index_uid, options = {})
21
+ body = options.merge(uid: index_uid)
22
+ res = http_post '/indexes', body
23
+ index_object(res['uid'])
24
+ end
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
+
35
+ def delete_index(index_uid)
36
+ index_object(index_uid).delete
37
+ end
38
+
39
+ # Usage:
40
+ # client.index('indexUID')
41
+ def index(index_uid)
42
+ index_object(index_uid)
43
+ end
44
+ alias get_index index
45
+
46
+ ### KEYS
47
+
48
+ def keys
49
+ http_get '/keys'
50
+ end
51
+ alias get_keys keys
52
+
53
+ ### HEALTH
54
+
55
+ def healthy?
56
+ http_get '/health'
57
+ true
58
+ rescue StandardError
59
+ false
60
+ end
61
+
62
+ def health
63
+ http_get '/health'
64
+ end
65
+
66
+ def update_health(bool)
67
+ http_put '/health', health: bool
68
+ end
69
+
70
+ ### STATS
71
+
72
+ def version
73
+ http_get '/version'
74
+ end
75
+
76
+ def sysinfo
77
+ http_get '/sys-info'
78
+ end
79
+
80
+ def pretty_sysinfo
81
+ http_get '/sys-info/pretty'
82
+ end
83
+
84
+ def stats
85
+ http_get '/stats'
86
+ end
87
+
88
+ private
89
+
90
+ def index_object(uid)
91
+ Index.new(uid, @base_url, @api_key)
92
+ end
15
93
  end
16
94
  end
@@ -1,34 +1,58 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MeiliSearch
4
- class MeiliSearchError < StandardError; end
5
- class IndexIdentifierError < MeiliSearchError; end
4
+ class ApiError < StandardError
5
+ attr_reader :http_code # e.g. 400, 404...
6
+ attr_reader :http_message # e.g. Bad Request, Not Found...
7
+ attr_reader :http_body # The response body received from the MeiliSearch API
8
+ attr_reader :ms_code # The error code given by the MeiliSearch API
9
+ attr_reader :ms_type # The error type given by the MeiliSearch API
10
+ attr_reader :ms_link # The documentation link given by the MeiliSearch API
11
+ attr_reader :ms_message # The error message given by the MeiliSearch API
12
+ attr_reader :message # The detailed error message of this error class
6
13
 
7
- class HTTPError < MeiliSearchError
8
- attr_reader :status
14
+ alias code ms_code
15
+ alias type ms_type
16
+ alias link ms_link
17
+
18
+ def initialize(http_code, http_message, http_body)
19
+ get_meilisearch_error_info(http_body) unless http_body.nil? || http_body.empty?
20
+ @http_code = http_code
21
+ @http_message = http_message
22
+ @ms_message ||= 'MeiliSearch API has not returned any error message'
23
+ @ms_link ||= '<no documentation link found>'
24
+ @message = "#{http_code} #{http_message} - #{@ms_message.capitalize}. See #{ms_link}."
25
+ super(details)
26
+ end
27
+
28
+ def get_meilisearch_error_info(http_body)
29
+ @http_body = JSON.parse(http_body)
30
+ @ms_code = @http_body['errorCode']
31
+ @ms_message = @http_body['message']
32
+ @ms_type = @http_body['errorType']
33
+ @ms_link = @http_body['errorLink']
34
+ end
35
+
36
+ def details
37
+ "MeiliSearch::ApiError - code: #{@ms_code} - type: #{ms_type} - message: #{@ms_message} - link: #{ms_link}"
38
+ end
39
+ end
40
+
41
+ class CommunicationError < StandardError
9
42
  attr_reader :message
10
- attr_reader :http_body
11
- attr_reader :http_body_message
12
- attr_reader :details
13
-
14
- alias code status
15
- alias body http_body
16
- alias body_message http_body_message
17
-
18
- def initialize(status, message, http_body, details = nil)
19
- @status = status
20
- unless http_body.nil? || http_body.empty?
21
- @http_body = JSON.parse(http_body)
22
- @http_body_message = @http_body['message']
23
- end
24
- @message = message.capitalize
25
- @message = "#{@message} - #{@http_body_message.capitalize}" unless @http_body_message.nil?
26
- @details = details
43
+
44
+ def initialize(message)
45
+ @message = "An error occurred while trying to connect to the MeiliSearch instance: #{message}"
46
+ super(@message)
27
47
  end
48
+ end
49
+
50
+ class TimeoutError < StandardError
51
+ attr_reader :message
28
52
 
29
- def to_s
30
- final_message = @details.nil? ? @message : "#{@message}. #{@details}"
31
- "#{@status}: #{final_message}."
53
+ def initialize
54
+ @message = 'The update was not processed in the expected time'
55
+ super(@message)
32
56
  end
33
57
  end
34
58
  end
@@ -16,67 +16,63 @@ module MeiliSearch
16
16
  }.compact
17
17
  end
18
18
 
19
- def http_get(path = '', query = {})
20
- response = self.class.get(
21
- @base_url + path,
22
- query: query,
23
- headers: @headers,
24
- timeout: 1
19
+ def http_get(relative_path = '', query_params = {})
20
+ send_request(
21
+ proc { |path, config| self.class.get(path, config) },
22
+ relative_path,
23
+ query_params
25
24
  )
26
- validate(response)
27
25
  end
28
26
 
29
- def http_post(path = '', body = nil)
30
- response = if body.nil?
31
- self.class.post(
32
- @base_url + path,
33
- headers: @headers,
34
- timeout: 1
35
- )
36
- else
37
- self.class.post(
38
- @base_url + path,
39
- body: body.to_json,
40
- headers: @headers,
41
- timeout: 1
42
- )
43
- end
44
- validate(response)
27
+ def http_post(relative_path = '', body = nil, query_params = nil)
28
+ send_request(
29
+ proc { |path, config| self.class.post(path, config) },
30
+ relative_path,
31
+ query_params,
32
+ body
33
+ )
45
34
  end
46
35
 
47
- def http_put(path = '', body = {})
48
- response = self.class.put(
49
- @base_url + path,
50
- body: body.to_json,
51
- headers: @headers,
52
- timeout: 1
36
+ def http_put(relative_path = '', body = nil, query_params = nil)
37
+ send_request(
38
+ proc { |path, config| self.class.put(path, config) },
39
+ relative_path,
40
+ query_params,
41
+ body
53
42
  )
54
- validate(response)
55
43
  end
56
44
 
57
- def http_patch(path = '', body = {})
58
- response = self.class.patch(
59
- @base_url + path,
60
- body: body.to_json,
61
- headers: @headers,
62
- timeout: 1
45
+ def http_delete(relative_path = '')
46
+ send_request(
47
+ proc { |path, config| self.class.delete(path, config) },
48
+ relative_path
63
49
  )
50
+ end
51
+
52
+ private
53
+
54
+ def send_request(http_method, relative_path, query_params = nil, body = nil)
55
+ config = http_config(query_params, body)
56
+ begin
57
+ response = http_method.call(@base_url + relative_path, config)
58
+ rescue Errno::ECONNREFUSED => e
59
+ raise CommunicationError, e.message
60
+ end
64
61
  validate(response)
65
62
  end
66
63
 
67
- def http_delete(path = '')
68
- response = self.class.delete(
69
- @base_url + path,
64
+ def http_config(query_params, body)
65
+ body = body.to_json unless body.nil?
66
+ {
70
67
  headers: @headers,
68
+ query: query_params,
69
+ body: body,
71
70
  timeout: 1
72
- )
73
- validate(response)
71
+ }.compact
74
72
  end
75
73
 
76
- private
77
-
78
74
  def validate(response)
79
- raise HTTPError.new(response.code, response.message, response.body) unless response.success?
75
+ raise ApiError.new(response.code, response.message, response.body) unless response.success?
80
76
 
81
77
  response.parsed_response
82
78
  end
@@ -1,26 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'meilisearch/http_request'
4
- require 'meilisearch/index/base'
5
- require 'meilisearch/index/documents'
6
- require 'meilisearch/index/search'
7
- require 'meilisearch/index/stats'
8
- require 'meilisearch/index/settings'
9
- require 'meilisearch/index/updates'
10
- require 'meilisearch/index/stop_words'
11
- require 'meilisearch/index/synonyms'
4
+ require 'timeout'
12
5
 
13
6
  module MeiliSearch
14
7
  class Index < HTTPRequest
15
- include MeiliSearch::Index::Base
16
- include MeiliSearch::Index::Documents
17
- include MeiliSearch::Index::Search
18
- include MeiliSearch::Index::Stats
19
- include MeiliSearch::Index::Settings
20
- include MeiliSearch::Index::Updates
21
- include MeiliSearch::Index::StopWords
22
- include MeiliSearch::Index::Synonyms
23
-
24
8
  attr_reader :uid
25
9
 
26
10
  def initialize(index_uid, url, api_key = nil)
@@ -28,15 +12,262 @@ module MeiliSearch
28
12
  super(url, api_key)
29
13
  end
30
14
 
31
- def name
32
- index_name_from_uid
15
+ def show
16
+ http_get "/indexes/#{@uid}"
17
+ end
18
+ alias show_index show
19
+
20
+ def update(body)
21
+ http_put "/indexes/#{@uid}", body
22
+ end
23
+ alias update_index update
24
+
25
+ def delete
26
+ http_delete "/indexes/#{@uid}"
27
+ end
28
+ alias delete_index delete
29
+
30
+ def primary_key
31
+ show['primaryKey']
32
+ end
33
+ alias get_primary_key primary_key
34
+
35
+ ### DOCUMENTS
36
+
37
+ def document(document_id)
38
+ encode_document = URI.encode_www_form_component(document_id)
39
+ http_get "/indexes/#{@uid}/documents/#{encode_document}"
40
+ end
41
+ alias get_document document
42
+ alias get_one_document document
43
+
44
+ def documents(options = {})
45
+ http_get "/indexes/#{@uid}/documents", options
46
+ end
47
+ alias get_documents documents
48
+
49
+ def add_documents(documents, primary_key = nil)
50
+ documents = [documents] if documents.is_a?(Hash)
51
+ http_post "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact
52
+ end
53
+ alias replace_documents add_documents
54
+ alias add_or_replace_documents add_documents
55
+
56
+ def update_documents(documents, primary_key = nil)
57
+ documents = [documents] if documents.is_a?(Hash)
58
+ http_put "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact
59
+ end
60
+ alias add_or_update_documents update_documents
61
+
62
+ def delete_documents(documents_ids)
63
+ if documents_ids.is_a?(Array)
64
+ http_post "/indexes/#{@uid}/documents/delete-batch", documents_ids
65
+ else
66
+ delete_document(documents_ids)
67
+ end
68
+ end
69
+ alias delete_multiple_documents delete_documents
70
+
71
+ def delete_document(document_id)
72
+ encode_document = URI.encode_www_form_component(document_id)
73
+ http_delete "/indexes/#{@uid}/documents/#{encode_document}"
74
+ end
75
+ alias delete_one_document delete_document
76
+
77
+ def delete_all_documents
78
+ http_delete "/indexes/#{@uid}/documents"
79
+ end
80
+
81
+ ### SEARCH
82
+
83
+ def search(query, options = {})
84
+ parsed_options = options.transform_keys(&:to_sym).map do |k, v|
85
+ if [:facetFilters, :facetsDistribution].include?(k)
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)
94
+ end
95
+
96
+ ### UPDATES
97
+
98
+ def get_update_status(update_id)
99
+ http_get "/indexes/#{@uid}/updates/#{update_id}"
100
+ end
101
+
102
+ def get_all_update_status
103
+ http_get "/indexes/#{@uid}/updates"
104
+ end
105
+
106
+ def wait_for_pending_update(update_id, timeout_in_ms = 5000, interval_in_ms = 50)
107
+ Timeout.timeout(timeout_in_ms.to_f / 1000) do
108
+ loop do
109
+ get_update = get_update_status(update_id)
110
+ return get_update if get_update['status'] != 'enqueued'
111
+
112
+ sleep interval_in_ms.to_f / 1000
113
+ end
114
+ end
115
+ rescue Timeout::Error
116
+ raise MeiliSearch::TimeoutError
117
+ end
118
+
119
+ ### STATS
120
+
121
+ def stats
122
+ http_get "/indexes/#{@uid}/stats"
123
+ end
124
+
125
+ def number_of_documents
126
+ stats['numberOfDocuments']
127
+ end
128
+
129
+ def indexing?
130
+ stats['isIndexing']
131
+ end
132
+
133
+ def last_update
134
+ stats['lastUpdate']
135
+ end
136
+
137
+ def fields_distribution
138
+ stats['fieldsDistribution']
139
+ end
140
+
141
+ ### SETTINGS - GENERAL
142
+
143
+ def settings
144
+ http_get "/indexes/#{@uid}/settings"
145
+ end
146
+ alias get_settings settings
147
+
148
+ def update_settings(settings)
149
+ http_post "/indexes/#{@uid}/settings", settings
150
+ end
151
+
152
+ def reset_settings
153
+ http_delete "/indexes/#{@uid}/settings"
33
154
  end
34
- alias get_name name
35
155
 
36
- private
156
+ ### SETTINGS - RANKING RULES
157
+
158
+ def ranking_rules
159
+ http_get "/indexes/#{@uid}/settings/ranking-rules"
160
+ end
161
+ alias get_ranking_rules ranking_rules
162
+
163
+ def update_ranking_rules(ranking_rules)
164
+ http_post "/indexes/#{@uid}/settings/ranking-rules", ranking_rules
165
+ end
166
+
167
+ def reset_ranking_rules
168
+ http_delete "/indexes/#{@uid}/settings/ranking-rules"
169
+ end
170
+
171
+ ### SETTINGS - SYNONYMS
172
+
173
+ def synonyms
174
+ http_get "/indexes/#{@uid}/settings/synonyms"
175
+ end
176
+ alias get_synonyms synonyms
177
+
178
+ def update_synonyms(synonyms)
179
+ http_post "/indexes/#{@uid}/settings/synonyms", synonyms
180
+ end
181
+
182
+ def reset_synonyms
183
+ http_delete "/indexes/#{@uid}/settings/synonyms"
184
+ end
185
+
186
+ ### SETTINGS - STOP-WORDS
187
+
188
+ def stop_words
189
+ http_get "/indexes/#{@uid}/settings/stop-words"
190
+ end
191
+ alias get_stop_words stop_words
192
+
193
+ def update_stop_words(stop_words)
194
+ body = stop_words.is_a?(Array) ? stop_words : [stop_words]
195
+ http_post "/indexes/#{@uid}/settings/stop-words", body
196
+ end
197
+
198
+ def reset_stop_words
199
+ http_delete "/indexes/#{@uid}/settings/stop-words"
200
+ end
201
+
202
+ ### SETTINGS - DINSTINCT ATTRIBUTE
203
+
204
+ def distinct_attribute
205
+ http_get "/indexes/#{@uid}/settings/distinct-attribute"
206
+ end
207
+ alias get_distinct_attribute distinct_attribute
208
+
209
+ def update_distinct_attribute(distinct_attribute)
210
+ http_post "/indexes/#{@uid}/settings/distinct-attribute", distinct_attribute
211
+ end
212
+
213
+ def reset_distinct_attribute
214
+ http_delete "/indexes/#{@uid}/settings/distinct-attribute"
215
+ end
216
+
217
+ ### SETTINGS - SEARCHABLE ATTRIBUTES
218
+
219
+ def searchable_attributes
220
+ http_get "/indexes/#{@uid}/settings/searchable-attributes"
221
+ end
222
+ alias get_searchable_attributes searchable_attributes
223
+
224
+ def update_searchable_attributes(searchable_attributes)
225
+ http_post "/indexes/#{@uid}/settings/searchable-attributes", searchable_attributes
226
+ end
227
+
228
+ def reset_searchable_attributes
229
+ http_delete "/indexes/#{@uid}/settings/searchable-attributes"
230
+ end
231
+
232
+ ### SETTINGS - DISPLAYED ATTRIBUTES
233
+
234
+ def displayed_attributes
235
+ http_get "/indexes/#{@uid}/settings/displayed-attributes"
236
+ end
237
+ alias get_displayed_attributes displayed_attributes
238
+
239
+ def update_displayed_attributes(displayed_attributes)
240
+ http_post "/indexes/#{@uid}/settings/displayed-attributes", displayed_attributes
241
+ end
242
+
243
+ def reset_displayed_attributes
244
+ http_delete "/indexes/#{@uid}/settings/displayed-attributes"
245
+ end
246
+
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
+ ### SETTINGS - ATTRIBUTES FOR FACETING
259
+
260
+ def attributes_for_faceting
261
+ http_get "/indexes/#{@uid}/settings/attributes-for-faceting"
262
+ end
263
+ alias get_attributes_for_faceting attributes_for_faceting
264
+
265
+ def update_attributes_for_faceting(attributes_for_faceting)
266
+ http_post "/indexes/#{@uid}/settings/attributes-for-faceting", attributes_for_faceting
267
+ end
37
268
 
38
- def index_name_from_uid
39
- show['name']
269
+ def reset_attributes_for_faceting
270
+ http_delete "/indexes/#{@uid}/settings/attributes-for-faceting"
40
271
  end
41
272
  end
42
273
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MeiliSearch
4
- VERSION = '0.8.1'
4
+ VERSION = '0.12.0'
5
5
  end
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: meilisearch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.12.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-03-04 00:00:00.000000000 Z
11
+ date: 2020-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 0.17.1
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: 0.19.0
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: 0.17.1
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 0.19.0
27
33
  description: An easy-to-use ruby client for Meilisearch API. See https://github.com/meilisearch/MeiliSearch
28
34
  email: bonjour@meilisearch.com
29
35
  executables: []
@@ -32,21 +38,9 @@ extra_rdoc_files: []
32
38
  files:
33
39
  - lib/meilisearch.rb
34
40
  - lib/meilisearch/client.rb
35
- - lib/meilisearch/client/health.rb
36
- - lib/meilisearch/client/indexes.rb
37
- - lib/meilisearch/client/keys.rb
38
- - lib/meilisearch/client/stats.rb
39
41
  - lib/meilisearch/error.rb
40
42
  - lib/meilisearch/http_request.rb
41
43
  - lib/meilisearch/index.rb
42
- - lib/meilisearch/index/base.rb
43
- - lib/meilisearch/index/documents.rb
44
- - lib/meilisearch/index/search.rb
45
- - lib/meilisearch/index/settings.rb
46
- - lib/meilisearch/index/stats.rb
47
- - lib/meilisearch/index/stop_words.rb
48
- - lib/meilisearch/index/synonyms.rb
49
- - lib/meilisearch/index/updates.rb
50
44
  - lib/meilisearch/version.rb
51
45
  homepage: https://github.com/meilisearch/meilisearch-ruby
52
46
  licenses:
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MeiliSearch
4
- class Client < HTTPRequest
5
- module Health
6
- def healthy?
7
- http_get '/health'
8
- true
9
- rescue StandardError
10
- false
11
- end
12
-
13
- def health
14
- http_get '/health'
15
- end
16
-
17
- def update_health(bool)
18
- http_put '/health', health: bool
19
- end
20
- end
21
- end
22
- end
@@ -1,71 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MeiliSearch
4
- class Client < HTTPRequest
5
- module Indexes
6
- def indexes
7
- http_get '/indexes'
8
- end
9
-
10
- def show_index(index_uid)
11
- index_object(index_uid).show
12
- end
13
-
14
- # Usage:
15
- # create_index('name')
16
- # create_index(name: 'name')
17
- # create_index(name: 'name', uid: 'uid')
18
- # create_index(name: 'name', schema: {...})
19
- # create_index(name: 'name', uid: 'uid', schema: {...})
20
- def create_index(attributes)
21
- body = if attributes.is_a?(Hash)
22
- attributes
23
- else
24
- { name: attributes }
25
- end
26
- res = http_post '/indexes', body
27
- index_object(res['uid'])
28
- end
29
-
30
- def delete_index(index_uid)
31
- index_object(index_uid).delete
32
- end
33
-
34
- # Usage:
35
- # index('uid')
36
- # index(uid: 'uid')
37
- # index(name: 'name') => WARNING: the name of an index is not guaranteed to be unique. This method will return the first occurrence. We recommend using the index uid instead.
38
- # index(uid: 'uid', name: 'name') => only the uid field will be taken into account.
39
- def index(identifier)
40
- uid = index_uid(identifier)
41
- raise IndexIdentifierError if uid.nil?
42
-
43
- index_object(uid)
44
- end
45
- alias get_index index
46
-
47
- private
48
-
49
- def index_object(uid)
50
- Index.new(uid, @base_url, @api_key)
51
- end
52
-
53
- def index_uid(identifier)
54
- if identifier.is_a?(Hash)
55
- identifier[:uid] || index_uid_from_name(identifier)
56
- else
57
- identifier
58
- end
59
- end
60
-
61
- def index_uid_from_name(identifier)
62
- index = indexes.find { |i| i['name'] == identifier[:name] }
63
- if index.nil?
64
- nil
65
- else
66
- index['uid']
67
- end
68
- end
69
- end
70
- end
71
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MeiliSearch
4
- class Client < HTTPRequest
5
- module Keys
6
- def keys
7
- http_get '/keys'
8
- end
9
-
10
- def key(key)
11
- http_get "/keys/#{key}"
12
- end
13
-
14
- def create_key(options = {})
15
- http_post '/keys', options
16
- end
17
-
18
- def update_key(key, options = {})
19
- http_put "/keys/#{key}", options
20
- end
21
-
22
- def delete_key(key)
23
- http_delete "/keys/#{key}"
24
- end
25
- end
26
- end
27
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MeiliSearch
4
- class Client < HTTPRequest
5
- module Stats
6
- def version
7
- http_get '/version'
8
- end
9
-
10
- def sysinfo
11
- http_get '/sys-info'
12
- end
13
-
14
- def pretty_sysinfo
15
- http_get '/sys-info/pretty'
16
- end
17
-
18
- def stats
19
- http_get '/stats'
20
- end
21
- end
22
- end
23
- end
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MeiliSearch
4
- class Index < HTTPRequest
5
- module Base
6
- def show
7
- http_get "/indexes/#{@uid}"
8
- end
9
- alias show_index show
10
-
11
- def schema
12
- http_get "/indexes/#{@uid}/schema"
13
- rescue HTTPError => e
14
- raise if e.body_message != 'missing index schema'
15
-
16
- nil
17
- end
18
- alias get_schema schema
19
-
20
- def update_name(new_index_name)
21
- http_put "/indexes/#{@uid}", name: new_index_name
22
- end
23
- alias update_index_name update_name
24
-
25
- def update_schema(new_schema)
26
- http_put "/indexes/#{@uid}/schema", new_schema
27
- end
28
- alias update_index_schema update_schema
29
-
30
- def delete
31
- http_delete "/indexes/#{@uid}"
32
- end
33
- alias delete_index delete
34
- end
35
- end
36
- end
@@ -1,52 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MeiliSearch
4
- class Index < HTTPRequest
5
- module Documents
6
- def document(document_id)
7
- encode_document = URI.encode_www_form_component(document_id)
8
- http_get "/indexes/#{@uid}/documents/#{encode_document}"
9
- end
10
- alias get_document document
11
- alias get_one_document document
12
-
13
- def documents(options = {})
14
- http_get "/indexes/#{@uid}/documents", options
15
- end
16
- alias get_documents documents
17
-
18
- def add_documents(documents)
19
- documents = [documents] if documents.is_a?(Hash)
20
- http_post "/indexes/#{@uid}/documents", documents
21
- end
22
- alias replace_documents add_documents
23
- alias add_or_replace_documents add_documents
24
-
25
- def update_documents(documents)
26
- documents = [documents] if documents.is_a?(Hash)
27
- http_put "/indexes/#{@uid}/documents", documents
28
- end
29
- alias add_or_update_documents update_documents
30
-
31
- def clear_documents
32
- http_delete "/indexes/#{@uid}/documents"
33
- end
34
- alias clear_all_documents clear_documents
35
-
36
- def delete_documents(documents_ids)
37
- if documents_ids.is_a?(Array)
38
- http_post "/indexes/#{@uid}/documents/delete", documents_ids
39
- else
40
- delete_document(documents_ids)
41
- end
42
- end
43
- alias delete_multiple_documents delete_documents
44
-
45
- def delete_document(document_id)
46
- encode_document = URI.encode_www_form_component(document_id)
47
- http_delete "/indexes/#{@uid}/documents/#{encode_document}"
48
- end
49
- alias delete_one_document delete_document
50
- end
51
- end
52
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MeiliSearch
4
- class Index < HTTPRequest
5
- module Search
6
- def search(query, options = {})
7
- http_get "/indexes/#{@uid}/search", { q: query }.merge(options)
8
- end
9
- end
10
- end
11
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MeiliSearch
4
- class Index < HTTPRequest
5
- module Settings
6
- def settings
7
- http_get "/indexes/#{@uid}/settings"
8
- end
9
- alias get_settings settings
10
-
11
- def add_or_replace_settings(options)
12
- http_post "/indexes/#{@uid}/settings", options
13
- end
14
- alias add_settings add_or_replace_settings
15
- alias replace_settings add_or_replace_settings
16
-
17
- def reset_all_settings
18
- body = {
19
- rankingOrder: nil,
20
- distinctField: nil,
21
- rankingRules: nil
22
- }
23
- http_post "/indexes/#{@uid}/settings", body
24
- end
25
- end
26
- end
27
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MeiliSearch
4
- class Index < HTTPRequest
5
- module Stats
6
- def stats
7
- http_get "/stats/#{@uid}"
8
- end
9
-
10
- def number_of_documents
11
- stats['numberOfDocuments']
12
- end
13
-
14
- def indexing?
15
- stats['isIndexing']
16
- end
17
-
18
- def last_update
19
- stats['lastUpdate']
20
- end
21
-
22
- def fields_frequency
23
- stats['fieldsFrequency']
24
- end
25
- end
26
- end
27
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MeiliSearch
4
- class Index < HTTPRequest
5
- module StopWords
6
- def stop_words
7
- http_get "/indexes/#{@uid}/stop-words"
8
- end
9
- alias get_stop_words stop_words
10
-
11
- def add_stop_words(stop_words)
12
- if stop_words.is_a?(Array)
13
- http_patch "/indexes/#{@uid}/stop-words", stop_words
14
- else
15
- http_patch "/indexes/#{@uid}/stop-words", [stop_words]
16
- end
17
- end
18
-
19
- def delete_stop_words(stop_words)
20
- if stop_words.is_a?(Array)
21
- http_post "/indexes/#{@uid}/stop-words", stop_words
22
- else
23
- http_post "/indexes/#{@uid}/stop-words", [stop_words]
24
- end
25
- end
26
- end
27
- end
28
- end
@@ -1,52 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MeiliSearch
4
- class Index < HTTPRequest
5
- module Synonyms
6
- def synonyms_of(synonym)
7
- encode_synonym = URI.encode_www_form_component(synonym)
8
- http_get "/indexes/#{@uid}/synonyms/#{encode_synonym}"
9
- end
10
- alias get_synonyms_of_one_sequence synonyms_of
11
- alias get_synonyms_of synonyms_of
12
-
13
- def all_synonyms
14
- http_get "/indexes/#{@uid}/synonyms"
15
- end
16
- alias get_all_synonyms all_synonyms
17
- alias get_all_sequences all_synonyms
18
-
19
- def add_synonyms(synonyms)
20
- http_post "/indexes/#{@uid}/synonyms", synonyms
21
- end
22
-
23
- def add_synonyms_one_way(input, synonyms)
24
- add_synonyms(input: input, synonyms: synonyms)
25
- end
26
-
27
- def add_synonyms_multi_way(synonyms)
28
- add_synonyms(synonyms: synonyms)
29
- end
30
-
31
- def update_synonym(synonym, new_synonyms)
32
- encode_synonym = URI.encode_www_form_component(synonym)
33
- http_put "/indexes/#{@uid}/synonyms/#{encode_synonym}", new_synonyms
34
- end
35
-
36
- def delete_synonym(synonym)
37
- encode_synonym = URI.encode_www_form_component(synonym)
38
- http_delete "/indexes/#{@uid}/synonyms/#{encode_synonym}"
39
- end
40
- alias delete_one_synonym delete_synonym
41
-
42
- def batch_write_synonyms(synonyms)
43
- http_post "/indexes/#{@uid}/synonyms/batch", synonyms
44
- end
45
-
46
- def clear_synonyms
47
- http_delete "/indexes/#{@uid}/synonyms"
48
- end
49
- alias clear_all_synonyms clear_synonyms
50
- end
51
- end
52
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MeiliSearch
4
- class Index < HTTPRequest
5
- module Updates
6
- def get_update_status(update_id)
7
- http_get "/indexes/#{@uid}/updates/#{update_id}"
8
- end
9
-
10
- def get_all_update_status
11
- http_get "/indexes/#{@uid}/updates"
12
- end
13
- end
14
- end
15
- end