POST cars/_search
{"size":10000,"query":{"bool":{"should":[{"query_string":{"fields":["car_name","car_description"],"query":"*765*"}},{"regexp":{"car_model":"(765)[0-9]*"}}]}}}
Fabricated Result
{"car_name": "98765 Mercedes", "car_model":"98765", "car_description":"Mercedes benz ......"}
We have single source of input for car_name, car_model,car_description to be searched and car_model should be searched only sequentially. When I run this query, it gives me the above result which comes because of car_name field but expectation is that it should not give me any result for 765.
So is there any way I can tell ES query to pick the scripted value of car_name(Without model number prefixed) produced via painless scripting.
Your query is doing what you asked it to: it is matching car name/descript containing "765" at any location within the text (note this is a very expensive query and requires a linear scan of all documents) OR it is matching car model beginning with "765".
Did you mean to use must clauses instead of should (meaning both clauses must match)? I also wonder why you would want both to match: could you just match on the car model? You might also consider indexing several prefixes if you intend to search like this often, to improve performance. Additionally, you can do a prefix query instead of regex on the car model which may perform slightly better (although still not as good as indexing the prefixes you intend to match on).
Thank Ryan for replying here. I can not use must clause as the input can contain car name or description or model.To user we show the car name with out model prefixed so user is always in impression that car name has only characters in it.
My problem is that I can not change the car_name to exclude the car_model prefixed. So if anyhow I can exclude the car model prefixed in car name while querying car name field.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.