Skip to content

Commit 4ef82c6

Browse files
Merge #566
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]>
2 parents 75b78f6 + 67e83a6 commit 4ef82c6

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

.code-samples.meilisearch.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,8 @@ update_search_cutoff_1: |-
653653
client.index('movies').update_search_cutoff_ms(150)
654654
reset_search_cutoff_1: |-
655655
client.index('movies').reset_search_cutoff_ms
656+
get_similar_post_1: |-
657+
client.index('INDEX_NAME').search_similar_documents('TARGET_DOCUMENT_ID')
656658
search_parameter_reference_ranking_score_threshold_1: |-
657659
client.index('INDEX_NAME').search('badman', {
658660
rankingScoreThreshold: 0.2

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Metrics/BlockLength:
1515
# Offense count: 4
1616
# Configuration parameters: CountComments, CountAsOne.
1717
Metrics/ClassLength:
18-
Max: 433
18+
Max: 441
1919

2020
# Offense count: 1
2121
# Configuration parameters: Max, CountKeywordArgs.

lib/meilisearch/index.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,14 @@ def search(query, options = {})
271271
response
272272
end
273273

274+
# document_id: Identifier of the target document
275+
def search_similar_documents(document_id, **options)
276+
options.merge!(id: document_id)
277+
options = Utils.transform_attributes(options)
278+
279+
http_post("/indexes/#{@uid}/similar", options)
280+
end
281+
274282
### FACET SEARCH
275283

276284
def facet_search(name, query = '', **options)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe 'MeiliSearch::Index - Search for similar documents' do
4+
let(:new_index) { client.index('similar_test_search') }
5+
6+
before do
7+
client.create_index('similar_test_search').await
8+
end
9+
10+
it 'requires document_id parameter' do
11+
expect { new_index.search_similar_documents }.to raise_error ArgumentError
12+
end
13+
14+
it 'does a search for similar documents' do
15+
enable_vector_store(true)
16+
17+
documents = [
18+
{
19+
title: 'Shazam!',
20+
release_year: 2019,
21+
id: '287947',
22+
_vectors: { 'manual' => [0.8, 0.4, -0.5] }
23+
},
24+
{
25+
title: 'Captain Marvel',
26+
release_year: 2019,
27+
id: '299537',
28+
_vectors: { 'manual' => [0.6, 0.8, -0.2] }
29+
},
30+
{
31+
title: 'How to Train Your Dragon: The Hidden World',
32+
release_year: 2019,
33+
id: '166428',
34+
_vectors: { 'manual' => [0.7, 0.7, -0.4] }
35+
}
36+
]
37+
38+
new_index.update_settings(
39+
embedders: {
40+
'manual' => {
41+
source: 'userProvided',
42+
dimensions: 3
43+
}
44+
}
45+
).await
46+
47+
new_index.add_documents(documents).await
48+
49+
response = new_index.search_similar_documents('287947')
50+
51+
expect(response['hits']).not_to be_empty
52+
expect(response['estimatedTotalHits']).not_to be_nil
53+
end
54+
end

0 commit comments

Comments
 (0)