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

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.

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

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

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