Elasticsearch: How to use two different matching fields?

I want to do something very similar to and 'filter example , with the exception of terms with' should 'in each, instead of the field types in the example. I came up with the following:

{ "query": { "bool": { "must": [ { "ids": { "type": "foo", "values": [ "fff", "bar", "baz", ] } } ] } }, "filter": { "and": { "filters": [ { "bool": { "should": { "term": { "fruit": [ "orange", "apple", "pear", ] } }, "minimum_should_match": 1 } }, { "bool": { "should": { "term": { "color": [ "red", "yellow", "green" ] } }, "minimum_should_match": 1 } } ] } } } 

However, I get this error:

 [bool] filter does not support [minimum_should_match]; 

Is there any other way to do what I'm trying to do, or am I on the right track? Or is it simply impossible to find elastics?

+4
source share
2 answers

Each sentence of a bool request can contain several sentences. A query of terms ( http://www.elasticsearch.org/guide/reference/query-dsl/terms-query/ ) is an easy way to indicate that a query must match any of the list of terms. Here, which uses terms to say that the fruit must be one of orange, apple, pear and color, must be one of red, yellow, green, in addition to the ids request you had before:

 { "query": { "bool": { "must": [ { "ids": { "type": "foo", "values": [ "fff", "bar", "baz" ] } }, { "terms": { "fruit": [ "orange", "apple","pear" ], "minimum_should_match": 1 } }, { "terms": { "color": [ "red", "yellow", "green" ], "minimum_should_match": 1 } } ] } } } 
+9
source

I think you do not need to specify a bool filter . If I understand correctly what you are trying to accomplish, a term filter with and filter should be enough. So something like this:

 # Delete index # curl -s -X DELETE 'http://localhost:9200/bool-filter-test' ; echo # Create index # curl -s -XPUT 'http://localhost:9200/bool-filter-test/' -d '{ "mappings": { "document": { "properties": { "color": { "type": "string", "index": "not_analyzed" }, "fruit": { "type": "string", "index": "not_analyzed" } } } } }' ; echo # Index some documents # curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/1?pretty=true' -d '{ "fruit" : "apple", "color" : "red" }' ; echo curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/2?pretty=true' -d '{ "fruit" : "apple", "color" : "yellow" }' ; echo curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/3?pretty=true' -d '{ "fruit" : "apple", "color" : "green" }' ; echo curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/4?pretty=true' -d '{ "fruit" : "banana", "color" : "green" }' ; echo curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/5?pretty=true' -d '{ "fruit" : "banana", "color" : "yellow" }' ; echo curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/6?pretty=true' -d '{ "fruit" : "pear", "color" : "green" }' ; echo curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/7?pretty=true' -d '{ "fruit" : "pear", "color" : "yellow" }' ; echo curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/7?pretty=true' -d '{ "fruit" : "pear", "color" : "red" }' ; echo # Refresh index # curl -s -XPOST 'http://localhost:9200/bool-filter-test/_refresh'; echo # This query should return only red apples and pears # curl -s -X POST 'http://localhost:9200/bool-filter-test/_search?pretty' -d '{ "query" : { "match_all" : { } }, "filter" : { "and" : [ { "terms" : { "fruit" : ["apple", "pear"] } }, { "terms" : { "color" : ["red"] } } ] } }' 

You can even specify execution - bool (which according to the documentation) Generates a term filter (which is cached) for each term, and wraps those in a bool filter. The bool filter itself is not cached as it can operate very quickly on the cached term filters. Generates a term filter (which is cached) for each term, and wraps those in a bool filter. The bool filter itself is not cached as it can operate very quickly on the cached term filters.

0
source

All Articles