distinct
0 (no distinct)
'distinct' => 0|1|2|3|4
Can be used in these methods:
search,
setSettings,
searchForFacetValues,
generateSecuredApiKey,
addApiKey,
updateApiKey
search,
set_settings,
search_for_facet_values,
generate_secured_api_key,
add_api_key,
update_api_key
search,
setSettings,
searchForFacetValues,
generateSecuredApiKey,
addApiKey,
updateApiKey
search,
set_settings,
search_for_facet_values,
generate_secured_api_key,
add_api_key,
update_api_key
search,
setSettings,
searchForFacetValues,
generateSecuredApiKey,
addAPIKey,
updateAPIKey
search,
setSettings,
searchForFacetValues,
generateSecuredApiKey,
addApiKey,
updateApiKey
Search,
SetSettings,
SearchForFacetValues,
GenerateSecuredApiKey,
AddApiKey,
UpdateApiKey
Search,
setSettings,
searchForFacetValues,
generateSecuredApiKey,
addApiKey,
updateApiKey
Search,
SetSettings,
SearchForFacetValues,
GenerateSecuredAPIKey,
AddAPIKey,
UpdateAPIKey
search,
setSettings,
search into facet values,
generateSecuredApiKey,
add key,
update key
About this parameter
Deduplicate or group results.
With distinct, you can limit the number of duplicate records: those with the same attributeForDistinct value.
The distinct attribute can be one of the following values:
- Deduplication.
distinctis1ortrue. Only the most relevant variant is shown for each group. - Grouping.
distinctis a number N greater than 1. For each group, the N most relevant variants are shown. - No deduplication (default).
distinctis0orfalse. All matching results are shown.
Usage notes
The distinct attribute works in combination with attributeForDistinct:
attributeForDistinctestablishes groups: all records with the same value ofattributeForDistinctare treated as a group.distinctdetermines how many variants per group are included in the search results.
If attributeForDistinct isn’t set, no error is generated and the distinct setting is ignored.
Faceting
If you’re using distinct in combination with faceting,
use the afterDistinct modifier when declaring your attributesForFaceting.
This lets Algolia compute faceting after deduplication, which results in accurate facet counts.
Grouping
Don’t use grouping (distinct > 1) with promoted records.
If you do, the value for nbHits will be wrong, and faceting won’t work correctly.
When using grouping:
- The
hitsPerPageparameter controls the number of returned groups. For example, ifhitsPerPage=10anddistinct=3, up to 30 records are returned: 10 distinct groups with up to 3 variants per group. This lets you implement pagination with grouping. - The
nbHitsattribute in the response contains the number of groups.
Don’t set distinct to values higher than 4.
High distinct values makes your search a lot slower.
Examples
Set a default distinct mode
The following example uses setSettings to deduplicate records for all searches:
For all records with the same value for the url attribute, only 1 record is shown.
1
2
3
4
$index->setSettings([
'distinct' => 1,
'attributeForDistinct' => 'url'
]);
Override default distinct mode for the current search
The following example overrides the default behavior and turns off deduplication: all matching records are shown.
1
2
3
$results = $index->search('query', [
'distinct' => 0
]);