Elastic Search High Level Client Aggregation to get Index wise Hits on Alias Search

Hi, I am using ES 7.3.1 and Elasticsearch high level client 7.3.1
We have an alias which comprises of 3 indexes.

    Alias Name :  employee_search
    Indexes in this alias : contract_employee,permanent_employee,client

I am searching over this alias as

    SearchRequest searchReq = new SearchRequest("employee_search");
    WrapperQueryBuilder qb = QueryBuilders.wrapperQuery(searchQuery); //i am preparing a dsl query
    SearchSourceBuilder sbb = new SearchSourceBuilder();
    sbb.query(qb);
    searchReq.source(sbb);
    SearchResponse resp = esClient.search(searchReq, RequestOptions.DEFAULT);

In response I need an aggregation where I can get index wise hits (which index gave how many hits?)
Aggregation which needs to be implemented is

 {
      "aggs": {
        "index_wise_count": {
          "terms": {
            "field": "_index"
          }
        }
      }
    }

How can I implement this in the existing search response?

What I tried but did not work:

    SearchRequest searchReq = new SearchRequest("employee_search");
        WrapperQueryBuilder qb = QueryBuilders.wrapperQuery(searchQuery); //i am preparing a dsl query
        SearchSourceBuilder sbb = new SearchSourceBuilder();
        sbb.query(qb);
        sbb.aggreagation(AggregationBuilders.terms("index_wise_count").field("index"));
        searchReq.source(sbb);
        SearchResponse resp = esClient.search(searchReq, RequestOptions.DEFAULT);

Can some 1 help how to implement aggregation to get index wise hit counts for alias?

There is a missing underscore in the field("index") statement, compared to your query.

1 Like

Thanks

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