# Cascaded querying in ElasticSearch

**URL:** <https://discuss.elastic.co/t/cascaded-querying-in-elasticsearch/83931>\
**Category:** Elasticsearch\
**Created:** [April 27, 2017, 7:05pm UTC](https://discuss.elastic.co/t/cascaded-querying-in-elasticsearch/83931 "2017-04-27T19:05:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rory](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@Rory](https://discuss.elastic.co/u/Rory)\
**Post date:** [April 27, 2017, 7:05pm UTC](https://discuss.elastic.co/t/cascaded-querying-in-elasticsearch/83931/1 "2017-04-27T19:05:32Z")

</div>

I am not a hundred percent sure what the title for this question should be. But here goes,

I have some data which looks like this:

 ![](https://us1.discourse-cdn.com/elastic/original/3X/6/6/660d920ff3ef42a421d91385225f2ce331270f64.png)

There are 3 columns; ID, Name, FavoriteSoccerPlayer

I can write a query to return all documents where Name = "A" or "C". The query looks like:

```
GET /myindex/mytype/_search
{
  "size": 0,
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "Name": "A"
          }
        },
        {
          "match": {
            "Name": "C"
          }
        }
      ]
    }
  }
}

```

This query returns documents with value of ID column in (1,3,4,6,8,9). The unique list of FavoriteSoccerPlayer returned by this query is ('Ronaldo','Henry','Buffon').

What I want to do next is write a query which returns all documents where the FavoriteSoccerPlayer attribute is ('Ronaldo','Henry','Buffon'). The result of this query should be documents with ID 1,3,4,5,6,8,9,10.

I come from a SQL background, and this is similar to writing a cascaded query. Any help is highly appreciated.

Thanks.

---

<div class="post-metadata">

**Author:** ![Rory](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@Rory](https://discuss.elastic.co/u/Rory)\
**Post date:** [May 4, 2017, 9:22pm UTC](https://discuss.elastic.co/t/cascaded-querying-in-elasticsearch/83931/2 "2017-05-04T21:22:06Z")

</div>

Bump.

---

<div class="post-metadata">

**Author:** ![mmichaels01](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mmichaels01/32/22513_2.png) [@mmichaels01](https://discuss.elastic.co/u/mmichaels01)\
**Post date:** [May 4, 2017, 10:04pm UTC](https://discuss.elastic.co/t/cascaded-querying-in-elasticsearch/83931/3 "2017-05-04T22:04:01Z")

</div>

Have you tried this?

```
GET /myindex/mytype/_search
{
  "size": 10,
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "FavoriteSoccerPlayer": "Ronaldo"
          }
        },
        {
          "match": {
            "FavoriteSoccerPlayer": "Henry"
          },
        {
          "match": {
            "FavoriteSoccerPlayer": "Buffon"
          }
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![Rory](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@Rory](https://discuss.elastic.co/u/Rory)\
**Post date:** [May 5, 2017, 1:19am UTC](https://discuss.elastic.co/t/cascaded-querying-in-elasticsearch/83931/4 "2017-05-05T01:19:43Z")

</div>

I want the final query to run dynamically based on the results of the first query. This query is a static version just for this example.

Thanks.

---

<div class="post-metadata">

**Author:** ![mmichaels01](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mmichaels01/32/22513_2.png) [@mmichaels01](https://discuss.elastic.co/u/mmichaels01)\
**Post date:** [May 5, 2017, 8:51pm UTC](https://discuss.elastic.co/t/cascaded-querying-in-elasticsearch/83931/5 "2017-05-05T20:51:38Z")

</div>

I see what you're saying. A scripted field may be able to get you the desired result here.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html)

If that doesn't work, you may need something external performing the second query based on your first response.

I'm not aware of any out of the box functionality that would work exactly as you describe, however, it's nothing I've ever read about or attempted to do myself.

---

<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:** [June 2, 2017, 9:04pm UTC](https://discuss.elastic.co/t/cascaded-querying-in-elasticsearch/83931/6 "2017-06-02T21:04:09Z")

</div>

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