# Giant Elasticsearch query

**URL:** <https://discuss.elastic.co/t/giant-elasticsearch-query/112692>\
**Category:** Elasticsearch\
**Created:** [December 20, 2017, 5:58pm UTC](https://discuss.elastic.co/t/giant-elasticsearch-query/112692 "2017-12-20T17:58:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sparklegoat](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@sparklegoat](https://discuss.elastic.co/u/sparklegoat)\
**Post date:** [December 20, 2017, 5:58pm UTC](https://discuss.elastic.co/t/giant-elasticsearch-query/112692/1 "2017-12-20T17:58:52Z")

</div>

I have a list of must and must\_not items that I currently have in a giant query but I want to know if this is the best way about the problem.

example of the query:

I have 470 must items and 485 must\_not items that are a whitelist/blacklist type of rules for data. The analytic is built in spark and the data is housed in elastic search. The query I am passing to spark is a query with one of the must followed by all 485 must\_not items.

{"query":{ "bool" : { "must" : {"match" : {"tag":"apple"}}, "must\_not": [{ "match": { "city": "new york" }},{ "match": { "name": "pizza" }},...........]}}}

As you can guess the query itself is rather large and takes around 2 seconds to return the results. I am submitting this type of query for each of the must items so therefore 470 queries passed. This application currently takes around 22 min to complete.

My question - Is this the best way to tackle this problem or is there a way to make it faster and is this even a good problem for elasticsearch at all given the gigantic complex query?

I have previously attempted to preform spark joins with the data after just passing a query with just the must\_not data, which takes far longer than the 470 elastic search individual queries. I used a broadcast hash join because the must data is smaller that the resultant data frame.

Thank you for the help.

---

<div class="post-metadata">

**Author:** ![sparklegoat](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@sparklegoat](https://discuss.elastic.co/u/sparklegoat)\
**Post date:** [December 22, 2017, 3:18pm UTC](https://discuss.elastic.co/t/giant-elasticsearch-query/112692/2 "2017-12-22T15:18:21Z")

</div>

I decided to combine the it into one large query which drastically cut the run time down because the spark - ES overhead did't happen 470 times.

---

<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:** [January 19, 2018, 3:26pm UTC](https://discuss.elastic.co/t/giant-elasticsearch-query/112692/3 "2018-01-19T15:26:31Z")

</div>

Closed on January 19, 2018, 3:26pm UTC

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