Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.9: distinct search parameter #2885

Merged
merged 10 commits into from
Jun 26, 2024
25 changes: 25 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,31 @@ analytics_event_bind_event_1: |-
"objectId": "0",
"position": 0
}'
search_parameter_reference_distinct_1: |-
curl \
-X POST 'http://localhost:7700/indexes/INDEX_NAME/search' \
-H 'Content-Type: application/json' \
--data-binary '{
"q": "QUERY TERMS",
"distinct": "ATTRIBUTE_A"
}'
distinct_attribute_guide_filterable_1: |-
curl \
-X PUT 'http://localhost:7700/indexes/products/settings/filterable-attributes' \
-H 'Content-Type: application/json' \
--data-binary '[
"product_id",
"sku",
"url"
]'
distinct_attribute_guide_distinct_parameter_1: |-
curl \
-X POST 'http://localhost:7700/indexes/products/search' \
-H 'Content-Type: application/json' \
--data-binary '{
"q": "white shirt",
"distinct": "sku"
}'
get_similar_post_1: |-
curl \
-X POST 'http://localhost:7700/indexes/INDEX_NAME/similar' \
Expand Down
18 changes: 18 additions & 0 deletions learn/index_settings/distinct.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ description: Distinct attribute is a field that prevents Meilisearch from return

The distinct attribute is a special, user-designated field. It is most commonly used to prevent Meilisearch from returning a set of several similar documents, instead forcing it to return only one.

You may set a distinct attribute in two ways: using the `distinctAttribute` index setting during configuration, or the `distinct` search parameter at search time.

## Setting a distinct attribute during configuration

`distinctAttribute` is an index setting that configures a default distinct attribute Meilisearch applies to all searches and facet retrievals in that index.

<Capsule intent="warning">
There can be only one `distinctAttribute` per index. Trying to set multiple fields as a `distinctAttribute` will return an error.
</Capsule>
Expand Down Expand Up @@ -77,3 +83,15 @@ After setting the distinct attribute as shown above, querying for `lee leather j
```

For more in-depth information on distinct attribute, consult the [API reference](/reference/api/settings#distinct-attribute).

## Setting a distinct attribute at search time

`distinct` is a search parameter you may add to any search query. It allows you to selectively use distinct attributes depending on the context. `distinct` takes precedence over `distinctAttribute`.

To use an attribute with `distinct`, first add it to the `filterableAttributes` list:

<CodeSamples id="distinct_attribute_guide_filterable_1" />

Then use `distinct` in a search query, specifying one of the configured attributes:

<CodeSamples id="distinct_attribute_guide_distinct_parameter_1" />
14 changes: 14 additions & 0 deletions reference/api/search.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,20 @@ The response shows the facet distribution for `genres` and `rating`. Since `rati

[Learn more about facet distribution in the faceted search guide.](/learn/fine_tuning_results/faceted_search#configuring-and-using-facets)

### Distinct attributes at search time

**Parameter**: `distinct`<br />
**Expected value**: An `attribute` present in the `filterableAttributes` list<br />
**Default value**: `null`

Defines one attribute in the `filterableAttributes` list as a distinct attribute. Distinct attributes indicate documents sharing the same value for the specified field are equivalent and only the most relevant one should be returned in search results.

This behavior is similar to the [`distinctAttribute` index setting](/reference/api/settings#distinct-attribute), but can be configured at search time. `distinctAttribute` acts as a default distinct attribute value you may override with `distinct`.

#### Examples

<CodeSamples id="search_parameter_reference_distinct_1" />

### Attributes to retrieve

**Parameter**: `attributesToRetrieve`<br />
Expand Down