【发布时间】:2020-09-25 11:04:02
【问题描述】:
我想使用 Elasticsearch 的聚合来做 OLAP 数据分析。 我要做的是将scriptedMetric聚合嵌套到术语聚合中,如下所示(正确)
{
"from": 0,
"size": 0,
"query":{
"bool":{
"must":[
{
"match":{
"poi_id":1
}
}
]
}
},
"aggregations": {
"poi_id": {
"terms": {
"script": {
"inline": "doc['poi_id'].value + 1"
}
},
"aggregations": {
"price": {
"sum": {
"field": "price"
}
}
}
}
}
}
但我没有在 Elasticsearch 的 java api 中找到如何做到这一点。
我试过这样:
SearchResponse response = client.prepareSearch("poi")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setFetchSource(new String[]{"poi_id","poi_name"}, null)
.setQuery(QueryBuilders.termQuery("poi_id", 1))
.addAggregation(AggregationBuilders.terms("poi_id").subAggregation((AggregationBuilders.scriptedMetric("poi_id").mapScript(new Script("doc['poi_id'].value + 1")))))
.execute()
.actionGet();
但出现错误
Caused by: NotSerializableExceptionWrapper[: value source config is invalid; must have either a field context or a script or marked as unwrapped]; nested: IllegalStateException[value source config is invalid; must have either a field context or a script or marked as unwrapped];
我搜索了很多,但找不到演示。
任何帮助将不胜感激。
谢谢!
【问题讨论】:
-
你解决了吗?
-
@mlecz 还没有。我在自己的应用程序中做到了
-
我刚刚在我的情况下解决了它(最大聚合)。而你似乎缺乏同样的东西。将 .field("fieldname) 添加到您的聚合构建器中。也许会有所帮助。
标签: elasticsearch elasticsearch-aggregation