gpt4 book ai didi

elasticsearch - 如何在 ElasticSearch 中按字段值过滤?

转载 作者:行者123 更新时间:2023-11-29 02:52:46 26 4
gpt4 key购买 nike

给定下面的 ElasticSearch 文档:

{
"_id": "3330481",
"_type": "user",
"_source": {
"id": "3330481",
"following": ["1", "2", "3", ... , "15000"]
}
}

对于给定的用户列表 ["50", "51", "52"] 我想检查 ID 为 3330481 的用户关注了哪些。因为她关注了 15000 个用户,我不想得到整个列表,然后在本地检查。

有没有办法只检索相关用户?

最佳答案

我不确定您是否可以从文档中获取子数组。

但是,实现此目的的一种方法是使用嵌套字段。您可以将 following 字段的架构更改为 nested,如下所示。

 {
"id": "3330481",
"following": [
{"userId":"1"}, {"userId":"2"},{"userId": "3"}, ... ]
}

然后你可以使用inner_hits检索数组中的匹配元素,如下所示。

{
"query" : {
"nested" : {
"path" : "following",
"query" : {
"terms" : {"following.userId" : ["50","51","53"]}
},
"inner_hits" : {
"size":0 // fetches all
}
}
}
}

关于elasticsearch - 如何在 ElasticSearch 中按字段值过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36188168/

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