Skip to content

Commit

Permalink
Merge #566
Browse files Browse the repository at this point in the history
566: Similar search added (#546) r=brunoocasali a=andre-m-dev

# Pull Request

## Related issue
Fixes #546 

## What does this PR do?
- Get similar documents

## 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: Andre <>
Co-authored-by: André <[email protected]>
  • Loading branch information
meili-bors[bot] and andre-m-dev authored Oct 14, 2024
2 parents 75b78f6 + 67e83a6 commit 4ef82c6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
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

0 comments on commit 4ef82c6

Please sign in to comment.