0

I want to ask that what is equivalent of sql count(dictinct())) in elasticsearch ? I mean how to make the below sql query to elasticsearch query. I tried some aggregation but i didn't find the exact answer.

select count(distinct(unique_field)) where db_date < "..." and db_date > "..."

I have tried the below code but it doesn't give the expected result via sql.

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "city": {
              "value": "bratislava"
            }
          }
        },
        {
          "range": {
            "db_date": {
              "gte": "2020-05-15T00:00:00.000Z",
              "lte": "2020-05-16T00:00:00.000Z"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "types_count": {
      "value_count": {
        "field": "unique_field"
      }
    },
    "group_by_status": {
      "terms": {
        "field": "unique_field"
      }
    }
  }
}

Thanks for answering

Orkun
  • 492
  • 4
  • 16

0 Answers0