Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes related to the next MeiliSearch release (v0.25.0) #273

Merged
merged 14 commits into from
Jan 12, 2022
Merged
64 changes: 60 additions & 4 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ delete_documents_1: |-
client.index('movies').delete_documents([23488, 153738, 437035, 363869])
search_post_1: |-
client.index('movies').search('american ninja')
get_update_1: |-
client.index('movies').get_update_status(1)
get_all_updates_1: |-
client.index('movies').get_all_update_status
get_task_by_index_1: |-
client.index('movies').task(1)
get_all_tasks_by_index_1: |-
client.index('movies').tasks
get_task_1: |-
client.task(1)
get_all_tasks_1: |-
client.tasks
get_keys_1: |-
client.keys
get_settings_1: |-
Expand Down Expand Up @@ -400,3 +404,55 @@ geosearch_guide_sort_usage_1: |-
client.index('restaurants').search('', { sort: ['_geoPoint(48.8583701,2.2922926):asc'] })
geosearch_guide_sort_usage_2: |-
client.index('restaurants').search('', { sort: ['_geoPoint(48.8583701,2.2922926):asc', 'rating:desc'] })
authorization_header_1: |-
client = MeiliSearch::Client.new('http://127.0.0.1:7700', 'masterKey')
client.keys
get_one_key_1: |-
client.key('d0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4')
get_all_keys_1: |-
client.keys
create_a_key_1: |-
client.create_key(
description: 'Add documents: Products API key',
actions: ['documents.add'],
indexes: ['products'],
expires_at: '2042-04-02T00:42:42Z'
)
update_a_key_1: |-
client.update_key(
'd0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4',
{
description: 'Manage documents: Products/Reviews API key',
actions: [
'documents.add',
'documents.delete'
],
indexes: [
'products',
'reviews'
],
expires_at: '2042-04-02T00:42:42Z'
}
)
delete_a_key_1: |-
client.delete_key('d0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4')
security_guide_search_key_1: |-
client = MeiliSearch::Client.new('http://127.0.0.1:7700', 'apiKey')
client.index('patient_medical_records').search
security_guide_update_key_1: |-
client = MeiliSearch::Client.new('http://127.0.0.1:7700', 'masterKey')
client.update_key('d0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4', indexes: ['doctors'])
security_guide_create_key_1: |-
client = MeiliSearch::Client.new('http://127.0.0.1:7700', 'masterKey')
client.create_key(
description: 'Search patient records key',
actions: ['search'],
indexes: ['patient_medical_records'],
expires_at: '2023-01-01T00:00:00Z'
)
security_guide_list_keys_1: |-
client = MeiliSearch::Client.new('http://127.0.0.1:7700', 'masterKey')
client.keys
security_guide_delete_key_1: |-
client = MeiliSearch::Client.new('http://127.0.0.1:7700', 'masterKey')
client.delete_key('d0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4')
10 changes: 5 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2021-11-21 21:31:55 UTC using RuboCop version 1.23.0.
# on 2021-12-13 22:26:31 UTC using RuboCop version 1.23.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -20,23 +20,23 @@ Layout/HeredocIndentation:
Exclude:
- 'spec/meilisearch/index/documents_spec.rb'

# Offense count: 32
# Offense count: 33
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
# IgnoredMethods: refine
Metrics/BlockLength:
Max: 512
Max: 557

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 289
Max: 278

# Offense count: 1
# Configuration parameters: Max, CountKeywordArgs.
Metrics/ParameterLists:
MaxOptionalParameters: 4

