# How does Elasticsearch process a query?

**URL:** <https://discuss.elastic.co/t/how-does-elasticsearch-process-a-query/191181>\
**Category:** Elasticsearch\
**Created:** [July 18, 2019, 10:51am UTC](https://discuss.elastic.co/t/how-does-elasticsearch-process-a-query/191181 "2019-07-18T10:51:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sourav\_Paul](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sourav_paul/32/45032_2.png) [@Sourav\_Paul](https://discuss.elastic.co/u/Sourav_Paul)\
**Post date:** [July 18, 2019, 10:51am UTC](https://discuss.elastic.co/t/how-does-elasticsearch-process-a-query/191181/1 "2019-07-18T10:51:37Z")

</div>

I want to have understanding on how search is being performed at Elasticsearch, when we place a query?

I know, this is elaborative, but would appreciate if any expert shares deep insight on query processing.

FYI, setup has 1 node, 10 indices and 10 shards in each index.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 18, 2019, 12:07pm UTC](https://discuss.elastic.co/t/how-does-elasticsearch-process-a-query/191181/2 "2019-07-18T12:07:49Z")

</div>

I'd probably first read [https://www.elastic.co/guide/en/elasticsearch/guide/current/distributed-search.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/distributed-search.html). It's old but I think that most of the content is still accurate.

---

<div class="post-metadata">

**Author:** ![Sourav\_Paul](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sourav_paul/32/45032_2.png) [@Sourav\_Paul](https://discuss.elastic.co/u/Sourav_Paul)\
**Post date:** [July 18, 2019, 6:14pm UTC](https://discuss.elastic.co/t/how-does-elasticsearch-process-a-query/191181/3 "2019-07-18T18:14:16Z")

</div>

Thanks David.  
This link is indeed helpful. So here is my understanding after the read and please correct me, if I am wrong. I also have few questions. Would be helpful, if you can share your views on those.

1. When a query arrives, it lands on a node (this node becomes coordinating node for that request).  
**Question 1** : If we have multiple nodes, do we have any Load Balancer component in ES stack, which decides which node is ideal to take the request?
2. Coordinating node creates an empty priority queue and then forwards the request to every shard in the index.  
**Question 2** : If we have multiple indices, does coordinating node forwards the request to all indices parallelly or sequentially?
3. Each shard executes the query locally and adds the results in locally created priority queue.  
**Question 3** : What if multiple queries land on a specific shard, does it create individual queue for each request? How many such parallel queries can be handled by a specific shard?
4. Each shard returns the results to coordinating node, which merges these results into it's own priority queue to produce final set of results.  
**Question 4** : When we have multiple requests coming into ES, does it maintain any queue for these incoming requests?  
**Question 5** : How many parallel threads at the node level and as a whole, ES can spawn to cater these incoming requests? Does ES have any limit?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 19, 2019, 7:59am UTC](https://discuss.elastic.co/t/how-does-elasticsearch-process-a-query/191181/4 "2019-07-19T07:59:21Z")

</div>

> **Question 1** : If we have multiple nodes, do we have any Load Balancer component in ES stack, which decides which node is ideal to take the request?

No. It just goes to the node you connected to.

> Coordinating node creates an empty priority queue and then forwards the request to every shard in the index.

I'd say not to every shard but one copy of each shard of the index.

> **Question 2** : If we have multiple indices, does coordinating node forwards the request to all indices parallelly or sequentially?

In parallel.

> **Question 3** : What if multiple queries land on a specific shard, does it create individual queue for each request? How many such parallel queries can be handled by a specific shard?

Yes and I don't know.

> **Question 4** : When we have multiple requests coming into ES, does it maintain any queue for these incoming requests?

Yes.

> **Question 5** : How many parallel threads at the node level and as a whole, ES can spawn to cater these incoming requests? Does ES have any limit?

I don't really know and yes there are limits. Have a look at [Thread pools | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-threadpool.html)

---

<div class="post-metadata">

**Author:** ![Sourav\_Paul](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sourav_paul/32/45032_2.png) [@Sourav\_Paul](https://discuss.elastic.co/u/Sourav_Paul)\
**Post date:** [July 20, 2019, 12:33pm UTC](https://discuss.elastic.co/t/how-does-elasticsearch-process-a-query/191181/5 "2019-07-20T12:33:16Z")

</div>

Thanks again David for answering my questions. I have few more doubts and will be great, if you can answer them as well when you have time.

- When we have a cluster with multiple nodes, can we have common end point for searches and implement it through Reverse Proxy (like Nginx) - which can eventually be used as Load balancer and ensure less loaded node gets the search request, instead of randomly picked node.

- If you are aware, can you please provide any link or pointers, on how many parallel queries can each shard execute?

- Thanks for sharing the link on thread module.

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [July 20, 2019, 12:53pm UTC](https://discuss.elastic.co/t/how-does-elasticsearch-process-a-query/191181/6 "2019-07-20T12:53:23Z")

</div>

> [@Sourav\_Paul](#):
>
> If you are aware, can you please provide any link or pointers, on how many parallel queries can each shard execute?

There is no simple answer to this question. Searches run on the `search` or `search_throttled` threadpools on each node, but run in multiple phases. They may also contend for resources other than CPU (e.g. heap space or filesystem cache or IO bandwidth) resulting in worse overall performance at higher levels of parallelism. The only way to find a useful answer is by carefully [benchmarking](https://esrally.readthedocs.io/en/stable/) your specific system with your specific data and workload.

---

<div class="post-metadata">

**Author:** ![Sourav\_Paul](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sourav_paul/32/45032_2.png) [@Sourav\_Paul](https://discuss.elastic.co/u/Sourav_Paul)\
**Post date:** [July 20, 2019, 2:04pm UTC](https://discuss.elastic.co/t/how-does-elasticsearch-process-a-query/191181/7 "2019-07-20T14:04:45Z")

</div>

Thanks David. While I do agree with you, to find right set of configurable setting, we need benchmarking with specified data set and workload. My question was more on available configurable settings at the shard level with default values and whether they are tunable or not.

---

<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 17, 2019, 2:04pm UTC](https://discuss.elastic.co/t/how-does-elasticsearch-process-a-query/191181/8 "2019-08-17T14:04:48Z")

</div>

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