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

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!!

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 request and see which has what you need.

In practice, most people use multi_match 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?

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