Nadi Hassan Hassan
Nadi Hassan Hassan

Reputation: 142

ElasticSearch return conditionals

How do I add conditional queries to elasticsearch POST results such that I can remove certain results based on the hits array ?

Example :

If I have the following documents

{id:1 , status:1} {id:1 , status:2} {id:2 , status:1} {id:2 , status:2} {id:2 , status:3}

I want to return the highest of each status fields :

{id:1 , status:2} {id:2 , status:3}

Upvotes: 0

Views: 28

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

You make an aggregation for this:

{
  "size": 0,
  "aggs": {
    "IDs": {
      "terms": {
        "field": "id",
        "size": 10
      },
      "aggs": {
        "lastStatus": {
          "top_hits": {
            "size": 1,
            "sort": [
              {
                "status": "desc"
              }
            ]
          }
        }
      }
    }
  }
}

Upvotes: 1

Related Questions