elasticsearch - 在嵌套查询中过滤聚合
问题描述
通过这个查询,我确定了最常用的类别。我想获取我识别的情绪值大于 0 的类别的数量。
[
'aggs' => [
'category_sentiment' => [
[
'nested' => [
'path' => 'entities.categories'
],
'aggs' => [
'buckets' => [
'terms' => [
'field' => 'entities.categories.category',
'size' => 100
],
'aggs' => [
'pos' => [
'filter' => [
'range' => [
'sentiment_score' => [ 'gt' => 0 ]
]
]
]
]
]
]
]
]
]
]
我得到的结果
"category_sentiment": {
"doc_count": 8424,
"buckets": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Category 1",
"doc_count": 6614,
"pos": {
"doc_count": 0
}
},
{
"key": "Category 2",
"doc_count": 1217,
"pos": {
"doc_count": 0
}
},
{
"key": "Category 3",
"doc_count": 422,
"pos": {
"doc_count": 0
}
}
]
}
},
所有成分都有情绪值。但是,结果返回 0。
解决方案
因此,根据您的查询,您希望汇总嵌套查询中的内容。
现在,根据情绪得分的位置,您要么必须使用嵌套位置
'range' => ['entities.categories.sentiment_score' => [ 'gt' => 0 ]]
否则你将不得不反向嵌套备份链
"aggs": {
"to_ent": :{
# EITHER :
"reverse_nested":{ } # to go to the top level
"reverse_nested":{ "entities" } # to go to entities level
}
}
然后您可以根据需要继续进行 aggs。
如果您提供示例,我将更新示例。
推荐阅读
- php - 引导切换没有响应
- jdbc - Kafka Connect JDBC Sink 连接器:如何删除没有 NULL 值的记录?
- python - 无法在 Ejabberd 19.09.1 中启动外部身份验证程序
- java - 在背景上设置对象/图像的位置
- azure-devops - 如果我授予某人对 Azure DevOps 存储库的只读访问权限,我可以阻止他们复制和粘贴源吗
- javascript - Nodejs 异步/等待 mysql 查询
- forms - Symfony 4 形式:三个动态选择框
- java - 将值分配给二维数组的元素时出现“']'预期”错误
- python - 具有相同参数的函数的不同输出
- r - Left_join 使用来自第二个数据帧的数据值填充 NA 条目