title: Spring data elasticsearch aggregate sum - price, quantity
body: <p>I would like to aggregate data by using spring data elasticsearch. I want aggregate <code>sum(price)</code> , <code>sum(qty)</code>. What I should add to method <code>.addAggregation</code> to get that specific result and how to get it?</p>
  
  <pre><code>"properties": {
                          "cat": { "store": true,  "type": "long" }, 
                          "curr": { "index": "not_analyzed",  "store": true,  "type": "string" }, 
                          "end_date": { "store": true,  "type": "long" }, 
                          "price": { "store": true,  "type": "long" }, 
                          "start_date": { "store": true,  "type": "long" }, 
                          "tcat": { "store": true,  "type": "long" }, 
                          "title": { "store": true,  "type": "string" }, 
                          "uid": { "store": true,  "type": "long" }
                      }
  </code></pre>
  
  <p>My service method look like:</p>
  
  <pre><code>final List&lt;FilterBuilder&gt; filters = Lists.newArrayList();
          final NativeSearchQueryBuilder searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery());
          Optional.ofNullable(searchParams.getCategoryId()).ifPresent(v -&gt; filters.add(boolFilter().must(termFilter("cat", v))));
          Optional.ofNullable(searchParams.getCurrency()).ifPresent(v -&gt; filters.add(boolFilter().must(termFilter("curr", v))));
          Optional.ofNullable(searchParams.getTreeCategoryId()).ifPresent(v -&gt; filters.add(boolFilter().must(termFilter("tcat", v))));
          Optional.ofNullable(searchParams.getUid()).ifPresent(v -&gt; filters.add(boolFilter().must(termFilter("uid", v))));
  
          //access for many uids
          if (searchParams.getUids() != null) {
              Optional.ofNullable(searchParams.getUids().split(",")).ifPresent(v -&gt; {
                  filters.add(boolFilter().must(termsFilter("uid", v)));
              });
          }
  
          //access for many categories
          if (searchParams.getCategories() != null) {
              Optional.ofNullable(searchParams.getCategories().split(",")).ifPresent(v -&gt; {
                  filters.add(boolFilter().must(termsFilter("cat", v)));
              });
          }
  
          final BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
  
          if (Optional.ofNullable(searchParams.getTitle()).isPresent()) {
              boolQueryBuilder.should(queryStringQuery(searchParams.getTitle()).analyzeWildcard(true).field("title"));
          }
  
          if (Optional.ofNullable(searchParams.getStartDateFrom()).isPresent()
                  || Optional.ofNullable(searchParams.getStartDateTo()).isPresent()) {
              filters.add(rangeFilter("start_date").from(searchParams.getStartDateFrom()).to(searchParams.getStartDateTo()));
          }
  
          if (Optional.ofNullable(searchParams.getEndDateFrom()).isPresent()
                  || Optional.ofNullable(searchParams.getEndDateTo()).isPresent()) {
              filters.add(rangeFilter("end_date").from(searchParams.getEndDateFrom()).to(searchParams.getEndDateTo()));
          }
  
          if (Optional.ofNullable(searchParams.getPriceFrom()).isPresent()
                  || Optional.ofNullable(searchParams.getPriceTo()).isPresent()) {
              filters.add(rangeFilter("price").from(searchParams.getPriceFrom()).to(searchParams.getPriceTo()));
          }
  
  
  
      searchQuery.withQuery(boolQueryBuilder);
  
              FilterBuilder[] filterArr = new FilterBuilder[filters.size()];
              filterArr = filters.toArray(filterArr);
              searchQuery.withFilter(andFilter(filterArr));
  searchQuery.addAggregation(AggregationBuilders("price").field("price"));
          Aggregations aggregations = searchTemplate.query(searchQuery.build(), new ResultsExtractor&lt;Aggregations&gt;(){
          @Override
              public Aggregations extract(SearchResponse response) {
                  return response.getAggregations();
              }
          });
          return aggreg
  
  ations.asMap();
  </code></pre>
  
  <p>Why aggregation not getting to aggregate filters, bool etc. and return aggs I thnik for all records?</p>