我有以下格式的数据
{
"mappings": {
"blog": {
"properties": {
"comments": {
"type": "nested",
"properties": {
"subComments": {
"type": "nested"
}
}
}
}
}
}
}
我有多个文档,其中包含类似的数据
{
"blog_post_id": "blog1",
"comments": [
{
"id": "c1",
"user_id": "u1",
"timestamp": 1487781975676,
"value": "CVLA1",
"subComments": [
{
"value": "sub comment 1"
},
{
"value": "sub comment 2"
}
]
},
{
"id": "c2",
"user_id": "u1",
"timestamp": 1487781975686,
"value": "CVLA2",
"subComments": [
{
"value": "sub comment 3"
},
{
"value": "sub comment 4"
}
]
}
]
}
我想匹配具有评论值 CVLA1 的博客文档和具有值“sub comment 2”的 suc 评论。
我写了一个类似的查询
{
"query": {
"nested": {
"path": "comments",
"query": {
"bool": {
"must": [
{
"match": {
"comments.value": "CVLA1"
}
},
{
"nested": {
"path": "comments.subComments",
"query": {
"match": {
"commnets.subComments.value": "sub comment 2"
}
}
}
}
]
}
}
}
}
}
但是这个并没有像预期的那样工作。任何帮助如何在多级嵌套文档的不同级别查询。
最佳答案
您在有关 commnets.subComments.value
的查询中有错字。它应该是 comments.subComments.value
。所以整个查询看起来像这样:
{
"query": {
"nested": {
"path": "comments",
"query": {
"bool": {
"must": [
{
"match": {
"comments.value": "CVLA1"
}
},
{
"nested": {
"path": "comments.subComments",
"query": {
"match": {
"comments.subComments.value": "sub comment 2"
}
}
}
}
]
}
}
}
}
}
我仔细检查过 - 它适合我。
关于elasticsearch - 在不同级别查询多级嵌套文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42832231/