具有多个嵌套属性的映射中的嵌套查询
Nested query in mapping with multiple nested properties
我有一个具有以下映射的索引:
curl -XPUT "http://localhost:9200/index1" -d'
{
"mappings": {
"movie": {
"properties": {
"people": {
"properties": {
"cast": {
"type": "nested",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
}
},
"producers": {
"type": "nested",
"properties": {
"lastName": {
"type": "string"
}
}
}
}
}
}
}
}
}'
然后我放了一个简单的文档:
curl -XPOST "http://localhost:9200/index1/movie/" -d'
{
"title": "The Matrix",
"people": {
"cast": [
{
"firstName": "Keanu",
"lastName": "Reeves"
},
{
"firstName": "Laurence",
"lastName": "Fishburne"
}
]
}
}'
现在,这是有趣的部分。如果我为 firstName
创建嵌套过滤器
领域,它工作得很好。但是,如果我为 lastName
这样做,它就不起作用
根本。似乎因为这个字段出现在两个嵌套属性中,所以我不能
用它们过滤:
作品:
curl -XPOST "http://localhost:9200/index1/movie/_search" -d'
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "people.cast",
"filter": {
"term": {
"firstName": "keanu"
}
}
}
}
}
}
}'
{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"index1","_type":"movie","_id":"AUzX6wLHdI5sfxoODE5E","_score":1.0,"_source":
{
"title": "The Matrix",
"people": {
"cast": [
{
"firstName": "Keanu",
"lastName": "Reeves"
},
{
"firstName": "Laurence",
"lastName": "Fishburne"
}
]
}
}}]}}
不起作用:
curl -XPOST "http://localhost:9200/index1/movie/_search" -d'
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "people.cast",
"filter": {
"term": {
"lastName": "reeves"
}
}
}
}
}
}
}'
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}%
这是一个错误还是我做错了什么?
我真的不明白为什么你的第一个查询有效,但这里是让第二个查询有效的方法:
curl -XPOST "http://localhost:9200/index1/movie/_search" -d'
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "people.cast",
"filter": {
"term": {
"people.cast.lastName": "reeves"
}
}
}
}
}
}
}'
这是我用来测试它的代码:
http://sense.qbox.io/gist/fd9c45768ae441cd08e6754f41e0a7df91fb758e
我有一个具有以下映射的索引:
curl -XPUT "http://localhost:9200/index1" -d'
{
"mappings": {
"movie": {
"properties": {
"people": {
"properties": {
"cast": {
"type": "nested",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
}
},
"producers": {
"type": "nested",
"properties": {
"lastName": {
"type": "string"
}
}
}
}
}
}
}
}
}'
然后我放了一个简单的文档:
curl -XPOST "http://localhost:9200/index1/movie/" -d'
{
"title": "The Matrix",
"people": {
"cast": [
{
"firstName": "Keanu",
"lastName": "Reeves"
},
{
"firstName": "Laurence",
"lastName": "Fishburne"
}
]
}
}'
现在,这是有趣的部分。如果我为 firstName
创建嵌套过滤器
领域,它工作得很好。但是,如果我为 lastName
这样做,它就不起作用
根本。似乎因为这个字段出现在两个嵌套属性中,所以我不能
用它们过滤:
作品:
curl -XPOST "http://localhost:9200/index1/movie/_search" -d'
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "people.cast",
"filter": {
"term": {
"firstName": "keanu"
}
}
}
}
}
}
}'
{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"index1","_type":"movie","_id":"AUzX6wLHdI5sfxoODE5E","_score":1.0,"_source":
{
"title": "The Matrix",
"people": {
"cast": [
{
"firstName": "Keanu",
"lastName": "Reeves"
},
{
"firstName": "Laurence",
"lastName": "Fishburne"
}
]
}
}}]}}
不起作用:
curl -XPOST "http://localhost:9200/index1/movie/_search" -d'
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "people.cast",
"filter": {
"term": {
"lastName": "reeves"
}
}
}
}
}
}
}'
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}%
这是一个错误还是我做错了什么?
我真的不明白为什么你的第一个查询有效,但这里是让第二个查询有效的方法:
curl -XPOST "http://localhost:9200/index1/movie/_search" -d'
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "people.cast",
"filter": {
"term": {
"people.cast.lastName": "reeves"
}
}
}
}
}
}
}'
这是我用来测试它的代码:
http://sense.qbox.io/gist/fd9c45768ae441cd08e6754f41e0a7df91fb758e