How to write a query using query's response as part of the query?

Hi,

I had to write a query that , where field matches the text , ids is one among the keys in the time dictionary ,
like in this case => items = { "id1": "time1", "id2": "time2".....}

{
    "size": 1000,
    "_source": [.., .., .. , ..., "timestamp"],
    "query": {
        "bool": {
            "must": [
                {"match": {"field": "text"}},
                {"terms": {"ids": list(items.keys())}}
            ],
            "filter": [
                {"range": {"timestamp": {"gte": <timestamp of matching id> }}}
            ]
        }
    }
}

I tried the following :

{
    "size": 1000,
    "_source": [.., .., .. , .. , "timestamp"],
    "query": {
        "bool": {
            "must": [
                {"match": {"field": "text"}},
                {"terms": {"ids": list(items.keys())}}
            ],
            "filter": [
                {"range": {"date": {"gte": start_date, "lte": end_date}}}
            ],
           "script": {
                "script": " doc[timestamp'] >= params.param1 ",
                "lang": "painless",
                "params": {
                    "param1": items["doc['ids']"]
                }
        }
    }
}

but it returned improperly configured query.
I am using Elasticsearch 6.3

You will need to do two queries, there's no way to do it natively in Elasticsearch.

I honestly wanted to avoid that as I had to match individual id and check for its equivalent timestamp in a loop.
Thanks for the response, I wanted to know if there are any other way of doing it.

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