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

**URL:** <https://discuss.elastic.co/t/can-you-give-me-an-advice-to-make-the-elasticsearch-query/237274>\
**Category:** Elasticsearch\
**Created:** [June 16, 2020, 10:36am UTC](https://discuss.elastic.co/t/can-you-give-me-an-advice-to-make-the-elasticsearch-query/237274 "2020-06-16T10:36:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hyungsun\_lim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hyungsun_lim/32/50790_2.png) [@hyungsun\_lim](https://discuss.elastic.co/u/hyungsun_lim)\
**Post date:** [June 16, 2020, 10:36am UTC](https://discuss.elastic.co/t/can-you-give-me-an-advice-to-make-the-elasticsearch-query/237274/1 "2020-06-16T10:36:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ming1997](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@ming1997](https://discuss.elastic.co/u/ming1997)\
**Post date:** [June 17, 2020, 7:28am UTC](https://discuss.elastic.co/t/can-you-give-me-an-advice-to-make-the-elasticsearch-query/237274/2 "2020-06-17T07:28:55Z")

</div>

You can try this.

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

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 15, 2020, 7:28am UTC](https://discuss.elastic.co/t/can-you-give-me-an-advice-to-make-the-elasticsearch-query/237274/3 "2020-07-15T07:28:59Z")

</div>

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