# Query Execution order?

**URL:** <https://discuss.elastic.co/t/query-execution-order/92237>\
**Category:** Elasticsearch\
**Created:** [July 7, 2017, 8:47am UTC](https://discuss.elastic.co/t/query-execution-order/92237 "2017-07-07T08:47:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yaswanth](https://avatars.discourse-cdn.com/v4/letter/y/94ad74/32.png) [@Yaswanth](https://discuss.elastic.co/u/Yaswanth)\
**Post date:** [July 7, 2017, 8:47am UTC](https://discuss.elastic.co/t/query-execution-order/92237/1 "2017-07-07T08:47:03Z")

</div>

Hi,

I have query like this:

```
POST data-*/_search
{ "_source": ["Date","Source", "Name","company"],
"query": {
    "nested": {
      "path": "List",
      "inner_hits": {
       "_source": [
          "university","Name","Code","State","Status","Articles","PIN"
        ]  
      },
"query": {
    "bool": { 
      "must": {
        "term": { "List.PIN":{"value":149}}
      },
      "filter": {
        "terms":{"List.Code":[276,915,319]} }
}
}
}
}
} 

```

I have data-\* indices which contain 1000 documents . I want to know whether \_source will work first or after bool query. Why because if `_source` worked first then it will take 1000 documents content and then remove as per bool query which will take time. Or it will run `_source` after bool query so that it have to take 3 doc's since `List.Code` has 3 id's

Thanks

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [July 7, 2017, 9:14am UTC](https://discuss.elastic.co/t/query-execution-order/92237/2 "2017-07-07T09:14:25Z")

</div>

The `inner_hits` part is executed after the search during the fetch phase so the `_source` is retrieved only for the top hits that match the query.

---

<div class="post-metadata">

**Author:** ![Yaswanth](https://avatars.discourse-cdn.com/v4/letter/y/94ad74/32.png) [@Yaswanth](https://discuss.elastic.co/u/Yaswanth)\
**Post date:** [July 7, 2017, 9:18am UTC](https://discuss.elastic.co/t/query-execution-order/92237/3 "2017-07-07T09:18:46Z")

</div>

Thanks @jimczi

SO, you are saying `_source` will only apply on the hits of bool query. Right?

Correct me if i am wrong

Thanks

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [July 7, 2017, 9:22am UTC](https://discuss.elastic.co/t/query-execution-order/92237/4 "2017-07-07T09:22:46Z")

</div>

Sort of yes since `inner_hits` is applied to each top hits returned in the response. If your search matches 1M documents, it will be applied to the 10 best document returned by the query.

---

<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:** [August 4, 2017, 9:27am UTC](https://discuss.elastic.co/t/query-execution-order/92237/6 "2017-08-04T09:27:12Z")

</div>

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