# Is there any way we can tell ES query to pick the scripted value for the field specified?

**URL:** <https://discuss.elastic.co/t/is-there-any-way-we-can-tell-es-query-to-pick-the-scripted-value-for-the-field-specified/182019>\
**Category:** Elasticsearch\
**Created:** [May 21, 2019, 2:09pm UTC](https://discuss.elastic.co/t/is-there-any-way-we-can-tell-es-query-to-pick-the-scripted-value-for-the-field-specified/182019 "2019-05-21T14:09:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nav18](https://avatars.discourse-cdn.com/v4/letter/n/a9a28c/32.png) [@Nav18](https://discuss.elastic.co/u/Nav18)\
**Post date:** [May 21, 2019, 2:09pm UTC](https://discuss.elastic.co/t/is-there-any-way-we-can-tell-es-query-to-pick-the-scripted-value-for-the-field-specified/182019/1 "2019-05-21T14:09:09Z")

</div>

Hi. I am using below query in kibana

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.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [May 21, 2019, 5:29pm UTC](https://discuss.elastic.co/t/is-there-any-way-we-can-tell-es-query-to-pick-the-scripted-value-for-the-field-specified/182019/2 "2019-05-21T17:29:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Nav18](https://avatars.discourse-cdn.com/v4/letter/n/a9a28c/32.png) [@Nav18](https://discuss.elastic.co/u/Nav18)\
**Post date:** [May 22, 2019, 6:34am UTC](https://discuss.elastic.co/t/is-there-any-way-we-can-tell-es-query-to-pick-the-scripted-value-for-the-field-specified/182019/3 "2019-05-22T06:34:31Z")

</div>

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.

---

<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:** [June 19, 2019, 6:34am UTC](https://discuss.elastic.co/t/is-there-any-way-we-can-tell-es-query-to-pick-the-scripted-value-for-the-field-specified/182019/4 "2019-06-19T06:34:34Z")

</div>

Closed on June 19, 2019, 6:34am UTC

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