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.

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.

WARNING

If the field does not exist, no error will be thrown.