# Filter be scripted field

**URL:** https://discuss.elastic.co/t/filter-be-scripted-field/138950
**Category:** Elasticsearch
**Created:** [July 6, 2018, 3:35pm UTC](https://discuss.elastic.co/t/filter-be-scripted-field/138950 "2018-07-06T15:35:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![nuketro0p3r](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nuketro0p3r/32/32584_2.png) [@nuketro0p3r](https://discuss.elastic.co/u/nuketro0p3r)
#### Post date: [July 6, 2018, 3:35pm UTC](https://discuss.elastic.co/t/filter-be-scripted-field/138950/1 "2018-07-06T15:35:14Z")

</div>

Is it possible to filter by a scripted field? If so, how would the query look like?

I tried using the scripted field in a post\_filter but it dies silently (0 matches)

The goal is to derive a new field as a predicate based on an existing field, and then filter on it based a condition...

GET /concession\_test/\_search  
{  
"\_source": ["ref\_nr","material\_text\_en"],  
"script\_fields":{  
"test\_pred\_1":{  
"script":{  
"lang":"painless",  
"source":"if(params.\_source.material\_text\_en.contains("TASK\_1") || params.\_source.material\_text\_en.contains("QUEUE\_1") || params.\_source.material\_text\_en.contains("TEST\_1")){ return true } else { return false }"  
}  
}  
},  
"post\_filter": {  
"term": { "test\_pred\_1": true }  
}  
}

Thanks! 🙂

---

<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: [July 6, 2018, 6:33pm UTC](https://discuss.elastic.co/t/filter-be-scripted-field/138950/2 "2018-07-06T18:33:30Z")

</div>

script fields are only additional fields added to the top N results. They are not run for every document potentially matching the query. You would need to copy the script. However, I strongly discourage that in this case as you are using `_source`. Instead, use queries. For example, a bool query with should match clauses looking for `TEST_1` or `QUEUE_1` or `TASK_1` should be equivalent?

---

<div class="post-metadata">

### Author: ![nuketro0p3r](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nuketro0p3r/32/32584_2.png) [@nuketro0p3r](https://discuss.elastic.co/u/nuketro0p3r)
#### Post date: [July 7, 2018, 9:43am UTC](https://discuss.elastic.co/t/filter-be-scripted-field/138950/3 "2018-07-07T09:43:53Z")

</div>

Do you mean doing a script in a query context?

I had to use \_source as the fielddata is off for our main server. Getting it changed is too much work 😃

---

<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: [August 4, 2018, 9:43am UTC](https://discuss.elastic.co/t/filter-be-scripted-field/138950/4 "2018-08-04T09:43:53Z")

</div>

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