gpt4 book ai didi

elasticsearch - 在ElasticSearch中是否可以通过逻辑关系执行用户计数/基数?

转载 作者:行者123 更新时间:2023-12-03 01:56:05 31 4
gpt4 key购买 nike

我的用户文档具有以下格式:

{
userId: "<userId>",
userAttributes: [
"<Attribute1>",
"<Attribute2>",
...
"<AttributeN>"
]
}

我希望能够获得回答逻辑语句的唯一用户数,例如 。有多少用户具有attribute1 AND attribute2 OR attribute3?

我已经阅读了 cardinality-aggregation中的基数函数,但是它似乎只适用于单个值,缺少“AND”和“OR”的逻辑功能。

请注意,我有大约1,000,000,000个文档,并且我需要尽快得到结果,这就是为什么我要查看基数估计的原因。

最佳答案

考虑到userAttributesstring的简单数组(在我的情况下进行了分析,但使用单个小写字母表示),该尝试如何?

POST /users/user/_bulk
{"index":{"_id":1}}
{"userId":123,"userAttributes":["xxx","yyy","zzz"]}
{"index":{"_id":2}}
{"userId":234,"userAttributes":["xxx","yyy","aaa"]}
{"index":{"_id":3}}
{"userId":345,"userAttributes":["xxx","yyy","bbb"]}
{"index":{"_id":4}}
{"userId":456,"userAttributes":["xxx","ccc","zzz"]}
{"index":{"_id":5}}
{"userId":567,"userAttributes":["xxx","ddd","ooo"]}

GET /users/user/_search
{
"query": {
"query_string": {
"query": "userAttributes:(((xxx AND yyy) NOT zzz) OR ooo)"
}
},
"aggs": {
"unique_ids": {
"cardinality": {
"field": "userId"
}
}
}
}

它给出了以下内容:
  "hits": [
{
"_index": "users",
"_type": "user",
"_id": "2",
"_score": 0.16471066,
"_source": {
"userAttributes": [
"xxx",
"yyy",
"aaa"
]
}
},
{
"_index": "users",
"_type": "user",
"_id": "3",
"_score": 0.04318809,
"_source": {
"userAttributes": [
"xxx",
"yyy",
"bbb"
]
}
},
{
"_index": "users",
"_type": "user",
"_id": "5",
"_score": 0.021594046,
"_source": {
"userAttributes": [
"xxx",
"ddd",
"ooo"
]
}
}
]

关于elasticsearch - 在ElasticSearch中是否可以通过逻辑关系执行用户计数/基数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36564168/

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