0

Java = 8 ElasticSearchServer = 7.16.3

My code never returns the value for aggregations , I tried most of the combinations. Below is the code , please see if you can help here

@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;
   }
Mahaveer Jangir
  • 597
  • 7
  • 15

1 Answers1

0

You cannot get aggregations with repository queries in Spring Data Elasticsearch. You will need to create a NativeQuery with the required aggregations and need to use the ElasticsearchOperations to process that query.

You do not specify what verison of Spring Data Elasticsearch you use, but looking at the tag resthighlevelclient and the Elasticsearch version you seem to be using an old version which is deprecated now.

P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66