# How do I effectively combine query statements?

**URL:** <https://discuss.elastic.co/t/how-do-i-effectively-combine-query-statements/39142>\
**Category:** Elasticsearch\
**Created:** [January 13, 2016, 5:21pm UTC](https://discuss.elastic.co/t/how-do-i-effectively-combine-query-statements/39142 "2016-01-13T17:21:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luhn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luhn/32/7136_2.png) [@luhn](https://discuss.elastic.co/u/luhn)\
**Post date:** [January 13, 2016, 5:21pm UTC](https://discuss.elastic.co/t/how-do-i-effectively-combine-query-statements/39142/1 "2016-01-13T17:21:39Z")

</div>

I'm using Elasticsearch to perform what I assume is a fairly standard search on a database of restaurants.

First, I want to use **one or more** of the following criteria to determine if a document should be included in the search results at all:

- Full text search match or partial match
- Within _n_ miles of the given coordinates.
- Classified as the given cuisine.
- Not marked as deleted
- Mark as verified
- Within the given price range (prices ranked from 1 to 4)

And then I want to take the documents satisfying the criteria and rank them by how well they satisfy the following criteria:

- Not marked as closed.
- Accepts reservations.
- Best if within _n_ miles of the given coordinates, then decreasing rank as we move outward.
- How well the full text search matches.

Perhaps each of these would have a multiplier that I could play around with to find the right mixture.

Now, each one of these criterion are easy enough to do independently. Elasticsearch has a wide variety of queries and filters that can achieve every criterion I want to do. The problem arises when I try to combine them to construct a query. For example, [here's one of the godawful queries that I end up with](https://gist.github.com/luhn/bfa22bbded20be33bdac). It's completely unintelligible and unmaintainable.

I must be missing something here—something I don't grok. How can I combine these criteria into a sane query? Are there any resources on how to achieve things like this?

---

<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:** [January 13, 2016, 7:38pm UTC](https://discuss.elastic.co/t/how-do-i-effectively-combine-query-statements/39142/2 "2016-01-13T19:38:37Z")

</div>

I'd use a `bool` query with `should` clauses.  
And probably compute my own score using `function_score`.

---

<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:** [July 5, 2017, 11:24pm UTC](https://discuss.elastic.co/t/how-do-i-effectively-combine-query-statements/39142/3 "2017-07-05T23:24:50Z")

</div>


