我无法将结果集限制为与下面两个kol_tags.scored.name选项和kol_tags.scored.score选项都匹配的文档。
我想匹配的文件,有kol_tags.scored.name的“核心种植者”和kol_tags.scored.score在1到100之间,除非他们也有kol_tags.scored.name的“连接”,其中kol_tags.scored.score是,而不是在35至65。
考虑到以下映射(为简洁而省略的非嵌套字段):
GET /production_users/user/_mapping
{
"user": {
"_all": {
"enabled": false
},
"properties": {
"kol_tags": {
"type": "nested",
"properties": {
"scored": {
"type": "nested",
"properties": {
"name": {
"type": "string",
"index": "not_analyzed",
"omit_norms": true,
"index_options": "docs"
},
"score": {
"type": "integer"
}
}
}
}
}
}
}
}我正在执行以下查询:
{
"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
}
}
}
]
}
]
}
}
}
}通过上面的查询,我得到了与“核心种植者”的kol_tags.scored.name和1到100之间的kol_tags.scored.score匹配的文档,以及在任何范围内都具有"Connectivity“和kol_tags.scored.score的kol_tags.scored.name的。
我需要的是匹配的文件:
kol_tags.scored.name和1到100之间的kol_tags.scored.scorekol_tags.scored.name和35到65之间的kol_tags.scored.scorekol_tags.scored.name 和 kol_tags.scored.score 小于34且大于66的文档发布于 2014-02-11 18:01:22
您的描述有些含糊不清,但我尝试在这里创建一个可以运行的示例:https://www.found.no/play/gist/8940202 (也嵌入在下面)
我做了几件事:
filtered-query中。顶级filter (在ElasticSearch1.0中重命名为post_filter )只应用于筛选命中,而不是方面。bool而不是and和or,因为过滤器是可选的。这里有更多信息:http://www.elasticsearch.org/blog/all-about-elasticsearch-filter-bitsets/nested放在bool中,这样逻辑就可以得到正确的wrt。嵌套文档与父文档之间应该匹配的内容。must_not来解释您的最后一点。不确定是否可以有两个名为"Connectivity"的子文档,但如果可以的话,这就说明了这一点。如果您只拥有一个,则可以删除must_not。你没有提供任何样本文件,所以我做了一些我认为应该符合你的描述。我不认为你需要两个层次的嵌套。
#!/bin/bash
export ELASTICSEARCH_ENDPOINT="http://localhost:9200"
# Create indexes
curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{
"mappings": {
"type": {
"properties": {
"kol_tags": {
"properties": {
"scored": {
"type": "nested",
"properties": {
"name": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}
}'
# Index documents
curl -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 searches
curl -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
}
}
}
]
}
]
}
}
}
}
'https://stackoverflow.com/questions/21691882
复制相似问题