# Retrieve the last value of each "should" in a bool query

**URL:** <https://discuss.elastic.co/t/retrieve-the-last-value-of-each-should-in-a-bool-query/225634>\
**Category:** Elasticsearch\
**Created:** [March 30, 2020, 9:10am UTC](https://discuss.elastic.co/t/retrieve-the-last-value-of-each-should-in-a-bool-query/225634 "2020-03-30T09:10:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeopoldBriand](https://avatars.discourse-cdn.com/v4/letter/l/97f17d/32.png) [@LeopoldBriand](https://discuss.elastic.co/u/LeopoldBriand)\
**Post date:** [March 30, 2020, 9:10am UTC](https://discuss.elastic.co/t/retrieve-the-last-value-of-each-should-in-a-bool-query/225634/1 "2020-03-30T09:10:54Z")

</div>

Hi, I'm new to using Elastisearch and I'm completely stuck on a query. I've actually written this query that returns different values for all the ingredients.

> ```
> {
> "query": { 
> "bool": { 
> "must": [ 
> { 
> "range": {
> "timestamp": {
> "lte" 1584436490917
> } 
> }
> },
> { 
> "bool": {
> "should": [ 
> {
> "term": {
> "name": "IngredientName1"
> }
> }, 
> {
> "term": { 
> "name": "IngredientName2"
> }
> }
> ]
> }
> }
> ]
> }
> }
> }
> 
> ```

I would like to complete it so that it only returns the last value of each ingredient.  
Is it possible ? How ?  
Thanks for reading.

---

<div class="post-metadata">

**Author:** ![LeopoldBriand](https://avatars.discourse-cdn.com/v4/letter/l/97f17d/32.png) [@LeopoldBriand](https://discuss.elastic.co/u/LeopoldBriand)\
**Post date:** [April 1, 2020, 2:02pm UTC](https://discuss.elastic.co/t/retrieve-the-last-value-of-each-should-in-a-bool-query/225634/2 "2020-04-01T14:02:08Z")

</div>

I found a solution with aggregations top hits so i close the ticket !  
Solution :

```auto
"aggs": {
    "IngredientName": {
        "terms": {
            "field": "name",
            "size": 10
         },
        "aggs": {
            "last_one": {
                "top_hits": {
                    "sort": [{"timestamp": {"order": "desc"}}],
                    "_source": {
                        "includes": ["name", "timestamp", "meta.valueText"]
                    },
                    "size": 1
                }
            }
        }
    }
}

```

---

<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:** [April 29, 2020, 2:14pm UTC](https://discuss.elastic.co/t/retrieve-the-last-value-of-each-should-in-a-bool-query/225634/3 "2020-04-29T14:14:52Z")

</div>

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