The distinct parameter controls how many variants of a record are shown
when multiple records share the same value for the attribute defined in
attributeForDistinct .
This lets you group and deduplicate search results.
Options
No grouping or deduplication. All matching records are shown.
This is the default.
Returns only the most relevant record from each group.
This is useful when you want to show one result per unique product, article, or listing. Only the most relevant variant is shown for each record group.
Returns the top 2, 3, or 4 records for each group.
Avoid grouping with promoted records ,
as this can lead to wrong nbHits values and broken faceting.
When distinct is greater than 1:
hitsPerPage controls the number of groups .
Up to hitsPerPage × distinct total hits can be returned.
The response field nbHits reflects the number of groups , not total records.
Don’t set distinct to values higher than 4,
as this significantly reduces search performance.
Usage
attributeForDistinct defines how records are grouped.
distinct defines how many records per group are shown.
If attributeForDistinct isn’t set, distinct is ignored.
To ensure accurate facet counts when distinct is applied,
use the afterDistinct
modifier when declaring attributesForFaceting.
Example
C#
Dart
Go
Java
JavaScript
Kotlin
PHP
Python
Ruby
Scala
Swift
var response = await client . SetSettingsAsync (
"INDEX_NAME" ,
new IndexSettings { Distinct = new Distinct ( 1 ), AttributeForDistinct = "url" }
);
final response = await client. setSettings (
indexName : "INDEX_NAME" ,
indexSettings : IndexSettings (
distinct : 1 ,
attributeForDistinct : "url" ,
),
);
response , err := client . SetSettings ( client . NewApiSetSettingsRequest (
"INDEX_NAME" ,
search . NewEmptyIndexSettings (). SetDistinct ( search . Int32AsDistinct ( 1 )). SetAttributeForDistinct ( "url" )))
if err != nil {
// handle the eventual error
panic ( err )
}
UpdatedAtResponse response = client . setSettings (
"INDEX_NAME" ,
new IndexSettings (). setDistinct ( Distinct . of ( 1 )). setAttributeForDistinct ( "url" )
);
const response = await client . setSettings ({
indexName: 'theIndexName' ,
indexSettings: { distinct: 1 , attributeForDistinct: 'url' },
});
var response =
client. setSettings (
indexName = "INDEX_NAME" ,
indexSettings = IndexSettings (distinct = Distinct. of ( 1 ), attributeForDistinct = "url" ),
)
$response = $client -> setSettings (
'INDEX_NAME' ,
[ 'distinct' => 1 ,
'attributeForDistinct' => 'url' ,
],
);
response = client.set_settings(
index_name = "INDEX_NAME" ,
index_settings = {
"distinct" : 1 ,
"attributeForDistinct" : "url" ,
},
)
response = client. set_settings (
"INDEX_NAME" ,
Algolia :: Search :: IndexSettings . new ( distinct: 1 , attribute_for_distinct: "url" )
)
val response = Await .result(
client.setSettings(
indexName = "INDEX_NAME" ,
indexSettings = IndexSettings (
distinct = Some ( Distinct ( 1 )),
attributeForDistinct = Some ( "url" )
)
),
Duration ( 100 , "sec" )
)
let response = try await client. setSettings (
indexName : "INDEX_NAME" ,
indexSettings : IndexSettings ( attributeForDistinct : "url" , distinct : SearchDistinct. int ( 1 ))
)
C#
Go
Java
JavaScript
Kotlin
PHP
Python
Ruby
Scala
Swift
IndexSettings settings = new IndexSettings ();
settings . Distinct = 1 ;
settings . AttributeForDistinct = "url" ;
index . SetSettings ( settings );
res , err := index . SetSettings ( search . Settings {
Distinct : opt . DistinctOf ( 1 ),
AttributeForDistinct : opt . AttributeForDistinct ( "url" ),
})
index . setSettings (
new IndexSettings ()
. setDistinct ( Distinct . of ( 1 ))
. setAttributeForDistinct ( "url" )
);
index
. setSettings ({
distinct: 1 ,
attributeForDistinct: "url" ,
})
. then (() => {
// done
});
val query = settings {
distinct = Distinct ( 1 )
attributeForDistinct = Attribute ( "url" )
}
index. setSettings (query)
$index -> setSettings ([
'distinct' => 1 ,
'attributeForDistinct' => 'url'
]);
index.set_settings({ "distinct" : 1 , "attributeForDistinct" : "url" })
index. set_settings (
{
distinct: 1 ,
attributeForDistinct: "url"
}
)
client.execute {
setSettings of "myIndex" `with` IndexSettings (
distinct = Some ( Distinct .int( 1 ))
attributeForDistinct = Some ( "url" )
)
}
let settings = Settings ()
. set (\. distinct , to : 1 )
. set (\. attributeForDistinct , to : "url" )
index. setSettings (settings) { result in
if case . success ( let response) = result {
print ( "Response: \( response ) " )
}
}