# Is there a better way to perform this type of query?

**URL:** <https://discuss.elastic.co/t/is-there-a-better-way-to-perform-this-type-of-query/95185>\
**Category:** Elasticsearch\
**Created:** [July 31, 2017, 12:40pm UTC](https://discuss.elastic.co/t/is-there-a-better-way-to-perform-this-type-of-query/95185 "2017-07-31T12:40:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Bourne](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kevin_bourne/32/53053_2.png) [@Kevin\_Bourne](https://discuss.elastic.co/u/Kevin_Bourne)\
**Post date:** [July 31, 2017, 12:40pm UTC](https://discuss.elastic.co/t/is-there-a-better-way-to-perform-this-type-of-query/95185/1 "2017-07-31T12:40:47Z")

</div>

I have come up with a way to perform 2 separate queries to get the data that I want but I was wondering is there a better way, specifically if there is a way I can do this query in a single query.

Here is example data with only 3 fields from a larger document:

## id parentId status

1 100 success  
2 100 success  
3 100 error  
4 200 partial  
5 300 error  
6 300 success

What I ultimately want is the full document of the latest object for each parentId that has a list of specific statuses. The latest object is the one with that has the largest id for each parentId. So in the above if I only wanted the latest objects with a status of success OR partial then I would want the full documents for ids 4 and 6.

My current solution is to do a terms query on parentId, then a max query on the id field to get the ids, which using the above would give me ids 3, 4 and 6. Then I will make a second query to retrieve only those ids but than add a filter to only retrieve where status is success OR partial to return the documents for ids 4 and 6. Is there a way to do this in a single query?

For completion, here is the aggregation I am doing to get the first set of ids, which does work:

```
"aggs": {
    "parentIdAgg": {
        "terms": {
            "field": "parentId"
        },
        "aggs": {
            "idAdd": {
                "max": {
                    "field": "id"
                }
            }
        }
    }
}
```

---

<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:** [August 28, 2017, 12:40pm UTC](https://discuss.elastic.co/t/is-there-a-better-way-to-perform-this-type-of-query/95185/2 "2017-08-28T12:40:49Z")

</div>

Closed on August 28, 2017, 12:40pm UTC

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