Distinct Attribute
In such a case, there will never be two, or more, occurrences of the same value of that field in the different documents returned by MeiliSearch.
Suppose you have an e-commerce dataset. For an index that contains information about jackets, you may have several identical items in different variations (color or size).
You may want to ignore the different colors of an item. To do so, you can set product_id
as a distinctAttribute
.
curl
-X POST 'http://localhost:7700/indexes/jackets/settings' \
--data '{ "distinctAttribute": "product_id" }'
client.index('jackets').updateSettings({ distinctAttribute: 'product_id' })
$client->index('jackets')->updateDistinctAttribute('product_id');
client.index('jackets').update_distinct_attribute('product_id')
let jackets: Index = client.get_index("jackets").await.unwrap();
For this example, querying for lee leather jacket
would only return the first document found. The response could look like this:
{
"hits": [
{
"id": 1,
"description": "Leather jacket",
"brand": "Lee jeans",
"product_id": "123456"
],
"offset": 0,
"limit": 20,
"nbHits": 1,
"exhaustiveNbHits": false,
"processingTimeMs": 0,
"query": "lee leather jacket"