Can you give me an advice to make the elasticsearch query?

Hello, I try to make elasticsearch query to perform below action

I have 4 documents like this

_id : 1
user_id : 12345
number : 1

_id : 2
user_id : 12345
number : 2

_id : 3
user_id : 12345
number : 3

_id : 4
user_id : 45678
number : 1

From the index, i want to get documents which is bigger number with same user_id.

So In this case, when i execute the query, i want to get documents like below:

_id : 3
user_id : 12345
number : 3

_id : 4
user_id : 45678
number : 1

Because _id 3 has big number (3) in the same user_id.
_id 4 is the same reason.

Can you give me an advice to query elasticsearch to get documents?

Thank you.

You can try this.

{
  "query": {},
  "size": 0,
  "aggs": {
    "data": {
      "terms": {
        "field": "user_id",
        "size": "10"
      },
      "aggs": {
        "max_number": {
          "max": {
            "field": "number"
          }
        }
      }
    }
  }
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.