# Offense count: 2
# Offense count: 1
Naming/AccessorMethodName:
Exclude:
- 'lib/meilisearch/index.rb'
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ documents = [
{ id: 6, title: 'Philadelphia', genres: ['Drama'] },
]
# If the index 'movies' does not exist, MeiliSearch creates it when you first add the documents.
index.add_documents(documents) # => { "updateId": 0 }
index.add_documents(documents) # => { "uid": 0 }
```

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).
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).

💡 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.

Expand Down Expand Up @@ -165,7 +165,7 @@ index.update_filterable_attributes([

You only need to perform this operation once.

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).
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)).

Then, you can perform the search:

Expand Down Expand Up @@ -198,7 +198,7 @@ JSON output:

## 🤖 Compatibility with MeiliSearch

This package only guarantees the compatibility with the [version v0.24.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.24.0).
This package only guarantees the compatibility with the [version v0.25.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.25.0).

## 💡 Learn More

Expand Down
1 change: 1 addition & 0 deletions lib/meilisearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'meilisearch/version'
require 'meilisearch/utils'
require 'meilisearch/task'
brunoocasali marked this conversation as resolved.
Show resolved Hide resolved
require 'meilisearch/client'
require 'meilisearch/index'

Expand Down
67 changes: 44 additions & 23 deletions lib/meilisearch/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,20 @@ def indexes
def create_index(index_uid, options = {})
body = Utils.transform_attributes(options.merge(uid: index_uid))

index_hash = http_post '/indexes', body
index_object(index_hash['uid'], index_hash['primaryKey'])
http_post '/indexes', body
end

def get_or_create_index(index_uid, options = {})
curquiza marked this conversation as resolved.
Show resolved Hide resolved
begin
index_instance = fetch_index(index_uid)
rescue ApiError => e
raise e unless e.code == 'index_not_found'

index_instance = create_index(index_uid, options)
end
index_instance
# Synchronous version of create_index.
# Waits for the task to be achieved, be careful when using it.
def create_index!(index_uid, options = {})
brunoocasali marked this conversation as resolved.
Show resolved Hide resolved
task = create_index(index_uid, options)
wait_for_task(task['uid'])
end

def delete_index(index_uid)
index_object(index_uid).delete
end

# Usage:
# client.delete_index_if_exists('indexUID')
def delete_index_if_exists(index_uid)
index_object(index_uid).delete
true
rescue ApiError => e
raise e if e.code != 'index_not_found'

false
end

# Usage:
# client.index('indexUID')
def index(index_uid)
Expand All @@ -71,7 +55,26 @@ def fetch_raw_index(index_uid)
def keys
http_get '/keys'
end
alias get_keys keys

def key(key_uid)
brunoocasali marked this conversation as resolved.
Show resolved Hide resolved
http_get "/keys/#{key_uid}"
end

def create_key(key_options)
body = Utils.transform_attributes(key_options)

http_post '/keys', body
end

def update_key(key_uid, key_options)
body = Utils.transform_attributes(key_options)

http_patch "/keys/#{key_uid}", body
end

def delete_key(key_uid)
http_delete "/keys/#{key_uid}"
end

### HEALTH

Expand Down Expand Up @@ -107,10 +110,28 @@ def dump_status(dump_uid)
end
alias get_dump_status dump_status

### TASKS

def tasks
task_endpoint.task_list
end

def task(task_uid)
task_endpoint.task(task_uid)
end

def wait_for_task(task_uid, timeout_in_ms = 5000, interval_in_ms = 50)
task_endpoint.wait_for_task(task_uid, timeout_in_ms, interval_in_ms)
end

private

def index_object(uid, primary_key = nil)
Index.new(uid, @base_url, @api_key, primary_key, @options)
end

def task_endpoint
@task_endpoint ||= Task.new(@base_url, @api_key, @options)
end
end
end
19 changes: 15 additions & 4 deletions lib/meilisearch/http_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ def http_put(relative_path = '', body = nil, query_params = nil)
)
end

def http_patch(relative_path = '', body = nil, query_params = nil)
send_request(
proc { |path, config| self.class.patch(path, config) },
relative_path,
query_params: query_params,
body: body,
options: @options
)
end

def http_delete(relative_path = '')
send_request(
proc { |path, config| self.class.delete(path, config) },
Expand All @@ -60,10 +70,11 @@ def http_delete(relative_path = '')
private

def build_default_options_headers(api_key = nil)
{
'Content-Type' => 'application/json',
'X-Meili-API-Key' => api_key
}.compact
header = {
brunoocasali marked this conversation as resolved.
Show resolved Hide resolved
'Content-Type' => 'application/json'
}
header = header.merge('Authorization' => "Bearer #{api_key}") unless api_key.nil?
header
end

def merge_options(default_options, added_options = {})
Expand Down
Loading