-1
{"size": 0,"query": {"term": {"loan_application_id": {"value": 87015}}},"aggs": {"group_by_fields": {"composite": {"size": 10,"sources": [{"loan_application_id": {"terms": {"field":"loan_application_id"}}},{"name": {"terms": {"field": "name.keyword"}}},{"upload_source": {"terms": {"field": "upload_source.keyword"}}}]},"aggregations":{"max_created_at": {"max": {"field": "created_at"}}}}}}

This query is working in postman but when i try to send it like

@Query("{\"query\": {\"term\": {\"loan_application_id\": {\"value\": 87015}}},\"aggs\": {\"group_by_fields\": {\"composite\": {\"size\": 10,\"sources\": [{\"loan_application_id\": {\"terms\": {\"field\":\"loan_application_id\"}}},{\"name\": {\"terms\": {\"field\": \"name.keyword\"}}},{\"upload_source\": {\"terms\": {\"field\": \"upload_source.keyword\"}}}]},\"aggregations\":{\"max_created_at\": {\"max\": {\"field\": \"created_at\"}}}}}}")
    SearchHits<LoanAttachmentEs> getDocUploadSourceOfQulaifiedLeads(@Param("loanApplicationIds") List<Long> loanApplicationIds); its not workinng after adding and removing size also i am not getting response. 

Can anyone help me with this.

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder;
import org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class YourService {

    private final RestHighLevelClient restHighLevelClient;

    @Autowired
    public YourService(RestHighLevelClient restHighLevelClient) {
        this.restHighLevelClient = restHighLevelClient;
    }

    public SearchResponse aggregateByLoanApplicationIds(List<String> loanApplicationIdList) throws IOException {
        CompositeAggregationBuilder compositeAggregation = AggregationBuilders.composite("group_by_fields")
                .sources(
                        new TermsValuesSourceBuilder("loan_application_id").field("loan_application_id.keyword"),
                        new TermsValuesSourceBuilder("name").field("name.keyword"),
                        new TermsValuesSourceBuilder("upload_source").field("upload_source.keyword")
                );

        MaxAggregationBuilder maxCreatedAtAggregation = AggregationBuilders.max("max_created_at").field("created_at");

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
                .query(QueryBuilders.termsQuery("loan_application_id.keyword", loanApplicationIdList))
                .aggregation(compositeAggregation.subAggregation(maxCreatedAtAggregation))
                .size(0); // Set size to 0 to get only the aggregations without hits

        SearchRequest searchRequest = new SearchRequest("loan_attachments");
        searchRequest.source(searchSourceBuilder);

        return restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    }
}

I asked Chat GPT and got this then I tried to write my query like this but didnt work its wrong given by CHAT GPT.

@Query("{\"query\": {\"term\": {\"loan_application_id\": {\"value\": 87015}}},\"aggs\": {\"group_by_fields\": {\"composite\": {\"size\": 10,\"sources\": [{\"loan_application_id\": {\"terms\": {\"field\":\"loan_application_id\"}}},{\"name\": {\"terms\": {\"field\": \"name.keyword\"}}},{\"upload_source\": {\"terms\": {\"field\": \"upload_source.keyword\"}}}]},\"aggregations\":{\"max_created_at\": {\"max\": {\"field\": \"created_at\"}}}}}}")
    SearchHits<LoanAttachmentEs> getDocUploadSourceOfQulaifiedLeads(@Param("loanApplicationIds") List<Long> loanApplicationIds);

this but didnt worked anything . Getting diffrent diffrent errors.

1 Answers1

0

@Query annotations on methods are only for defining the query part, you cannot pass in aggregations in that parameter.

To use aggregations you will need to build a NativeQuery that contains a query for the term query part and the aggregations definition. You can then pass this NativeQuery to your ElasticsearchOperations implementation (let that inject by Spring). If you want that in a repository function you'll need towrite a custom repository fragment.

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