# Indices Stats using the NodeClient with the Java API

**URL:** <https://discuss.elastic.co/t/indices-stats-using-the-nodeclient-with-the-java-api/21604>\
**Category:** Elasticsearch\
**Created:** [January 13, 2015, 11:56am UTC](https://discuss.elastic.co/t/indices-stats-using-the-nodeclient-with-the-java-api/21604 "2015-01-13T11:56:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_2](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Marc\_2](https://discuss.elastic.co/u/Marc_2)\
**Post date:** [January 13, 2015, 11:56am UTC](https://discuss.elastic.co/t/indices-stats-using-the-nodeclient-with-the-java-api/21604/1 "2015-01-13T11:56:07Z")

</div>

Hi,

I would like to get a list of the available indices in my cluster using the  
java api using the node client.

Currently the request is done via the REST interface similar to this:  
[http://localhost:9200/logstash-\*/\_stats/indices](http://localhost:9200/logstash-*/_stats/indices)

Cheers  
Marc

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/e3966486-9cdc-49b7-a657-ff22b2b5a772%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e3966486-9cdc-49b7-a657-ff22b2b5a772%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)\
**Post date:** [January 13, 2015, 9:02pm UTC](https://discuss.elastic.co/t/indices-stats-using-the-nodeclient-with-the-java-api/21604/2 "2015-01-13T21:02:31Z")

</div>

Marc,

Maybe these snippets will help?

The enclosing class's constructor sets the client data member to either a  
Node client or Transport client (both work fine; I prefer the  
TransportClient).

The source string contains one or more index names, with a comma between  
each pair of names. A name may contain wildcards as supported by ES.

This code works for 1.3.4. Not sure if 4.X has yet another breaking change,  
but if that's the case, it is usually no big deal to handle.

_public static String[] parseIndex(String source)_  
{  
return indexSplitter.split(source);  
}

_public String[] getIndexNames(String indexPattern) throws UtilityException_  
{  
if (indexPattern.trim().isEmpty())  
throw new UtilityException("Cannot resolve empty index pattern ["  
+ indexPattern + "]");

try  
{  
/\*  
\* Parse the index pattern on commas, if present. Then pass the list of  
\* individual names (which may include wildcards and - signs) to  
\* Elasticsearch for final resolution  
\*/  
String indexSpecList = parseIndex(indexPattern);

```
/*
 * Get the list of individual index names, along with their status
 * information (which we will ignore in this method)
 */
IndicesAdminClient iac = client.admin().indices();
RecoveryRequestBuilder isrb = iac.prepareRecoveries();
isrb.setIndices(indexSpecList);
RecoveryResponse isr = isrb.execute().actionGet();

/* Create an array of just the names of the indices */
ArrayList<String> indices = new ArrayList<String>();
Map<String, List<ShardRecoveryResponse>> sr = isr.shardResponses();
for (String index : sr.keySet())
{
  indices.add(index.trim());
}

/* Be sure there is at least one index that matches the pattern */
if (indices.isEmpty())
  throw new UtilityException("Cannot resolve index pattern ["
      + indexPattern + "] to at least one existing index");

/* Convert to String[] and return */
return indices.toArray(new String[indices.size()]);

```

}  
catch (ElasticsearchException e)  
{  
throw new UtilityException("Cannot resolve index pattern ["  
+ indexPattern + "]: " + e);  
}  
}

private final Client client;

private static final Pattern indexSplitter = Pattern.compile(Pattern  
.quote(","));

Brian

On Tuesday, January 13, 2015 at 6:56:07 AM UTC-5, Marc wrote:

> Hi,
> 
> I would like to get a list of the available indices in my cluster using  
> the java api using the node client.
> 
> Currently the request is done via the REST interface similar to this:  
> [http://localhost:9200/logstash-\*/\_stats/indices](http://localhost:9200/logstash-*/_stats/indices)
> 
> Cheers  
> Marc

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/49ed97eb-6c2f-4c56-a551-ebc86c9559ca%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/49ed97eb-6c2f-4c56-a551-ebc86c9559ca%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 12:39am UTC](https://discuss.elastic.co/t/indices-stats-using-the-nodeclient-with-the-java-api/21604/3 "2017-07-06T00:39:15Z")

</div>


