# Is it possible to write single query for exact match and partial match

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-write-single-query-for-exact-match-and-partial-match/58086>\
**Category:** Elasticsearch\
**Created:** [August 16, 2016, 3:55am UTC](https://discuss.elastic.co/t/is-it-possible-to-write-single-query-for-exact-match-and-partial-match/58086 "2016-08-16T03:55:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tatireddy\_Reddy](https://avatars.discourse-cdn.com/v4/letter/t/b5ac83/32.png) [@Tatireddy\_Reddy](https://discuss.elastic.co/u/Tatireddy_Reddy)\
**Post date:** [August 16, 2016, 3:55am UTC](https://discuss.elastic.co/t/is-it-possible-to-write-single-query-for-exact-match-and-partial-match/58086/1 "2016-08-16T03:55:16Z")

</div>

Hi All,

Am new to elastic search. Can you please let us know is it possible to write single query for exact match and partial match.

My secanrio: if there is no documents found for exact match it has returns the partial match records

THnaks,  
Sagar

---

<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:** [August 16, 2016, 7:05am UTC](https://discuss.elastic.co/t/is-it-possible-to-write-single-query-for-exact-match-and-partial-match/58086/2 "2016-08-16T07:05:35Z")

</div>

You can use a bool query with 2 should clauses. At least one clause has to be satisfied.  
You can boost the exact match clause so results will appear on top.

[https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl-bool-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl-bool-query.html)

HTH

---

<div class="post-metadata">

**Author:** ![Tatireddy\_Reddy](https://avatars.discourse-cdn.com/v4/letter/t/b5ac83/32.png) [@Tatireddy\_Reddy](https://discuss.elastic.co/u/Tatireddy_Reddy)\
**Post date:** [August 23, 2016, 1:13pm UTC](https://discuss.elastic.co/t/is-it-possible-to-write-single-query-for-exact-match-and-partial-match/58086/3 "2016-08-23T13:13:31Z")

</div>

Thanks for the reply. If my first condition returns results I don't want get records from second. Will it be possible

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [August 23, 2016, 1:28pm UTC](https://discuss.elastic.co/t/is-it-possible-to-write-single-query-for-exact-match-and-partial-match/58086/4 "2016-08-23T13:28:28Z")

</div>

> [@Tatireddy\_Reddy](#):
>
> If my first condition returns results I don't want get records from second

No. The normal way to handle this is to put a large boost on the query you'd prefer to match and just sort by score (the default). You can even use a [constant score query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html) to remove the regular scoring and just use the boosts, but I don't think that is a great idea usually.

If you really really want to remove hits that only match the second query you can use [named queries](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-named-queries-and-filters.html) to see which of your queries you matched which hit and do the filtering in your application. It is usually simpler to just use score though.

---

<div class="post-metadata">

**Author:** ![Tatireddy\_Reddy](https://avatars.discourse-cdn.com/v4/letter/t/b5ac83/32.png) [@Tatireddy\_Reddy](https://discuss.elastic.co/u/Tatireddy_Reddy)\
**Post date:** [February 13, 2017, 9:53pm UTC](https://discuss.elastic.co/t/is-it-possible-to-write-single-query-for-exact-match-and-partial-match/58086/5 "2017-02-13T21:53:51Z")

</div>

Thank you

---

<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, 10:03pm UTC](https://discuss.elastic.co/t/is-it-possible-to-write-single-query-for-exact-match-and-partial-match/58086/6 "2017-07-05T22:03:18Z")

</div>


