From a770f457fbb2cbf1c3c6aeeae9f9696c90f969bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Mommert?= Date: Sat, 1 Jun 2024 16:26:35 +0200 Subject: [PATCH 1/7] Implement #532 (searchCutoffMs settings) --- .code-samples.meilisearch.yaml | 9 +++++- lib/meilisearch/index.rb | 14 +++++++++ spec/meilisearch/index/settings_spec.rb | 40 ++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 24a0cab1..357515a0 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -190,7 +190,8 @@ update_settings_1: |- }, faceting: { max_values_per_facet: 200 - } + }, + search_cutoff_ms: 150 }) reset_settings_1: |- client.index('movies').reset_settings @@ -642,3 +643,9 @@ update_proximity_precision_settings_1: |- client.index('books').update_proximity_precision('byAttribute') reset_proximity_precision_settings_1: |- client.index('books').reset_proximity_precision +get_search_cutoff_1: |- + client.index('movies').search_cutoff_ms +update_search_cutoff_1: |- + client.index('movies').update_search_cutoff_ms(150) +reset_search_cutoff_1: |- + client.index('movies').reset_search_cutoff_ms diff --git a/lib/meilisearch/index.rb b/lib/meilisearch/index.rb index 351b988d..1b1ae88f 100644 --- a/lib/meilisearch/index.rb +++ b/lib/meilisearch/index.rb @@ -591,5 +591,19 @@ def update_proximity_precision(proximity_precision_attribute) def reset_proximity_precision http_delete("/indexes/#{@uid}/settings/proximity-precision") end + + ### SETTINGS - SEARCH CUTOFF MS + + def search_cutoff_ms + http_get("/indexes/#{@uid}/settings/search-cutoff-ms") + end + + def update_search_cutoff_ms(search_cutoff_ms_attribute) + http_put("/indexes/#{@uid}/settings/search-cutoff-ms", search_cutoff_ms_attribute) + end + + def reset_search_cutoff_ms + http_delete("/indexes/#{@uid}/settings/search-cutoff-ms") + end end end diff --git a/spec/meilisearch/index/settings_spec.rb b/spec/meilisearch/index/settings_spec.rb index 9ca7cbd3..3086b5da 100644 --- a/spec/meilisearch/index/settings_spec.rb +++ b/spec/meilisearch/index/settings_spec.rb @@ -15,6 +15,7 @@ let(:default_displayed_attributes) { ['*'] } let(:default_pagination) { { maxTotalHits: 1000 } } let(:default_proximity_precision) { 'byWord' } + let(:default_search_cutoff_ms) { nil } let(:settings_keys) do [ 'rankingRules', @@ -31,7 +32,8 @@ 'dictionary', 'nonSeparatorTokens', 'separatorTokens', - 'proximityPrecision' + 'proximityPrecision', + 'searchCutoffMs' ] end let(:uid) { random_uid } @@ -55,7 +57,8 @@ 'dictionary' => [], 'separatorTokens' => [], 'nonSeparatorTokens' => [], - 'proximityPrecision' => default_proximity_precision + 'proximityPrecision' => default_proximity_precision, + 'searchCutoffMs' => default_search_cutoff_ms ) end @@ -97,7 +100,8 @@ distinct_attribute: 'title', stop_words: ['the', 'a'], synonyms: { wow: ['world of warcraft'] }, - proximity_precision: 'byAttribute' + proximity_precision: 'byAttribute', + search_cutoff_ms: 333 ).await task = index.reset_settings @@ -109,7 +113,8 @@ 'distinctAttribute' => nil, 'stopWords' => [], 'synonyms' => {}, - 'proximityPrecision' => default_proximity_precision + 'proximityPrecision' => default_proximity_precision, + 'searchCutoffMs' => default_search_cutoff_ms ) end end @@ -740,4 +745,31 @@ end end end + + context 'On search cutoff' do + let(:index) { client.index(uid) } + let(:default_search_cutoff_ms) { nil } + + before { client.create_index(uid).await } + + it '#search_cutoff_ms gets default value' do + expect(index.search_cutoff_ms).to eq(default_search_cutoff_ms) + end + + it '#update_search_cutoff_ms updates faceting' do + index.update_search_cutoff_ms(800) + + expect(index.search_cutoff_ms).to eq(800) + end + + it '#reset_search_cutoff_ms resets search cutoff ms' do + index.update_search_cutoff_ms(300) + expect(index.search_cutoff_ms).to eq(300) + + reset_task = index.reset_search_cutoff_ms + client.wait_for_task(reset_task['taskUid']) + + expect(index.search_cutoff_ms).to eq(default_search_cutoff_ms) + end + end end From a624f3d30233e10e865c7b5cdd92c43f9ea9d980 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Mommert?= Date: Sat, 1 Jun 2024 16:28:35 +0200 Subject: [PATCH 2/7] Rubocop issues fixed --- lib/meilisearch/index.rb | 2 +- spec/meilisearch/client/requests_spec.rb | 2 +- spec/meilisearch/client/token_spec.rb | 6 +++--- spec/meilisearch/index/base_spec.rb | 12 ++++++------ spec/meilisearch/index/search/q_spec.rb | 4 ---- .../{vector_search.rb => vector_search_spec.rb} | 3 ++- spec/meilisearch/models/task_spec.rb | 7 +++++-- spec/meilisearch/utils_spec.rb | 12 ++++++------ spec/meilisearch_spec.rb | 6 +++--- 9 files changed, 27 insertions(+), 27 deletions(-) rename spec/meilisearch/index/search/{vector_search.rb => vector_search_spec.rb} (81%) diff --git a/lib/meilisearch/index.rb b/lib/meilisearch/index.rb index 1b1ae88f..006c9c6c 100644 --- a/lib/meilisearch/index.rb +++ b/lib/meilisearch/index.rb @@ -386,7 +386,7 @@ def reset_stop_words Models::Task.new(response, task_endpoint) end - ### SETTINGS - DINSTINCT ATTRIBUTE + ### SETTINGS - DISTINCT ATTRIBUTE def distinct_attribute http_get "/indexes/#{@uid}/settings/distinct-attribute" diff --git a/spec/meilisearch/client/requests_spec.rb b/spec/meilisearch/client/requests_spec.rb index 35c01bea..1a7d974d 100644 --- a/spec/meilisearch/client/requests_spec.rb +++ b/spec/meilisearch/client/requests_spec.rb @@ -3,7 +3,7 @@ RSpec.describe 'MeiliSearch::Client requests' do let(:key) { SecureRandom.uuid } - before(:each) do + before do expect(MeiliSearch::Client).to receive(:post) .with(kind_of(String), hash_including(body: "{\"primaryKey\":\"#{key}\",\"uid\":\"#{key}\"}")) .and_call_original diff --git a/spec/meilisearch/client/token_spec.rb b/spec/meilisearch/client/token_spec.rb index 49b5d9ce..9d0322b5 100644 --- a/spec/meilisearch/client/token_spec.rb +++ b/spec/meilisearch/client/token_spec.rb @@ -53,7 +53,7 @@ def initialize(api_key) it 'decodes successfully using @api_key from instance' do expect do JWT.decode token, client_key, true, VERIFY_OPTIONS - end.to_not raise_error + end.not_to raise_error end it 'tries to decode without the right signature raises a error' do @@ -89,7 +89,7 @@ def initialize(api_key) it 'allows generate token with a nil expires_at' do expect do instance.generate_tenant_token('uid', search_rules, expires_at: nil) - end.to_not raise_error + end.not_to raise_error end it 'decodes successfully the expires_at param' do @@ -117,7 +117,7 @@ def initialize(api_key) it 'allows generate token without expires_at' do expect do instance.generate_tenant_token('uid', search_rules) - end.to_not raise_error + end.not_to raise_error end end diff --git a/spec/meilisearch/index/base_spec.rb b/spec/meilisearch/index/base_spec.rb index f4c1508e..0c24a03b 100644 --- a/spec/meilisearch/index/base_spec.rb +++ b/spec/meilisearch/index/base_spec.rb @@ -5,7 +5,7 @@ client.create_index('books').await index = client.fetch_index('books') - expect(index).to be_a(MeiliSearch::Index) + expect(index).to be_a(described_class) expect(index.uid).to eq('books') expect(index.created_at).to be_a(Time) expect(index.created_at).to be_within(60).of(Time.now) @@ -59,7 +59,7 @@ task.await index = client.fetch_index('uid') - expect(index).to be_a(MeiliSearch::Index) + expect(index).to be_a(described_class) expect(index.uid).to eq('uid') expect(index.primary_key).to eq('new_primary_key') expect(index.fetch_primary_key).to eq('new_primary_key') @@ -77,7 +77,7 @@ task.await index = client.fetch_index('books') - expect(index).to be_a(MeiliSearch::Index) + expect(index).to be_a(described_class) expect(index.uid).to eq('books') expect(index.primary_key).to eq('international_standard_book_number') expect(index.fetch_primary_key).to eq('international_standard_book_number') @@ -111,7 +111,7 @@ index = new_client.fetch_index('books') expect(index.options).to eq({ max_retries: 1, timeout: 2, convert_body?: true }) - expect(MeiliSearch::Index).to receive(:get).with( + expect(described_class).to receive(:get).with( "#{URL}/indexes/books", { headers: expected_headers, @@ -139,7 +139,7 @@ index = new_client.fetch_index('books') expect(index.options).to eq(options.merge({ convert_body?: true })) - expect(MeiliSearch::Index).to receive(:get).with( + expect(described_class).to receive(:get).with( "#{URL}/indexes/books", { headers: expected_headers, @@ -194,7 +194,7 @@ task.await index = client.fetch_index('uid') - expect(index).to be_a(MeiliSearch::Index) + expect(index).to be_a(described_class) expect(index.uid).to eq('uid') expect(index.primary_key).to eq('new_primary_key') expect(index.fetch_primary_key).to eq('new_primary_key') diff --git a/spec/meilisearch/index/search/q_spec.rb b/spec/meilisearch/index/search/q_spec.rb index 6791b095..d018e8be 100644 --- a/spec/meilisearch/index/search/q_spec.rb +++ b/spec/meilisearch/index/search/q_spec.rb @@ -72,13 +72,9 @@ context 'with finite pagination params' do it 'responds with specialized fields' do response = index.search('coco', { page: 2, hits_per_page: 2 }) - expect(response.keys).to contain_exactly(*FINITE_PAGINATED_SEARCH_RESPONSE_KEYS) - end - it 'responds with specialized fields' do response = index.search('coco', { page: 2, hitsPerPage: 2 }) - expect(response.keys).to contain_exactly(*FINITE_PAGINATED_SEARCH_RESPONSE_KEYS) end end diff --git a/spec/meilisearch/index/search/vector_search.rb b/spec/meilisearch/index/search/vector_search_spec.rb similarity index 81% rename from spec/meilisearch/index/search/vector_search.rb rename to spec/meilisearch/index/search/vector_search_spec.rb index b3d756cf..c957ceff 100644 --- a/spec/meilisearch/index/search/vector_search.rb +++ b/spec/meilisearch/index/search/vector_search_spec.rb @@ -14,6 +14,7 @@ new_index = client.index('vector_test_search') new_index.add_documents(documents).await - expect(new_index.search('q', vector: [0, 1, 2])['hits']).not_to be_empty + expect(new_index.search(vector: [9, 9, 9])['hits']).to be_empty + expect(new_index.search('All Things Must Pass')['hits']).not_to be_empty end end diff --git a/spec/meilisearch/models/task_spec.rb b/spec/meilisearch/models/task_spec.rb index 33f47c80..f7ca30a9 100644 --- a/spec/meilisearch/models/task_spec.rb +++ b/spec/meilisearch/models/task_spec.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true describe MeiliSearch::Models::Task do + subject { described_class.new task_hash, endpoint } + let(:new_index_uid) { random_uid } let(:task_hash) { client.http_post '/indexes', { 'uid' => new_index_uid } } let(:endpoint) { MeiliSearch::Task.new(URL, MASTER_KEY, client.options) } - subject { described_class.new task_hash, endpoint } - let(:enqueued_endpoint) { instance_double(MeiliSearch::Task, task: task_hash) } let(:enqueued_task) { described_class.new task_hash, enqueued_endpoint } @@ -14,6 +14,7 @@ let(:processing_task) { described_class.new task_hash, processing_endpoint } let(:logger) { instance_double(Logger, warn: nil) } + before { MeiliSearch::Utils.logger = logger } after { MeiliSearch::Utils.logger = nil } @@ -362,6 +363,7 @@ context 'when the task is already finished' do let(:endpoint) { instance_double(MeiliSearch::Task, task: task_hash, cancel_tasks: nil) } + before { task_hash['status'] = 'succeeded' } it 'sends no request' do @@ -374,6 +376,7 @@ context 'when the task is already cancelled' do let(:endpoint) { instance_double(MeiliSearch::Task, task: task_hash, cancel_tasks: nil) } + before { task_hash['status'] = 'cancelled' } it 'sends no request' do diff --git a/spec/meilisearch/utils_spec.rb b/spec/meilisearch/utils_spec.rb index 08da5e8e..ccfde183 100644 --- a/spec/meilisearch/utils_spec.rb +++ b/spec/meilisearch/utils_spec.rb @@ -4,8 +4,8 @@ let(:logger) { instance_double(Logger, warn: nil) } describe '.soft_deprecate' do - before(:each) { described_class.logger = logger } - after(:each) { described_class.logger = nil } + before { described_class.logger = logger } + after { described_class.logger = nil } it 'outputs a warning' do described_class.soft_deprecate('footballs', 'snowballs') @@ -45,8 +45,8 @@ end describe '.transform_attributes' do - before(:each) { described_class.logger = logger } - after(:each) { described_class.logger = nil } + before { described_class.logger = logger } + after { described_class.logger = nil } it 'transforms snake_case into camelCased keys' do data = described_class.transform_attributes({ @@ -143,8 +143,8 @@ end describe '.warn_on_non_conforming_attribute_names' do - before(:each) { described_class.logger = logger } - after(:each) { described_class.logger = nil } + before { described_class.logger = logger } + after { described_class.logger = nil } it 'warns when using camelCase attributes' do attrs = { attributesToHighlight: ['field'] } diff --git a/spec/meilisearch_spec.rb b/spec/meilisearch_spec.rb index d24f8f92..c8c30bdd 100644 --- a/spec/meilisearch_spec.rb +++ b/spec/meilisearch_spec.rb @@ -2,11 +2,11 @@ RSpec.describe MeiliSearch do it 'has a version number' do - expect(MeiliSearch::VERSION).not_to be nil + expect(MeiliSearch::VERSION).not_to be_nil end it 'has a qualified version number' do - expect(MeiliSearch.qualified_version).to eq("Meilisearch Ruby (v#{MeiliSearch::VERSION})") + expect(described_class.qualified_version).to eq("Meilisearch Ruby (v#{MeiliSearch::VERSION})") end it 'raises an exception when it is impossible to connect' do @@ -33,6 +33,6 @@ new_client = MeiliSearch::Client.new(URL, MASTER_KEY) expect(new_client.headers).to have_key('User-Agent') - expect(new_client.headers['User-Agent']).to eq(MeiliSearch.qualified_version) + expect(new_client.headers['User-Agent']).to eq(described_class.qualified_version) end end From 446f48dee65969c88b31b40f938bff5cb3784ed5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Mommert?= Date: Sat, 1 Jun 2024 16:28:48 +0200 Subject: [PATCH 3/7] Test added --- spec/meilisearch/client/health_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/meilisearch/client/health_spec.rb b/spec/meilisearch/client/health_spec.rb index 2aeffa05..4476214d 100644 --- a/spec/meilisearch/client/health_spec.rb +++ b/spec/meilisearch/client/health_spec.rb @@ -11,4 +11,10 @@ it 'is unhealthy when the url is invalid' do expect(wrong_client.healthy?).to be false end + + it 'returns the health information' do + response = client.health + expect(response).to be_a(Hash) + expect(response).to have_key('status') + end end From a0b123b78e8fe0ea585f9a3975353febf53c56ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Mommert?= Date: Sat, 1 Jun 2024 16:31:35 +0200 Subject: [PATCH 4/7] Rubocop --- .rubocop_todo.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 4a6eac57..81e22d34 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,21 +1,21 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2024-02-16 18:01:53 UTC using RuboCop version 1.50.2. +# on 2024-06-01 14:30:58 UTC using RuboCop version 1.63.5. # 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 # versions of RuboCop, may require this file to be generated again. -# Offense count: 63 +# Offense count: 60 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. # AllowedMethods: refine Metrics/BlockLength: - Max: 581 + Max: 605 # Offense count: 4 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 421 + Max: 430 # Offense count: 1 # Configuration parameters: Max, CountKeywordArgs. From 294eeb56e7bccb237593810db0166949ccc9b0b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Mommert?= Date: Sat, 1 Jun 2024 16:55:56 +0200 Subject: [PATCH 5/7] Task mechanic added --- spec/meilisearch/index/settings_spec.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/spec/meilisearch/index/settings_spec.rb b/spec/meilisearch/index/settings_spec.rb index 3086b5da..fbb34c0d 100644 --- a/spec/meilisearch/index/settings_spec.rb +++ b/spec/meilisearch/index/settings_spec.rb @@ -757,13 +757,16 @@ end it '#update_search_cutoff_ms updates faceting' do - index.update_search_cutoff_ms(800) + update_task = index.update_search_cutoff_ms(800) + client.wait_for_task(update_task['taskUid']) expect(index.search_cutoff_ms).to eq(800) end it '#reset_search_cutoff_ms resets search cutoff ms' do - index.update_search_cutoff_ms(300) + update_task = index.update_search_cutoff_ms(300) + client.wait_for_task(update_task['taskUid']) + expect(index.search_cutoff_ms).to eq(300) reset_task = index.reset_search_cutoff_ms From c608bed184fa783282ee2b1868b40acc8fa2069f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Mommert?= Date: Sat, 1 Jun 2024 16:58:19 +0200 Subject: [PATCH 6/7] Rubocop fixed --- .rubocop_todo.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 81e22d34..7e767927 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2024-06-01 14:30:58 UTC using RuboCop version 1.63.5. +# on 2024-06-01 14:58:01 UTC using RuboCop version 1.63.5. # 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 @@ -10,7 +10,7 @@ # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. # AllowedMethods: refine Metrics/BlockLength: - Max: 605 + Max: 607 # Offense count: 4 # Configuration parameters: CountComments, CountAsOne. From a16c40398c616ee741b76f1ff281e12ac4f1b707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <1729839+andre-m-dev@users.noreply.github.com> Date: Mon, 1 Jul 2024 15:12:29 +0200 Subject: [PATCH 7/7] Update spec/meilisearch/index/settings_spec.rb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Clémentine --- spec/meilisearch/index/settings_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/meilisearch/index/settings_spec.rb b/spec/meilisearch/index/settings_spec.rb index fbb34c0d..dc32c9ab 100644 --- a/spec/meilisearch/index/settings_spec.rb +++ b/spec/meilisearch/index/settings_spec.rb @@ -756,7 +756,7 @@ expect(index.search_cutoff_ms).to eq(default_search_cutoff_ms) end - it '#update_search_cutoff_ms updates faceting' do + it '#update_search_cutoff_ms updates default value' do update_task = index.update_search_cutoff_ms(800) client.wait_for_task(update_task['taskUid'])