0

For the search results of /_search, I would like to get the count of the total records, after applying a condition such that if there are multiple records with the same value in fieldxyz, I would like to count it only one record. For example, here are the full results:

Doc 1 {field_one:'value one' , fieldxyz: 'value four';}

Doc 2 {field_one:'value two' , fieldxyz: 'value five';}

Doc 3 {field_one:'value three' , fieldxyz: 'value four';}

Because 'value four' occurs twice, I would like to count those two records as one, and the final count should be 2.

How can I do that?

Arbind
  • 1
  • Does this answer your question? [Count distinct on elastic search](https://stackoverflow.com/questions/42885532/count-distinct-on-elastic-search) – Sahil Gupta Aug 29 '20 at 06:31

1 Answers1

1

You can use the following elasticsearch cardinality aggregation to get the count of distinct values for a field:

{
  "aggs": {
    "counting": {
      "cardinality": {
        "field": "fieldxyz"
      }
    }
  }
}
Jaycreation
  • 2,029
  • 1
  • 15
  • 30
Ashee
  • 46
  • 5