ElasticsSearch 'Script Filter'


(thale jacobs) #1

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/55fe24a5-86e9-42cb-aaaa-29dcee3557e0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Alexander Reelsen) #2

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 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.
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?utm_medium=email&utm_source=footer
.
For more options, visit 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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAGCwEM8if5%2BMNKw5g0FLFAekj0t_T1O4s0gwcLAhcLDGkVWrhw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


(system) #3