# ElasticsSearch 'Script Filter'

**URL:** https://discuss.elastic.co/t/elasticssearch-script-filter/18930
**Category:** Elasticsearch
**Created:** [July 28, 2014, 1:23pm UTC](https://discuss.elastic.co/t/elasticssearch-script-filter/18930 "2014-07-28T13:23:38Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![thale\_jacobs](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@thale\_jacobs](https://discuss.elastic.co/u/thale_jacobs)
#### Post date: [July 28, 2014, 1:23pm UTC](https://discuss.elastic.co/t/elasticssearch-script-filter/18930/1 "2014-07-28T13:23:38Z")

</div>

I have a ES search question and I think it can be solved using ES  
scripting, but I was not able to solve it and there may be a better way.

The index has 3 document types, 'province', 'city' and 'neighborhood'  
Here is how the index is created:

curl -s -XPUT 'localhost:9200/test/province/1' -d '{ "province": "Ontario"  
}'

curl -s -XPUT 'localhost:9200/test/city/2' -d '{ "province": "Ontario",  
"city": "Toronto" }'  
curl -s -XPUT 'localhost:9200/test/city/3' -d '{ "province": "Ontario",  
"city" : "Ontario City" }'

curl -s -XPUT 'localhost:9200/test/neighborhood/4' -d '{ "province":  
"Ontario", "city" : "Ontario City", "neighborhood" : "Waterfront" }'  
curl -s -XPUT 'localhost:9200/test/neighborhood/5' -d '{ "province":  
"Ontario", "city" : "Ontario City", "neighborhood" : "Midtown Ontario" }'

The incoming search is for "Ontario". If the document type in the index is  
'province',I want to be able to search the 'province' field, if the  
document type in the index is 'city' I want to search the 'city' field, and  
if the document type in the index is 'neighborhood', I want to search the  
'neighborhood' field. So for the search of 'Ontario', the desired results  
would be to return document:

1,3,and 4 (1 is desired because the docuument type is 'province' and  
'Ontario' matched to the 'province' field, 3 is desired because 'Ontario'  
matched to the 'city', and 4 is desired because 'Ontario' matched to the  
district field.

Here is a simple search that does not produce the desired results:

curl -s -XGET 'localhost:9200/test/\_search?pretty=true' -d '{  
"query":{  
"bool":{  
"should" : [ {  
"multi\_match" : {  
"query" : "Ontario",  
"fields" : ["province", "city", "neighborhood"]  
}  
}]  
}  
}  
}'

The problem is that the search for 'Ontario' get a match on all the docs.  
Is it possible to search a specific field based on the document type?  
Based on some example, I it seems like this might be a good usage for  
"scripting" using a "script filter"?

Thanks for your help.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/55fe24a5-86e9-42cb-aaaa-29dcee3557e0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/55fe24a5-86e9-42cb-aaaa-29dcee3557e0%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [August 4, 2014, 8:53am UTC](https://discuss.elastic.co/t/elasticssearch-script-filter/18930/2 "2014-08-04T08:53:46Z")

</div>

Hey,

there is a types filter, which you could combine with a filtered query,  
which in turn you could combine with a bool query using should or must  
clauses (which could do what you want if I read it correctly). keep in  
mind, that its all just building blocks when creating a query...

--Alex

On Mon, Jul 28, 2014 at 3:23 PM, thale jacobs [thalejacobs@gmail.com](mailto:thalejacobs@gmail.com) wrote:

> I have a ES search question and I think it can be solved using ES  
> scripting, but I was not able to solve it and there may be a better way.
> 
> The index has 3 document types, 'province', 'city' and 'neighborhood'  
> Here is how the index is created:
> 
> curl -s -XPUT 'localhost:9200/test/province/1' -d '{ "province":  
> "Ontario" }'
> 
> curl -s -XPUT 'localhost:9200/test/city/2' -d '{ "province": "Ontario",  
> "city": "Toronto" }'  
> curl -s -XPUT 'localhost:9200/test/city/3' -d '{ "province": "Ontario",  
> "city" : "Ontario City" }'
> 
> curl -s -XPUT 'localhost:9200/test/neighborhood/4' -d '{ "province":  
> "Ontario", "city" : "Ontario City", "neighborhood" : "Waterfront" }'  
> curl -s -XPUT 'localhost:9200/test/neighborhood/5' -d '{ "province":  
> "Ontario", "city" : "Ontario City", "neighborhood" : "Midtown Ontario" }'
> 
> The incoming search is for "Ontario". If the document type in the index  
> is 'province',I want to be able to search the 'province' field, if the  
> document type in the index is 'city' I want to search the 'city' field, and  
> if the document type in the index is 'neighborhood', I want to search the  
> 'neighborhood' field. So for the search of 'Ontario', the desired results  
> would be to return document:
> 
> 1,3,and 4 (1 is desired because the docuument type is 'province' and  
> 'Ontario' matched to the 'province' field, 3 is desired because 'Ontario'  
> matched to the 'city', and 4 is desired because 'Ontario' matched to the  
> district field.
> 
> Here is a simple search that does not produce the desired results:
> 
> curl -s -XGET 'localhost:9200/test/\_search?pretty=true' -d '{  
> "query":{  
> "bool":{  
> "should" : [ {  
> "multi\_match" : {  
> "query" : "Ontario",  
> "fields" : ["province", "city", "neighborhood"]  
> }  
> }]  
> }  
> }  
> }'
> 
> The problem is that the search for 'Ontario' get a match on all the docs.  
> Is it possible to search a specific field based on the document type?  
> Based on some example, I it seems like this might be a good usage for  
> "scripting" using a "script filter"?
> 
> Thanks for your help.
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/55fe24a5-86e9-42cb-aaaa-29dcee3557e0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/55fe24a5-86e9-42cb-aaaa-29dcee3557e0%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/55fe24a5-86e9-42cb-aaaa-29dcee3557e0%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/55fe24a5-86e9-42cb-aaaa-29dcee3557e0%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGCwEM8if5%2BMNKw5g0FLFAekj0t\_T1O4s0gwcLAhcLDGkVWrhw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGCwEM8if5%2BMNKw5g0FLFAekj0t_T1O4s0gwcLAhcLDGkVWrhw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:11am UTC](https://discuss.elastic.co/t/elasticssearch-script-filter/18930/3 "2017-07-06T01:11:08Z")

</div>


