Perform count with high-level-rest-client ans spring boot

I am trying to get the count of total documents with some filtration.

here what I am trying-

    public Object count() throws IOException {
          SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
          BoolQueryBuilder query = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("job_name", "AGGREGRATION_TABLE"))
                .must(QueryBuilders.rangeQuery("start_time").gte("now-10d").lte("now"));
          sourceBuilder.query(query);
          SearchRequest searchRequest = new SearchRequest();
          searchRequest.indices(INDEX);
          searchRequest.source(sourceBuilder);
          SearchResponse response  = restHighLevelClient.search(searchRequest, 
                RequestOptions.DEFAULT);
          return response.getHits().totalHits;
   }

I am always getting the count as 0,
But when I am trying to hit the elastic API via postman with following details,

GET  /eps/_count
{
  "query" : {
    "bool" : {
      "filter" : {
        "range" : {
          "start_time" : { 
            "gt" : "now-10d" 
          }
        }
      },
  "must" : {
    "match" : {
      "job_name" : "AGGREGRATION_TABLE"
    }
  }
  }
  }
} 

I am getting the correct result as

{
    "count": 3,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    }
}

Any help would be appreciated.
thank you.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.