0

I know the curl command to get the information I need.

curl localhost:9200/_cat/indices/alerts*

and the output is

yellow open alerts_1502744517_1 5 1 25 0 642.3kb 642.3kb 
yellow open alerts_1502744517_0 5 1 25 0 741.8kb 741.8kb 
yellow open alerts_1502744882_0 5 1 27 0 679.8kb 679.8kb 

What I need is the index name and the document count, which in this case is the 6th column (the 25, 25, and 27). How do I make the equivalent call using the java API?

3 Answers 3

0

I haven't try, but after some research on baeldung and stackoverflow. I found a solution that could work using this maven dependency

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>2.4.0</version>
</dependency>

Here is the code

Node node = nodeBuilder()
  .clusterName("elasticsearch")
  .client(true).node();
Client client = node.client();
SearchResponse response = client.prepareSearch("my-index")
   .setTypes("my-type")
   .setSearchType(SearchType.QUERY_AND_FETCH)
   .setFetchSource(new String[]{"field1", "field2"}, null)
   .setQuery(QueryBuilders.termsQuery("field1", "1234"))
   .execute()
   .actionGet();

for (SearchHit hit : response.getHits()){
   Map map = hit.getSource();
   map.toString();
}

Let me know if you have any error

Sign up to request clarification or add additional context in comments.

Comments

0

I think one way is to get all the indices

String[] indices = client.admin()
                             .cluster().prepareState().get()
                             .getState().getMetaData()
                             .getConcreteAllIndices()

Then traverse them all and get the count for each indices.

Take a look into this class if you want to make a REST call: RestIndicesAction.java or the cat package

2 Comments

I was hoping for something that would get the index names and the document counts all in one go so I wouldn't have to keep querying for each index.
Second option with cat REST API can do that with one query.
0

Here is my solution. Written on ElasticSearch 6.2.4 with Java 8.

/*for document count*/
long docCount = client.admin().indices().prepareStats(indiceName).clear().setDocs(true).execute().actionGet().getPrimaries().getDocs().getCount();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.