Skip to content

Commit

Permalink
Added distinct search param
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre committed Jul 4, 2024
1 parent 4e90e66 commit 805f033
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
14 changes: 14 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,17 @@ update_search_cutoff_1: |-
client.index('movies').update_search_cutoff_ms(150)
reset_search_cutoff_1: |-
client.index('movies').reset_search_cutoff_ms
search_parameter_reference_distinct_1: |-
client.index('INDEX_NAME').search('QUERY TERMS', {
distinct: 'ATTRIBUTE_A'
})
distinct_attribute_guide_filterable_1: |-
client.index('products').update_filterable_attributes([
'product_id',
'sku',
'url'
])
distinct_attribute_guide_distinct_parameter_1: |-
client.index('products').search('white shirt', {
distinct: 'sku'
})
4 changes: 2 additions & 2 deletions spec/meilisearch/index/search/attributes_to_highlight_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
response = index.search('the', attributes_to_highlight: ['title'])
expect(response).to be_a(Hash)
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
expect(response['hits'].count).to eq(3)
expect(response['hits'].count).to eq(4)
expect(response['hits'].first).to have_key('_formatted')
expect(response['hits'].first['_formatted']['title']).to eq('<em>The</em> Hobbit')
end
Expand All @@ -16,7 +16,7 @@
response = index.search('', attributes_to_highlight: ['*'])
expect(response).to be_a(Hash)
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
expect(response['hits'].count).to eq(7)
expect(response['hits'].count).to eq(8)
expect(response['hits'].first).to have_key('_formatted')
end

Expand Down
6 changes: 3 additions & 3 deletions spec/meilisearch/index/search/attributes_to_retrieve_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
response = index.search('the', attributes_to_retrieve: ['title'])
expect(response).to be_a(Hash)
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
expect(response['hits'].count).to eq(3)
expect(response['hits'].count).to eq(4)
expect(response['hits'].first).to have_key('title')
expect(response['hits'].first).not_to have_key('objectId')
expect(response['hits'].first).not_to have_key('genre')
Expand All @@ -17,7 +17,7 @@
response = index.search('the', attributes_to_retrieve: ['title', 'genre'])
expect(response).to be_a(Hash)
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
expect(response['hits'].count).to eq(3)
expect(response['hits'].count).to eq(4)
expect(response['hits'].first).to have_key('title')
expect(response['hits'].first).not_to have_key('objectId')
expect(response['hits'].first).to have_key('genre')
Expand All @@ -27,7 +27,7 @@
response = index.search('the', attributes_to_retrieve: ['*'])
expect(response).to be_a(Hash)
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
expect(response['hits'].count).to eq(3)
expect(response['hits'].count).to eq(4)
expect(response['hits'].first).to have_key('objectId')
expect(response['hits'].first).to have_key('title')
expect(response['hits'].first).to have_key('genre')
Expand Down
19 changes: 19 additions & 0 deletions spec/meilisearch/index/search/distinct_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

RSpec.describe 'MeiliSearch::Index - Distinct search' do
include_context 'search books with genre'

before do
index.update_filterable_attributes(['genre']).await
end

it 'does a search without distinct' do
response = index.search('harry potter')
expect(response['hits'].count).to eq(2)
end

it 'does a custom search with distinct' do
response = index.search('harry potter', { distinct: 'genre' })
expect(response['hits'].count).to eq(1)
end
end
2 changes: 1 addition & 1 deletion spec/meilisearch/index/search/q_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
response = index.search('coco "harry"')
expect(response).to be_a(Hash)
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
expect(response['hits'].count).to eq(1)
expect(response['hits'].count).to eq(2)
expect(response['hits'].first['objectId']).to eq(4)
expect(response['hits'].first).not_to have_key('_formatted')
end
Expand Down
13 changes: 7 additions & 6 deletions spec/support/books_contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
let(:index) { client.index('books') }
let(:documents) do
[
{ objectId: 123, title: 'Pride and Prejudice', genre: 'romance' },
{ objectId: 456, title: 'Le Petit Prince', genre: 'adventure' },
{ objectId: 1, title: 'Alice In Wonderland', genre: 'adventure' },
{ objectId: 2, title: 'Le Rouge et le Noir', genre: 'romance' },
{ objectId: 1344, title: 'The Hobbit', genre: 'adventure' },
{ objectId: 4, title: 'Harry Potter and the Half-Blood Prince', genre: 'fantasy' },
{ objectId: 123, title: 'Pride and Prejudice', genre: 'romance' },
{ objectId: 456, title: 'Le Petit Prince', genre: 'adventure' },
{ objectId: 1, title: 'Alice In Wonderland', genre: 'adventure' },
{ objectId: 2, title: 'Le Rouge et le Noir', genre: 'romance' },
{ objectId: 1344, title: 'The Hobbit', genre: 'adventure' },
{ objectId: 4, title: 'Harry Potter and the Half-Blood Prince', genre: 'fantasy' },
{ objectId: 7, title: 'Harry Potter and the Chamber of Secrets', genre: 'fantasy' },
{ objectId: 42, title: 'The Hitchhiker\'s Guide to the Galaxy' }
]
end
Expand Down

0 comments on commit 805f033

Please sign in to comment.