The Distinct class controls how OpenSearch deduplicates search results by field value. Use it to cap the number of results returned per unique value of an attribute field—for example, limiting each seller to at most two products in a search result list.
Constructors
| Constructor | Signature | Description |
|---|---|---|
| No-argument | Distinct() | Creates a Distinct instance with no initial configuration. |
| Field-initialized | Distinct(String key) | Creates a Distinct instance and sets the deduplication field. |
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
key | String | Yes | — | The attribute field used for deduplication. Must be an attribute field. |
Methods
setKey
Distinct setKey(String key)Sets the attribute field used for deduplication.
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
key | String | Yes | — | The attribute field used for deduplication. Must be an attribute field. |
getKey
String getKey()Returns the attribute field currently set for deduplication.
setDistCount
Distinct setDistCount(int distCount)Sets the number of documents to extract each time.
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
distCount | int | No | 1 | The number of documents extracted each time. |
getDistCount
int getDistCount()Returns the current distCount value.
setDistTimes
Distinct setDistTimes(int distTimes)Sets the number of extraction passes to run.
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
distTimes | int | No | 1 | Number of times distinct extraction is applied. |
getDistTimes
int getDistTimes()Returns the current distTimes value.
setReserved
Distinct setReserved(boolean reserved)Controls whether documents that exceed the distCount limit are kept or discarded from the result set.
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
reserved | Boolean | No | true | true: keep excess documents in the result set. false: discard them. |
Whenreservedisfalse, the discarded documents are removed from the result set. Whether their count is subtracted fromtotalHitsdepends on theupdateTotalHitsetting.
setUpdateTotalHit
Distinct setUpdateTotalHit(boolean updateTotalHit)Controls whether the count of discarded documents is subtracted from totalHits. Takes effect only when reserved is false.
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
updateTotalHit | Boolean | No | false | true: subtract discarded document count from totalHits. false: leave totalHits unchanged. |
Setting updateTotalHit to true makes totalHits reflect the adjusted count, but the result may be inaccurate.
setDistFilter
Distinct setDistFilter(String distFilter)Sets a filter condition to restrict which documents are eligible for distinct extraction.
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
distFilter | String | No | All documents selected | A filter expression. Documents that do not match are excluded from extraction but are still sorted alongside the first group of extracted results. |
Excluded documents are not extracted, but they remain in the result set and are sorted together with the first extraction group.
getDistFilter
String getDistFilter()Returns the current filter condition.
setGrade
Distinct setGrade(double grade)Sets the grade threshold for distinct extraction.
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
grade | double | No | — | The threshold value for distinct extraction. |
getGrade
String getGrade()Returns the current grade threshold as a String.