gpt4 book ai didi

elasticsearch - Elasticsearch-条件嵌套获取

转载 作者:行者123 更新时间:2023-12-03 01:50:37 24 4
gpt4 key购买 nike

我有索引映射:

{
"dev.directory.3" : {
"mappings" : {
"profile" : {
"properties" : {
"email" : {
"type" : "string",
"index" : "not_analyzed"
},
"events" : {
"type" : "nested",
"properties" : {
"id" : {
"type" : "integer"
},
"name" : {
"type" : "string",
"index" : "not_analyzed"
},
}
}
}
}
}
}
}

与数据:
"hits" : [ {
"_index" : "dev.directory.3",
"_type" : "profile",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"email" : "test@dummy.com",
"events" : [
{
"id" : 111,
"name" : "ABC",
},
{
"id" : 222,
"name" : "DEF",
}
],
}
}]

我只想过滤匹配的嵌套元素,而不是返回所有 events数组-这在ES中可能吗?

查询示例:
{
"nested" : {
"path" : "events",
"query" : {
"bool" : {
"filter" : [
{ "match" : { "events.id" : 222 } },
]
}
}
}
}

例如。如果我查询events.id = 222,则返回的结果列表上应该只有一个元素。

什么样的策略才能最好地实现这种要求?

最佳答案

您可以使用inner_hits仅获取与查询匹配的嵌套记录。

{
"query": {
"nested": {
"path": "events",
"query": {
"bool": {
"filter": [
{
"match": {
"events.id": 222
}
}
]
}
},
"inner_hits": {}
}
},
"_source": false
}

我也排除了仅获得 嵌套匹配的的来源

关于elasticsearch - Elasticsearch-条件嵌套获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40749924/

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