您的描述含糊不清,但我尝试制作一个可运行的示例,该示例应在此处运行:https
:
//www.found.no/play/gist/8940202(也嵌入在下面)
这是我做的几件事:
将过滤器放入
filtered
-query中。仅当您要过滤匹配时才使用顶层filter
(post_filter
在Elasticsearch 1.0中重命名),而不要过滤方面。使用
bool
代替and
和or
,因为过滤器是可缓存的。此处更多信息:http : //www.elasticsearch.org/blog/all-about-elasticsearch-filter-bitsets/而最重要的,把
nested
里面 的bool
,所以逻辑得到正确的WRT。嵌套文档与父文档应该匹配什么。must_not
为您的最后一点添加了一个帐户。不知道您是否可以拥有两个名为name的子文档"Connectivity"
,但是如果可以的话,应该可以解决这个问题。如果您只有一个,可以删除must_not
。
您没有提供任何示例文档,所以我提出了一些我认为适合您的描述的文档。我认为您不需要两个级别的嵌套。
#!/bin/bashexport ELASTICSEARCH_ENDPOINT="http://localhost:9200"# Create indexescurl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{ "mappings": { "type": { "properties": { "kol_tags": { "properties": { "scored": { "type": "nested", "properties": { "name": { "type": "string", "index": "not_analyzed" } } } } } } } }}'# Index documentscurl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '{"index":{"_index":"play","_type":"type"}}{"kol_tags":{"scored":[{"name":"Core Grower","score":36},{"name":"Connectivity","score":42}]}}{"index":{"_index":"play","_type":"type"}}{"kol_tags":{"scored":[{"name":"Connectivity","score":34},{"name":"Connectivity","score":42}]}}{"index":{"_index":"play","_type":"type"}}{"kol_tags":{"scored":[{"name":"Core Grower","score":36}]}}{"index":{"_index":"play","_type":"type"}}{"kol_tags":{"scored":[{"name":"Connectivity","score":36}]}}'# Do searchescurl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '{ "query": { "filtered": { "filter": { "bool": { "should": [ { "nested": { "path": "kol_tags.scored", "filter": { "bool": { "must": [{ "term": { "name": "Core Grower" }},{ "range": { "score": { "gte": 1, "lte": 100 } }} ] } } } }, { "nested": { "path": "kol_tags.scored", "filter": { "bool": { "must": [{ "term": { "name": "Connectivity" }},{ "range": { "score": { "gte": 35, "lte": 65 } }} ] } } } } ], "must_not": [ { "nested": { "path": "kol_tags.scored", "filter": { "bool": { "must": [{ "term": { "name": "Connectivity" }},{ "not": { "range": { "score": { "gte": 35, "lte": 65 } } }} ] } } } } ] } } } }}'curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '{ "filter": { "nested": { "path": "kol_tags.scored", "filter": { "or": [ { "and": [ { "terms": { "kol_tags.scored.name": [ "Core Grower" ] } }, { "range": { "kol_tags.scored.score": { "gte": 1, "lte": 100 } } } ] }, { "and": [ { "terms": { "kol_tags.scored.name": [ "Connectivity" ] } }, { "range": { "kol_tags.scored.score": { "gte": 35, "lte": 65 } } } ] } ] } } }}'


