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


(Tatireddy Reddy) #1

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


(David Pilato) #2

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

HTH


(Tatireddy Reddy) #3

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


(Nik Everett) #4

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


(Tatireddy Reddy) #5

Thank you


(system) #6