尝试在java中使用聚合,使用ElasticsearchRepository从Elasticsearch获取聚合数据,但我的aggs总是null

pgpifvop  于 2023-03-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(164)

Java = 8 ElasticSearchServer = 7.16.3
我的代码从来不返回聚合的值,我尝试了大多数的组合。下面是代码,请看你是否能在这里提供帮助

@Repository
public interface ScPayHkIndexRepository extends ElasticsearchRepository<ScPayHkIndex, String> {
   Stream<ScPayHkIndex> findByStatusNotAndExceptionTypeAndTimestampBetween(String status, String exceptionType, long from, long to);
   Stream<ScPayHkIndex> findByPaymentTypeAndTimestampBetween(String paymentType, long from, long to);
   
   @Query(("{\"bool\": {\"must\": [{\"match\": {\"status\": \"Completed\"}}]}},\"aggs\": {\"percentAggs\": {\"percentile_ranks\": {\"field\": \"Total_Time\"}}}"))
   SearchHits<ScPayHkIndex> getPercentileRank();

   
   default double getMyPercentileValue() {
       SearchHits<ScPayHkIndex> response = getPercentileRank();
       Aggregations aggregation = response.getAggregations();
       
       PercentileRanks percentileRanks = aggregation.get("percentAggs");

       // Get the desired percentile value using the percentile() method of the Percentiles object
       double myPercentileValue = percentileRanks.percent(5000);

       return myPercentileValue;
   }
k97glaaz

k97glaaz1#

在Spring Data Elasticsearch中,您无法通过存储库查询获取聚合。您将需要创建一个具有所需聚合的NativeQuery,并需要使用ElasticsearchOperations来处理该查询。
你没有指定你使用的Spring Data Elasticsearch的版本,但是看看标签resthighlevelclient和Elasticsearch版本,你似乎在使用一个现在不推荐使用的旧版本。

相关问题