Skip to content

Commit 5d4e3ed

Browse files
Merge #551
551: Add code samples and tests for distinct search parameter r=curquiza a=andre-m-dev # Pull Request ## Related issue Fixes #547 ## What does this PR do? - Distinct search parameter added ## 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 <>
2 parents 4e90e66 + 805f033 commit 5d4e3ed

File tree

6 files changed

+46
-12
lines changed

6 files changed

+46
-12
lines changed

.code-samples.meilisearch.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,3 +653,17 @@ 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+
search_parameter_reference_distinct_1: |-
657+
client.index('INDEX_NAME').search('QUERY TERMS', {
658+
distinct: 'ATTRIBUTE_A'
659+
})
660+
distinct_attribute_guide_filterable_1: |-
661+
client.index('products').update_filterable_attributes([
662+
'product_id',
663+
'sku',
664+
'url'
665+
])
666+
distinct_attribute_guide_distinct_parameter_1: |-
667+
client.index('products').search('white shirt', {
668+
distinct: 'sku'
669+
})

spec/meilisearch/index/search/attributes_to_highlight_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
response = index.search('the', attributes_to_highlight: ['title'])
88
expect(response).to be_a(Hash)
99
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
10-
expect(response['hits'].count).to eq(3)
10+
expect(response['hits'].count).to eq(4)
1111
expect(response['hits'].first).to have_key('_formatted')
1212
expect(response['hits'].first['_formatted']['title']).to eq('<em>The</em> Hobbit')
1313
end
@@ -16,7 +16,7 @@
1616
response = index.search('', attributes_to_highlight: ['*'])
1717
expect(response).to be_a(Hash)
1818
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
19-
expect(response['hits'].count).to eq(7)
19+
expect(response['hits'].count).to eq(8)
2020
expect(response['hits'].first).to have_key('_formatted')
2121
end
2222

spec/meilisearch/index/search/attributes_to_retrieve_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
response = index.search('the', attributes_to_retrieve: ['title'])
88
expect(response).to be_a(Hash)
99
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
10-
expect(response['hits'].count).to eq(3)
10+
expect(response['hits'].count).to eq(4)
1111
expect(response['hits'].first).to have_key('title')
1212
expect(response['hits'].first).not_to have_key('objectId')
1313
expect(response['hits'].first).not_to have_key('genre')
@@ -17,7 +17,7 @@
1717
response = index.search('the', attributes_to_retrieve: ['title', 'genre'])
1818
expect(response).to be_a(Hash)
1919
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
20-
expect(response['hits'].count).to eq(3)
20+
expect(response['hits'].count).to eq(4)
2121
expect(response['hits'].first).to have_key('title')
2222
expect(response['hits'].first).not_to have_key('objectId')
2323
expect(response['hits'].first).to have_key('genre')
@@ -27,7 +27,7 @@
2727
response = index.search('the', attributes_to_retrieve: ['*'])
2828
expect(response).to be_a(Hash)
2929
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
30-
expect(response['hits'].count).to eq(3)
30+
expect(response['hits'].count).to eq(4)
3131
expect(response['hits'].first).to have_key('objectId')
3232
expect(response['hits'].first).to have_key('title')
3333
expect(response['hits'].first).to have_key('genre')
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe 'MeiliSearch::Index - Distinct search' do
4+
include_context 'search books with genre'
5+
6+
before do
7+
index.update_filterable_attributes(['genre']).await
8+
end
9+
10+
it 'does a search without distinct' do
11+
response = index.search('harry potter')
12+
expect(response['hits'].count).to eq(2)
13+
end
14+
15+
it 'does a custom search with distinct' do
16+
response = index.search('harry potter', { distinct: 'genre' })
17+
expect(response['hits'].count).to eq(1)
18+
end
19+
end

spec/meilisearch/index/search/q_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
response = index.search('coco "harry"')
6565
expect(response).to be_a(Hash)
6666
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
67-
expect(response['hits'].count).to eq(1)
67+
expect(response['hits'].count).to eq(2)
6868
expect(response['hits'].first['objectId']).to eq(4)
6969
expect(response['hits'].first).not_to have_key('_formatted')
7070
end

spec/support/books_contexts.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
let(:index) { client.index('books') }
55
let(:documents) do
66
[
7-
{ objectId: 123, title: 'Pride and Prejudice', genre: 'romance' },
8-
{ objectId: 456, title: 'Le Petit Prince', genre: 'adventure' },
9-
{ objectId: 1, title: 'Alice In Wonderland', genre: 'adventure' },
10-
{ objectId: 2, title: 'Le Rouge et le Noir', genre: 'romance' },
11-
{ objectId: 1344, title: 'The Hobbit', genre: 'adventure' },
12-
{ objectId: 4, title: 'Harry Potter and the Half-Blood Prince', genre: 'fantasy' },
7+
{ objectId: 123, title: 'Pride and Prejudice', genre: 'romance' },
8+
{ objectId: 456, title: 'Le Petit Prince', genre: 'adventure' },
9+
{ objectId: 1, title: 'Alice In Wonderland', genre: 'adventure' },
10+
{ objectId: 2, title: 'Le Rouge et le Noir', genre: 'romance' },
11+
{ objectId: 1344, title: 'The Hobbit', genre: 'adventure' },
12+
{ objectId: 4, title: 'Harry Potter and the Half-Blood Prince', genre: 'fantasy' },
13+
{ objectId: 7, title: 'Harry Potter and the Chamber of Secrets', genre: 'fantasy' },
1314
{ objectId: 42, title: 'The Hitchhiker\'s Guide to the Galaxy' }
1415
]
1516
end

0 commit comments

Comments
 (0)