我有以下映射:
PUT /test
{
"mappings": {
"test": {
"properties": {
"parent": {
"type": "nested",
"properties": {
"@id": {
"type": "string",
"index": "not_analyzed"
},
"@type": {
"type": "string"
},
"child": {
"type": "nested",
"properties": {
"@id": {
"type": "string",
"index": "not_analyzed"
},
"subchild": {
"type": "nested",
"properties": {
"@id": {
"type": "string",
"index": "not_analyzed"
},
"hasA": {
"type": "nested",
"properties": {
"@value": {
"type": "string"
}
}
},
"hasB": {
"type": "nested",
"properties": {
"@id": {
"type": "string",
"index": "not_analyzed"
}
}
},
"hasC": {
"type": "nested",
"properties": {
"@id": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}
}
}
}
}
}
以下文件:
POST /test/test/1
{
"parent": {
"@id": "12345",
"@type": "test",
"child": [
{
"@id": "1",
"subchild": [
{
"@id": "1.1",
"hasA": {
"@value": "hasA value"
},
"hasB": {
"@id": "hasB_1"
},
"hasC": {
"@id": "hasC_1"
}
}
]
},
{
"@id": "2",
"subchild": [
{
"@id": "2.1",
"hasA": {
"@value": "hasA value"
},
"hasB": {
"@id": "hasB_2"
},
"hasC": {
"@id": "hasC_2"
}
}
]
}
]
}
}
以下查询:
POST test/test/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "parent.child.subchild.hasB",
"filter": {
"bool": {
"must": [
{
"term": {
"parent.child.subchild.hasB.@id": "hasB_2"
}
}
]
}
},
"_cache": false
}
}
}
}
}
我无法将路径设置为parent.child.subchild,以便我可以在hasB和hasC上匹配,似乎我一次只能选择一个嵌套项。这是我希望能够做到的:
POST test/test/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "parent.child.subchild",
"filter": {
"bool": {
"must": [
{
"term": {
"parent.child.subchild.hasB.@id": "hasB_2"
}
},
{
"term": {
"parent.child.subchild.hasC.@id": "hasC_2"
}
}
]
}
},
"_cache": false
}
}
}
}
}
答案 0 :(得分:1)
你在找这样的东西吗?
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "parent.child.subchild",
"filter": {
"bool": {
"must": [
{
"nested": {
"path": "parent.child.subchild.hasB",
"query": {
"term": {
"parent.child.subchild.hasB.@id": "hasB_2"
}
}
}
},
{
"nested": {
"path": "parent.child.subchild.hasC",
"query": {
"term": {
"parent.child.subchild.hasC.@id": "hasC_2"
}
}
}
}
]
}
},
"_cache": false
}
}
}
}
}
答案 1 :(得分:0)
可以找到查询多级嵌套文档的正确语法here。看看里面的@martijnvg评论。
ES Docs在解释多级嵌套查询方面做得不好。
基本上,您需要将子子嵌套在父级内的子级中,并指定单个path
。您将需要三个嵌套查询。