gpt4 book ai didi

java - 如何将此 JSON 写入 Elasticsearch Java API 中?

转载 作者:太空宇宙 更新时间:2023-11-04 11:24:23 24 4
gpt4 key购买 nike

我有以下 Elasticsearch JSON 查询(工作正常)-

{
"aggs": {
"by_emotion": {
"terms": {"field": "classifier_emotion"},
"aggs": {
"avg_probability": {
"avg": {"field": "classifier_emotion_probability"}
},
"sum_probability": {
"sum": {"field": "classifier_emotion_probability"}
}
}
}
}
}

我试图使用 Java API 重写它 -

public AggregationBuilder<TermsBuilder> getClassifierAggregation(String classifierName) {
String probailityField = classifierName + "_probability";
return AggregationBuilders.terms("by_class")
.subAggregation(
AggregationBuilders.terms("classifier").field(classifierName)
)
.subAggregation(
AggregationBuilders.avg("avg_probability").field(probailityField)
)
.subAggregation(
AggregationBuilders.sum("sum_probability").field(probailityField)
);
}

然后将其用作 -

SearchRequestBuilder request = elasticsearchClient.prepareSearch(index)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setFrom(0)
.setSize(0)
.addAggregation(getClassifierAggregation(classifierName));
SearchResponse response = request.execute().actionGet();

但是我遇到了以下异常 -

org.elasticsearch.search.aggregations.AggregationExecutionException: could not find the appropriate value context to perform aggregation [by_class]

我做错了什么?

最佳答案

我提到的方法写错了。这是正确的 -

public TermsBuilder getClassifierAggregation(String classifierName) {
String probailityField = classifierName + "_probability";
return AggregationBuilders.terms("by_class").field(classifierName)
.subAggregation(
AggregationBuilders.avg("avg_probability").field(probailityField)
)
.subAggregation(
AggregationBuilders.sum("sum_probability").field(probailityField)
);
}

关于java - 如何将此 JSON 写入 Elasticsearch Java API 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44542333/

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