# Unique Index names using elastic rest client

**URL:** https://discuss.elastic.co/t/unique-index-names-using-elastic-rest-client/120344
**Category:** Elasticsearch
**Created:** [February 18, 2018, 11:00am UTC](https://discuss.elastic.co/t/unique-index-names-using-elastic-rest-client/120344 "2018-02-18T11:00:49Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Akib\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/b5a626/32.png) [@Akib\_Ali](https://discuss.elastic.co/u/Akib_Ali)
#### Post date: [February 18, 2018, 11:00am UTC](https://discuss.elastic.co/t/unique-index-names-using-elastic-rest-client/120344/1 "2018-02-18T11:00:49Z")

</div>

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());
}
```

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [February 18, 2018, 11:30am UTC](https://discuss.elastic.co/t/unique-index-names-using-elastic-rest-client/120344/2 "2018-02-18T11:30:21Z")

</div>

Please format your code, logs or configuration files using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) 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.

---

<div class="post-metadata">

### Author: ![Akib\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/b5a626/32.png) [@Akib\_Ali](https://discuss.elastic.co/u/Akib_Ali)
#### Post date: [February 18, 2018, 12:00pm UTC](https://discuss.elastic.co/t/unique-index-names-using-elastic-rest-client/120344/3 "2018-02-18T12:00:08Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [February 18, 2018, 1:02pm UTC](https://discuss.elastic.co/t/unique-index-names-using-elastic-rest-client/120344/4 "2018-02-18T13:02:06Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). 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.

---

<div class="post-metadata">

### Author: ![Akib\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/b5a626/32.png) [@Akib\_Ali](https://discuss.elastic.co/u/Akib_Ali)
#### Post date: [February 18, 2018, 4:28pm UTC](https://discuss.elastic.co/t/unique-index-names-using-elastic-rest-client/120344/5 "2018-02-18T16:28:25Z")

</div>

```
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.

---

<div class="post-metadata">

### Author: ![Akib\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/b5a626/32.png) [@Akib\_Ali](https://discuss.elastic.co/u/Akib_Ali)
#### Post date: [February 18, 2018, 4:30pm UTC](https://discuss.elastic.co/t/unique-index-names-using-elastic-rest-client/120344/6 "2018-02-18T16:30:03Z")

</div>

@dadoonet Could you please look at my code

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [February 18, 2018, 6:18pm UTC](https://discuss.elastic.co/t/unique-index-names-using-elastic-rest-client/120344/7 "2018-02-18T18:18:40Z")

</div>

Read [this](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) 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.

---

<div class="post-metadata">

### Author: ![Akib\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/b5a626/32.png) [@Akib\_Ali](https://discuss.elastic.co/u/Akib_Ali)
#### Post date: [February 19, 2018, 5:14pm UTC](https://discuss.elastic.co/t/unique-index-names-using-elastic-rest-client/120344/8 "2018-02-19T17:14:49Z")

</div>

@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.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [February 19, 2018, 6:17pm UTC](https://discuss.elastic.co/t/unique-index-names-using-elastic-rest-client/120344/9 "2018-02-19T18:17:09Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Akib\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/b5a626/32.png) [@Akib\_Ali](https://discuss.elastic.co/u/Akib_Ali)
#### Post date: [February 19, 2018, 6:55pm UTC](https://discuss.elastic.co/t/unique-index-names-using-elastic-rest-client/120344/10 "2018-02-19T18:55:22Z")

</div>

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”.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [February 19, 2018, 7:40pm UTC](https://discuss.elastic.co/t/unique-index-names-using-elastic-rest-client/120344/11 "2018-02-19T19:40:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [March 19, 2018, 7:41pm UTC](https://discuss.elastic.co/t/unique-index-names-using-elastic-rest-client/120344/12 "2018-03-19T19:41:10Z")

</div>

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