Distinct Attribute

The value of a field whose attribute is set as a distinct attribute will always be unique in the returned documents.

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.

When multiple documents have the same value for a distinct attribute, MeiliSearch returns the first one after applying ranking rules. If documents are equivalent in terms of ranking, MeiliSearch returns the first in terms of internal_id.

Example

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).

As shown below, you have 3 documents that contain information about the same jacket. One of the jackets is brown, one is black, and the last one is blue.

  1. [
  2. {
  3. "id": 1,
  4. "description": "Leather jacket",
  5. "brand": "Lee jeans",
  6. "color": "brown",
  7. "product_id": "123456"
  8. },
  9. {
  10. "id": 2,
  11. "description": "Leather jacket",
  12. "brand": "Lee jeans",
  13. "color": "black",
  14. "product_id": "123456"
  15. },
  16. {
  17. "id": 3,
  18. "description": "Leather jacket",
  19. "brand": "Lee jeans",
  20. "color": "blue",
  21. "product_id": "123456"
  22. }
  23. ]

You may want to ignore the different colors of an item. To do so, you can set product_id as a distinctAttribute.

  1. curl
  2. -X POST 'http://localhost:7700/indexes/jackets/settings' \
  3. --data '{ "distinctAttribute": "product_id" }'
  1. client.index('jackets').updateSettings({ distinctAttribute: 'product_id' })
  1. client.index('jackets').update_settings({'distinctAttribute': 'product_id'})
  1. $client->index('jackets')->updateDistinctAttribute('product_id');
  1. client.index('jackets').update_distinct_attribute('product_id')
  1. client.Settings("jackets").UpdateDistinctAttribute("product_id")
  1. let jackets: Index = client.get_index("jackets").await.unwrap();
  2. let progress: Progress = jackets.set_distinct_attribute("product_id").await.unwrap();

By setting product_id as a distinct attribute, search requests will never return more than one jacket with the same product_id.

For this example, querying for lee leather jacket would only return the first document found. The response could look like this:

  1. {
  2. "hits": [
  3. {
  4. "id": 1,
  5. "description": "Leather jacket",
  6. "brand": "Lee jeans",
  7. "color": "brown",
  8. "product_id": "123456"
  9. }
  10. ],
  11. "offset": 0,
  12. "limit": 20,
  13. "nbHits": 1,
  14. "exhaustiveNbHits": false,
  15. "processingTimeMs": 0,
  16. "query": "lee leather jacket"
  17. }