Elastic Search Java API implementaion to get No of Hits(First time working on Elastic Search)

Hi I am a Software tester trying to get the Number of Hits through Elastic Search for the first time.
I have no idea about Ellastic Search.Please tell me if my approach is correct.

Following are the dependencies in pom.xml,

org.elasticsearch elasticsearch 5.6.0
	<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->

Following is the code to Connect to Index,

Settings setting = Settings.builder()
.put("cluster.name", "cluster name")
.put("client.transport.sniff", true).build();

	TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
	        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("IPValue"), 9300));

return client;

Following is the code to get the Number of Hits,

//Getting the Manufacture Id's of all Active Manfacturers

TermQueryBuilder queryComp = QueryBuilders.termQuery("Manufacturer_Status","Active");

SearchResponse response = client.prepareSearch("X")



.setFetchSource(new String{"Manufacturer_Id"}, null)




SearchHits hits = response.getHits();

//Count of all Active Manufacturers:

long activeManCount=hits.getTotalHits();

System.out.println("Active Manufacturer Count is"+ activeManCount);

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:


This is the icon to use if you are not using markdown format:

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.

One small comments to start with:

  • upgrade at least to 6.8 or better 7.6
  • don't use the deprecated TransportClient

Hello David,

Thanks for your point.I have change the dependency version to 7.6 in my pom.xml.What should I replace the deprecated TransportClient with?Please give more suggestions if my Code needs any change as I am working on this Elastic Search implementation using Java for the very first time.Actually I am a QA Person.

Hi David, What should I replace the deprecated TransportClient with?

The TransportClient documentation says it. Have a look at https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html

Have a look at an example at https://github.com/dadoonet/legacy-search/tree/06-fuzziness?files=1

Also an old video here


Thanks a Ton David.I am trying to connect to Kibana on Chrome.The Kibana does not load and while I try to start kibana .yml I get message " Unable to revive connection: http:" and "No living connections".

That's not related. Please open a new question.

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