javaelasticsearchluceneresthighlevelclientelasticsearch-high-level-restclient

How to remove all aggregation from a SearchSourceBuilder fully?


I have a SearchSourceBuilder which has all blocks like Query,From, Size, Aggregations etc. But later I want to remove only the Aggregations blocks for some use case fully before sending the DSL to ES.

Example, I need to remove the entire aggregations block from the SearchSourceBuilder from the entire DSL enter image description here


Solution

  • SearchSourceBuilder class has multiple aggregation methods to build different types of aggregations as shown in the intelliJ image below

    You just need to remove those aggregation method from SearchSourceBuilder

    For example, my below code uses query filters, size and agss

    SearchSourceBuilder queryBuilder = new SearchSourceBuilder()
                    .query(queryFilters)
                    .size(0)
                    .aggregation(nodeTasksAggs);
    

    And if I don't want aggs, I can just use below code

    SearchSourceBuilder queryBuilder = new SearchSourceBuilder()
                    .query(queryFilters)
                    .size(0);
    

    enter image description here