# Script fields based on filters

**URL:** https://discuss.elastic.co/t/script-fields-based-on-filters/3317
**Category:** Elasticsearch
**Created:** [September 9, 2010, 5:35pm UTC](https://discuss.elastic.co/t/script-fields-based-on-filters/3317 "2010-09-09T17:35:11Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei/32/2856_2.png) [@Andrei](https://discuss.elastic.co/u/Andrei)
#### Post date: [September 9, 2010, 5:35pm UTC](https://discuss.elastic.co/t/script-fields-based-on-filters/3317/1 "2010-09-09T17:35:11Z")

</div>

Is it possible to use script fields to add a field to the returned doc  
based on which filter matched it in the first place? For example, in  
my situation I have something like:

...  
"filter": {  
"or": [  
{ "terms": { field1: [term1, term2, term2] } },  
{ "range": { field2: { "from": 0, "to": 100 } }  
]  
}  
...

I'd like to add the result doc to have a field called "foo1" if the  
terms filter matched or "foo2" if the range filter matched.

-Andrei

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [September 9, 2010, 8:59pm UTC](https://discuss.elastic.co/t/script-fields-based-on-filters/3317/2 "2010-09-09T20:59:01Z")

</div>

No, its not possible. Not sure what you are after and why not returning both  
fields is not an option?

On Thu, Sep 9, 2010 at 8:35 PM, Andrei [andrei@zmievski.org](mailto:andrei@zmievski.org) wrote:

> Is it possible to use script fields to add a field to the returned doc  
> based on which filter matched it in the first place? For example, in  
> my situation I have something like:
> 
> ...  
> "filter": {  
> "or": [  
> { "terms": { field1: [term1, term2, term2] } },  
> { "range": { field2: { "from": 0, "to": 100 } }  
> ]  
> }  
> ...
> 
> I'd like to add the result doc to have a field called "foo1" if the  
> terms filter matched or "foo2" if the range filter matched.
> 
> -Andrei

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei/32/2856_2.png) [@Andrei](https://discuss.elastic.co/u/Andrei)
#### Post date: [September 9, 2010, 9:02pm UTC](https://discuss.elastic.co/t/script-fields-based-on-filters/3317/3 "2010-09-09T21:02:39Z")

</div>

I misspoke a bit – I want to return a single field, but with the value  
of it depending on the which filter matched.

I'm trying to emulate something like SQL's CASE statement, to classify  
each document into one of the groups depending on which filter it  
matches.

-Andrei

On Sep 9, 1:59 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> No, its not possible. Not sure what you are after and why not returning both  
> fields is not an option?

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [September 10, 2010, 1:19pm UTC](https://discuss.elastic.co/t/script-fields-based-on-filters/3317/4 "2010-09-10T13:19:43Z")

</div>

mmm, interesting feature. You can't currently do it, but I think I have an  
idea of how it can be done. You will need to be able to "name" your filters,  
and then, when searching, you can say that for each hit, return all the  
named filters it matches. Sounds good?

On Fri, Sep 10, 2010 at 12:02 AM, Andrei [andrei@zmievski.org](mailto:andrei@zmievski.org) wrote:

> I misspoke a bit – I want to return a single field, but with the value  
> of it depending on the which filter matched.
> 
> I'm trying to emulate something like SQL's CASE statement, to classify  
> each document into one of the groups depending on which filter it  
> matches.
> 
> -Andrei
> 
> On Sep 9, 1:59 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > No, its not possible. Not sure what you are after and why not returning  
> > both  
> > fields is not an option?

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei/32/2856_2.png) [@Andrei](https://discuss.elastic.co/u/Andrei)
#### Post date: [September 10, 2010, 4:04pm UTC](https://discuss.elastic.co/t/script-fields-based-on-filters/3317/5 "2010-09-10T16:04:08Z")

</div>

That sounds great. I assume that I would be able to name only some of  
the filters? Also, would I be able to control the name of the field  
that will contain matched filters?

-Andrei

On Sep 10, 6:19 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> mmm, interesting feature. You can't currently do it, but I think I have an  
> idea of how it can be done. You will need to be able to "name" your filters,  
> and then, when searching, you can say that for each hit, return all the  
> named filters it matches. Sounds good?

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [September 10, 2010, 7:30pm UTC](https://discuss.elastic.co/t/script-fields-based-on-filters/3317/6 "2010-09-10T19:30:20Z")

</div>

Yea, you will only get back filters that you actually named when you defined  
them in the search DSL. I was thinking that the name of the field will  
always be the same, something like: "matched\_filters" : ["filter1",  
"filter3].

On Fri, Sep 10, 2010 at 7:04 PM, Andrei [andrei@zmievski.org](mailto:andrei@zmievski.org) wrote:

> That sounds great. I assume that I would be able to name only some of  
> the filters? Also, would I be able to control the name of the field  
> that will contain matched filters?
> 
> -Andrei
> 
> On Sep 10, 6:19 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > mmm, interesting feature. You can't currently do it, but I think I have  
> > an  
> > idea of how it can be done. You will need to be able to "name" your  
> > filters,  
> > and then, when searching, you can say that for each hit, return all the  
> > named filters it matches. Sounds good?

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei/32/2856_2.png) [@Andrei](https://discuss.elastic.co/u/Andrei)
#### Post date: [September 10, 2010, 8:08pm UTC](https://discuss.elastic.co/t/script-fields-based-on-filters/3317/7 "2010-09-10T20:08:51Z")

</div>

Okay, I can do some post-processing on the client end I guess.

On Sep 10, 12:30 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Yea, you will only get back filters that you actually named when you defined  
> them in the search DSL. I was thinking that the name of the field will  
> always be the same, something like: "matched\_filters" : ["filter1",  
> "filter3].

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [September 10, 2010, 8:11pm UTC](https://discuss.elastic.co/t/script-fields-based-on-filters/3317/8 "2010-09-10T20:11:05Z")

</div>

yea, giving the option to name the field as well I think it going too far.  
Care to open an issue since all of this needs to be implemented at one point  
😉

On Fri, Sep 10, 2010 at 11:08 PM, Andrei [andrei@zmievski.org](mailto:andrei@zmievski.org) wrote:

> Okay, I can do some post-processing on the client end I guess.
> 
> On Sep 10, 12:30 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > Yea, you will only get back filters that you actually named when you  
> > defined  
> > them in the search DSL. I was thinking that the name of the field will  
> > always be the same, something like: "matched\_filters" : ["filter1",  
> > "filter3].

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [September 11, 2010, 9:40am UTC](https://discuss.elastic.co/t/script-fields-based-on-filters/3317/9 "2010-09-11T09:40:03Z")

</div>

ok, opened an issue and pushed an implementation for it, I think its a great  
idea to have this capability. Here is the issue:  
[Search API: Allow to name filters and return per hit the filters it matched on · Issue #364 · elastic/elasticsearch · GitHub](http://github.com/elasticsearch/elasticsearch/issues/issue/364).

On Fri, Sep 10, 2010 at 11:11 PM, Shay Banon  
[shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:

> yea, giving the option to name the field as well I think it going too far.  
> Care to open an issue since all of this needs to be implemented at one point  
> 😉
> 
> On Fri, Sep 10, 2010 at 11:08 PM, Andrei [andrei@zmievski.org](mailto:andrei@zmievski.org) wrote:
> 
> > Okay, I can do some post-processing on the client end I guess.
> > 
> > On Sep 10, 12:30 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> > 
> > > Yea, you will only get back filters that you actually named when you  
> > > defined  
> > > them in the search DSL. I was thinking that the name of the field will  
> > > always be the same, something like: "matched\_filters" : ["filter1",  
> > > "filter3].

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei/32/2856_2.png) [@Andrei](https://discuss.elastic.co/u/Andrei)
#### Post date: [September 13, 2010, 9:50pm UTC](https://discuss.elastic.co/t/script-fields-based-on-filters/3317/10 "2010-09-13T21:50:58Z")

</div>

This is great, thank you! I will test soon and report any issues.

-Andrei

On Sep 11, 2:40 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> ok, opened an issue and pushed an implementation for it, I think its a great  
> idea to have this capability. Here is the issue:[Search API: Allow to name filters and return per hit the filters it matched on · Issue #364 · elastic/elasticsearch · GitHub](http://github.com/elasticsearch/elasticsearch/issues/issue/364).
> 
> On Fri, Sep 10, 2010 at 11:11 PM, Shay Banon  
> [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com)wrote:
> 
> > yea, giving the option to name the field as well I think it going too far.  
> > Care to open an issue since all of this needs to be implemented at one point  
> > 😉
> 
> > On Fri, Sep 10, 2010 at 11:08 PM, Andrei [and...@zmievski.org](mailto:and...@zmievski.org) wrote:
> 
> > > Okay, I can do some post-processing on the client end I guess.
> 
> > > On Sep 10, 12:30 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> > > 
> > > > Yea, you will only get back filters that you actually named when you  
> > > > defined  
> > > > them in the search DSL. I was thinking that the name of the field will  
> > > > always be the same, something like: "matched\_filters" : ["filter1",  
> > > > "filter3].

---

<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: [July 6, 2017, 4:19am UTC](https://discuss.elastic.co/t/script-fields-based-on-filters/3317/11 "2017-07-06T04:19:21Z")

</div>


