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

Add support of similar search #566

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,8 @@ update_search_cutoff_1: |-
client.index('movies').update_search_cutoff_ms(150)
reset_search_cutoff_1: |-
client.index('movies').reset_search_cutoff_ms
get_similar_post_1: |-
client.index('INDEX_NAME').search_similar_documents('TARGET_DOCUMENT_ID')
search_parameter_reference_ranking_score_threshold_1: |-
client.index('INDEX_NAME').search('badman', {
rankingScoreThreshold: 0.2
Expand Down
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Metrics/BlockLength:
# Offense count: 4
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 433
Max: 441

# Offense count: 1
# Configuration parameters: Max, CountKeywordArgs.
Expand Down
8 changes: 8 additions & 0 deletions lib/meilisearch/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ def search(query, options = {})
response
end

# document_id: Identifier of the target document
def search_similar_documents(document_id, **options)
options.merge!(id: document_id)
options = Utils.transform_attributes(options)

http_post("/indexes/#{@uid}/similar", options)
end

### FACET SEARCH

def facet_search(name, query = '', **options)
Expand Down
54 changes: 54 additions & 0 deletions spec/meilisearch/index/search/similar_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

RSpec.describe 'MeiliSearch::Index - Search for similar documents' do
let(:new_index) { client.index('similar_test_search') }

before do
client.create_index('similar_test_search').await
end

it 'requires document_id parameter' do
expect { new_index.search_similar_documents }.to raise_error ArgumentError
end

it 'does a search for similar documents' do
enable_vector_store(true)

documents = [
{
title: 'Shazam!',
release_year: 2019,
id: '287947',
_vectors: { 'manual' => [0.8, 0.4, -0.5] }
},
{
title: 'Captain Marvel',
release_year: 2019,
id: '299537',
_vectors: { 'manual' => [0.6, 0.8, -0.2] }
},
{
title: 'How to Train Your Dragon: The Hidden World',
release_year: 2019,
id: '166428',
_vectors: { 'manual' => [0.7, 0.7, -0.4] }
}
]

new_index.update_settings(
embedders: {
'manual' => {
source: 'userProvided',
dimensions: 3
}
}
).await

new_index.add_documents(documents).await

response = new_index.search_similar_documents('287947')

expect(response['hits']).not_to be_empty
expect(response['estimatedTotalHits']).not_to be_nil
end
end