# How to query elasticsearch based on response

**URL:** <https://discuss.elastic.co/t/how-to-query-elasticsearch-based-on-response/72638>\
**Category:** Elasticsearch\
**Created:** [January 24, 2017, 2:02pm UTC](https://discuss.elastic.co/t/how-to-query-elasticsearch-based-on-response/72638 "2017-01-24T14:02:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adhikz](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@adhikz](https://discuss.elastic.co/u/adhikz)\
**Post date:** [January 24, 2017, 2:02pm UTC](https://discuss.elastic.co/t/how-to-query-elasticsearch-based-on-response/72638/1 "2017-01-24T14:02:48Z")

</div>

Hi, Im trying to query elasticsearch to do the following :

**Data going into Elastic**

```
Order Number	Product Name
order1 Chicken    
order2 Banana
order3 Chicken   
order1 Cucumber   
order2 Chicken
order3 Apples
order1 Flour
order2 Rice
order3 Nuts

```

This is the Query im using to get a Product Name

```
{
    "query" : {
        "match" : {
        "ProductName" : "Chicken"
}

```

Result : All orders that contain Chicken

```
    Order Number	Product Name
    order1 Chicken          
    order3 Chicken
    order2 Chicken

```

**What I would like to achieve:**  
If the above orders have Chicken in it then give me all the other Products that was also purchased with Chicken.

```
Order Number Product Name
order1 Cucumber
order2 Banana
order3 Apples
order1 Flour
order2 Rice
order3 Nuts

```

How do i go about constructing a query to do this ? please help i am very new to elasticsearch.

- Do i require to Run the first query get all orders that contain Chicken then extract the order numbers and run another query ?

---

<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:** [January 29, 2017, 8:21am UTC](https://discuss.elastic.co/t/how-to-query-elasticsearch-based-on-response/72638/2 "2017-01-29T08:21:30Z")

</div>

You need to do two queries, and issue the second one from your client. You cannot chain queries like this natively.

---

<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:** [February 26, 2017, 8:21am UTC](https://discuss.elastic.co/t/how-to-query-elasticsearch-based-on-response/72638/3 "2017-02-26T08:21:56Z")

</div>

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