Skip to content

Commit

Permalink
Merge #540
Browse files Browse the repository at this point in the history
540: Support searchCutoffMs setting r=curquiza a=andre-m-dev

# Pull Request

## Related issue
Fixes #532 

## What does this PR do?
- Support searchCutoffMs setting

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?


Co-authored-by: André Mommert <andre.mommert@simpego.ch>
Co-authored-by: André <1729839+andre-m-dev@users.noreply.github.com>
  • Loading branch information
3 people committed Jul 1, 2024
2 parents 74b5165 + 137829d commit c666696
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 36 deletions.
9 changes: 8 additions & 1 deletion .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
8 changes: 4 additions & 4 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -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: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
# 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: 607

# Offense count: 4
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 421
Max: 430

# Offense count: 1
# Configuration parameters: Max, CountKeywordArgs.
Expand Down
16 changes: 15 additions & 1 deletion lib/meilisearch/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions spec/meilisearch/client/health_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/meilisearch/client/requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/meilisearch/client/token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions spec/meilisearch/index/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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')
Expand Down
4 changes: 0 additions & 4 deletions spec/meilisearch/index/search/q_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 39 additions & 4 deletions spec/meilisearch/index/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -31,7 +32,8 @@
'dictionary',
'nonSeparatorTokens',
'separatorTokens',
'proximityPrecision'
'proximityPrecision',
'searchCutoffMs'
]
end
let(:uid) { random_uid }
Expand All @@ -55,7 +57,8 @@
'dictionary' => [],
'separatorTokens' => [],
'nonSeparatorTokens' => [],
'proximityPrecision' => default_proximity_precision
'proximityPrecision' => default_proximity_precision,
'searchCutoffMs' => default_search_cutoff_ms
)
end

Expand Down Expand Up @@ -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
Expand All @@ -109,7 +113,8 @@
'distinctAttribute' => nil,
'stopWords' => [],
'synonyms' => {},
'proximityPrecision' => default_proximity_precision
'proximityPrecision' => default_proximity_precision,
'searchCutoffMs' => default_search_cutoff_ms
)
end
end
Expand Down Expand Up @@ -740,4 +745,34 @@
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 default value' do
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
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
client.wait_for_task(reset_task['taskUid'])

expect(index.search_cutoff_ms).to eq(default_search_cutoff_ms)
end
end
end
7 changes: 5 additions & 2 deletions spec/meilisearch/models/task_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# 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 }

let(:processing_endpoint) { instance_double(MeiliSearch::Task, task: task_hash.update('status' => 'processing')) }
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 }

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions spec/meilisearch/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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'] }
Expand Down
6 changes: 3 additions & 3 deletions spec/meilisearch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit c666696

Please sign in to comment.