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

**URL:** <https://discuss.elastic.co/t/how-to-write-a-query-using-querys-response-as-part-of-the-query/145523>\
**Category:** Elasticsearch\
**Created:** [August 22, 2018, 9:34am UTC](https://discuss.elastic.co/t/how-to-write-a-query-using-querys-response-as-part-of-the-query/145523 "2018-08-22T09:34:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Satish\_Kumar2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/satish_kumar2/32/34244_2.png) [@Satish\_Kumar2](https://discuss.elastic.co/u/Satish_Kumar2)\
**Post date:** [August 22, 2018, 9:34am UTC](https://discuss.elastic.co/t/how-to-write-a-query-using-querys-response-as-part-of-the-query/145523/1 "2018-08-22T09:34:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [August 22, 2018, 9:39am UTC](https://discuss.elastic.co/t/how-to-write-a-query-using-querys-response-as-part-of-the-query/145523/2 "2018-08-22T09:39:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Satish\_Kumar2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/satish_kumar2/32/34244_2.png) [@Satish\_Kumar2](https://discuss.elastic.co/u/Satish_Kumar2)\
**Post date:** [August 22, 2018, 9:53am UTC](https://discuss.elastic.co/t/how-to-write-a-query-using-querys-response-as-part-of-the-query/145523/3 "2018-08-22T09:53:11Z")

</div>

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.

---

<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:** [September 19, 2018, 9:53am UTC](https://discuss.elastic.co/t/how-to-write-a-query-using-querys-response-as-part-of-the-query/145523/4 "2018-09-19T09:53:21Z")

</div>

Closed on September 19, 2018, 9:53am UTC

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