# Conditional query (If matches in field than do this...)

**URL:** https://discuss.elastic.co/t/conditional-query-if-matches-in-field-than-do-this/136978
**Category:** Elasticsearch
**Created:** [June 22, 2018, 6:57am UTC](https://discuss.elastic.co/t/conditional-query-if-matches-in-field-than-do-this/136978 "2018-06-22T06:57:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![creaticious](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/creaticious/32/24784_2.png) [@creaticious](https://discuss.elastic.co/u/creaticious)
#### Post date: [June 22, 2018, 6:57am UTC](https://discuss.elastic.co/t/conditional-query-if-matches-in-field-than-do-this/136978/1 "2018-06-22T06:57:47Z")

</div>

Hey guys,

i've some problems to build a special conditional query. Pretend i have documents a,b,c etc. with a "country"-field and a "description"-field. My query should look in both fields...BUT if the user searches for a country...let's say "Island" (german for Iceland) the query also matches documents that have "Vancouver Island" in the description...of course...

So it would be great if i could say...if the search term matches in country...i assume the user typed a country name and i would only return the documents that have the term in the country field and ignore the description...

Is there anyway to do this in one query and pure elasticsearch respectively?

Thank in advance!!

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [June 22, 2018, 10:24am UTC](https://discuss.elastic.co/t/conditional-query-if-matches-in-field-than-do-this/136978/2 "2018-06-22T10:24:28Z")

</div>

Replaying your requirement:

> "If _any_ document has the value `country:Island` then drop the description query on all other docs

This would not be possible as an in-flight query optimisation in a distributed system because machine 1 while being queried would not know that machine 2 might have just discovered that "Island" is a country in one of its docs.  
I can think of 2 options:

1. In your client code you can have logic that first tries searching strictly for `country:Island` and if there's zero results query again with the sloppier field choices
2. Run sloppy and strict queries simultaneously using a single [multi search](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-multi-search.html) request and see which has what you need.

In practice, most people use [multi\_match](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/query-dsl-multi-match-query.html) queries with settings like `cross_fields` to make sure the docs using the search terms in the most-likely fields are ranked highest. After all, are you even certain that the user searching for "island" didn't want to consider places like Vancouver Island anyway?

---

<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 20, 2018, 10:24am UTC](https://discuss.elastic.co/t/conditional-query-if-matches-in-field-than-do-this/136978/3 "2018-07-20T10:24:29Z")

</div>

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