- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Elasticsearch 版本:2.3.3
基本上,标题说明了一切。如果在第二个嵌套聚合下使用 reverse_nested
,尽管文档似乎通过 reverse_nested
来限定范围(参见最后一个 “doc_count”
字段结果),它后面的聚合以某种方式不起作用。
这里我准备了一个例子——一个文件是一个学生,有入学日期和考试历史。
映射:
{
"mappings": {
"students": {
"properties": {
"name": {
"type": "string"},
"enrollment": {
"type": "date"},
"exam_histories": {
"type": "nested",
"properties": {
"date": {
"type": "date"},
"subjects": {
"type": "nested",
"properties": {
"name": {
"type": "string"},
"score": {
"type": "short"}}}}}}}}}
测试文档:
{
"name": "John",
"enrollment": "2012-09-01T00:00:00+00:00",
"exam_histories": [
{
"date": "2016-05-05T00:00:00+00:00",
"subjects": [
{
"name": "math",
"score": 90}]}]}
聚合查询(无实际意义):
{
"aggs": {
"nested_exam_histories": {
"nested": {
"path": "exam_histories"},
"aggs": {
"date_buckets": {
"date_histogram": {
"field": "exam_histories.date",
"interval": "day"},
"aggs": {
"this_reverse_nested_does_work": {
"reverse_nested": {},
"aggs": {
"newest_enrollment": {
"max": {
"field": "enrollment"}}}},
"deep_nested_subjects": {
"nested": {
"path": "exam_histories.subjects"},
"aggs": {
"score_buckets": {
"terms": {
"field": "exam_histories.subjects.score"},
"aggs": {
"this_reverse_nested_doesnt_work": {
"reverse_nested": {},
"aggs": {
"newest_exam_date": {
"max": {
"field": "exam_histories.date"}}}}}}}}}}}}}}
结果:
...
"aggregations" : {
"nested_exam_histories" : {
"doc_count" : 1,
"date_buckets" : {
"buckets" : [ {
"key_as_string" : "2016-05-05T00:00:00.000Z",
"key" : 1462406400000,
"doc_count" : 1,
"this_reverse_nested_does_work" : {
"doc_count" : 1,
"newest_enrollment" : {
"value" : 1.3464576E12,
"value_as_string" : "2012-09-01T00:00:00.000Z"
}
},
"deep_nested_subjects" : {
"doc_count" : 1,
"score_buckets" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : 90,
"doc_count" : 1,
"this_reverse_nested_doesnt_work" : {
"doc_count" : 1,
"newest_exam_date" : {
"value" : null
}
...
...在这里您可以看到聚合“newest_exam_date”不起作用。是错误还是我做错了什么?
最佳答案
您需要使用 path
选项明确指定要“反向聚合”的嵌套对象,否则它会假定该字段位于根级别。
来自documentation :
path - Which defines to what nested object field should be joined back. The default is empty, which means that it joins back to the root / main document level. The path cannot contain a reference to a nested object field that falls outside the nested aggregation’s nested structure a reverse_nested is in. Example:
{
"size":0,
"aggs": {
"nested_exam_histories": {
"nested": {
"path": "exam_histories"
},
"aggs": {
"date_buckets": {
"date_histogram": {
"field": "exam_histories.date",
"interval": "day"
},
"aggs": {
"this_reverse_nested_does_work": {
"reverse_nested": {},
"aggs": {
"newest_enrollment": {
"max": {
"field": "enrollment"
}
}
}
},
"deep_nested_subjects": {
"nested": {
"path": "exam_histories.subjects"
},
"aggs": {
"score_buckets": {
"terms": {
"field": "exam_histories.subjects.score"
},
"aggs": {
"this_reverse_nested_doesnt_work": {
"reverse_nested": {
"path": "exam_histories"
},
"aggs": {
"newest_exam_date": {
"max": {
"field": "exam_histories.date"
}
}
}
}
}
}
}
}
}
}
}
}
}
}
结果:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"nested_exam_histories": {
"doc_count": 2,
"date_buckets": {
"buckets": [
{
"key_as_string": "2016-05-05T00:00:00.000Z",
"key": 1462406400000,
"doc_count": 2,
"this_reverse_nested_does_work": {
"doc_count": 2,
"newest_enrollment": {
"value": 1377993600000,
"value_as_string": "2013-09-01T00:00:00.000Z"
}
},
"deep_nested_subjects": {
"doc_count": 2,
"score_buckets": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 90,
"doc_count": 2,
"this_reverse_nested_doesnt_work": {
"doc_count": 2,
"newest_exam_date": {
"value": 1462406400000,
"value_as_string": "2016-05-05T00:00:00.000Z"
}
}
}
]
}
}
}
]
}
}
}
}
注意第二个“reverse-aggergation”中的path
选项:
reverse_nested": {
"path": "exam_histories"
}
关于Elasticsearch:深度嵌套聚合下的 reverse_nested 聚合不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39012928/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!