gpt4 book ai didi

elasticsearch - 过滤深层嵌套的项目

转载 作者:行者123 更新时间:2023-12-03 02:00:05 25 4
gpt4 key购买 nike

我有以下映射:

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
}
}
}
}
}

最佳答案

您是否正在寻找这样的东西?

{
"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
}
}
}
}
}

关于elasticsearch - 过滤深层嵌套的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33568639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com