Unique Index names using elastic rest client

Using ES rest client, How to get List of all unique index based on some search criterion. Below code gives my all index names but duplicate ones too. Please help.

       SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
       BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); 
       boolQueryBuilder.must(QueryBuilders.matchQuery("indexMetadata.user","95103"));
    
    boolQueryBuilder.must(QueryBuilders.matchQuery("indexMetadata.indexLevel","LOCAL"));
        sourceBuilder.query(boolQueryBuilder);
        SearchRequest searchRequest = new SearchRequest();

        String[] includeFields = new String[]{"hits.hits._index"};
    String[] excludeFields = new String[]{};
    sourceBuilder.fetchSource(includeFields, excludeFields);

    searchRequest.source(sourceBuilder);
    SearchResponse searchResponse = client.search(searchRequest);
            SearchHits hits = searchResponse.getHits();
            SearchHit[] searchHits = hits.getHits();
           for (SearchHit hit : searchHits) {
            	System.out.println(hit.getIndex());
}

Please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:

```
CODE
```

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.
Please update your post.

I'd run here à terms aggregation on _index field.

Thanks @dadoonet. . I formatted the code.I tried to apply the aggregation on_index field. But still I am getting duplicate index names. I am using 6.2.1 High Level Rest Client. Could you please suggest me right code snippet for this aggregation case.Thanks In advance.

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.
Please share what you got and your code.

package org;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;

public class IndexService {
	public static void main(String[] args) throws IOException {
		RestHighLevelClient client = null;
		try {

			client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
			SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
			BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
			TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_index").field("hits.hits._index");

			boolQueryBuilder.must(QueryBuilders.matchQuery("indexMetadata.user.keyword", "95103"));
			boolQueryBuilder.must(QueryBuilders.matchQuery("indexMetadata.indexLevel.keyword", "LOCAL"));
			boolQueryBuilder.must(QueryBuilders.matchQuery("indexMetadata.indexType.keyword", "productoffering"));
			boolQueryBuilder.must(
					QueryBuilders.matchQuery("indexMetadata.businessRequestID.keyword", "567182321-dsfdsfsd-324324"));

			sourceBuilder.query(boolQueryBuilder);
			sourceBuilder.aggregation(aggregation);

			SearchRequest searchRequest = new SearchRequest();

			String[] includeFields = new String[] { "hits.hits._index" };
			String[] excludeFields = new String[] {};
			sourceBuilder.fetchSource(includeFields, excludeFields);

			searchRequest.source(sourceBuilder);

			SearchResponse searchResponse = client.search(searchRequest);
			SearchHits hits = searchResponse.getHits();
			SearchHit[] searchHits = hits.getHits();
			Set<String> h = new HashSet<String>();
			for (SearchHit hit : searchHits) {

				System.out.println(hit.getIndex());

			}

		} catch (Exception e) {

		} finally {

			client.close();

		}

	}

}

Above Code give me below results
index4
ui-index
index1
index1

Here index1 is getting duplicated.

@dadoonet Could you please look at my code

Read this and specifically the "Also be patient" part.

I did not say to run an agg on hits.hits._index bit on _index.

Alain, try first with the dev console, and make it work. Then convert to Java code.

@dadoonet I am able to get the result while hitting directly to elasticsearch using but i am not able to find corresponding java methods in high level rest client latest version.

Share the "Dev Console" version and your Java code please.

version is 6.2.1
And i have already share the code above in comment.Only change in above code is I am running aggregation on “_index”.

Sorry but I don't see the "Dev Console" like code which you referred as:

I am able to get the result while hitting directly to elasticsearch

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