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

**URL:** <https://discuss.elastic.co/t/elastic-search-java-api-implementaion-to-get-no-of-hits-first-time-working-on-elastic-search/230794>\
**Category:** Elasticsearch\
**Created:** [May 2, 2020, 7:53am UTC](https://discuss.elastic.co/t/elastic-search-java-api-implementaion-to-get-no-of-hits-first-time-working-on-elastic-search/230794 "2020-05-02T07:53:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhir83](https://avatars.discourse-cdn.com/v4/letter/a/e99b99/32.png) [@abhir83](https://discuss.elastic.co/u/abhir83)\
**Post date:** [May 2, 2020, 7:53am UTC](https://discuss.elastic.co/t/elastic-search-java-api-implementaion-to-get-no-of-hits-first-time-working-on-elastic-search/230794/1 "2020-05-02T07:53:09Z")

</div>

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 -->
	<dependency>
		<groupId>org.elasticsearch.client</groupId>
		<artifactId>transport</artifactId>
		<version>6.3.2</version>
	</dependency>

```

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")

.setTypes("text")

.setSearchType(SearchType.QUERY\_THEN\_FETCH)

.setFetchSource(new String{"Manufacturer\_Id"}, null)

.setQuery(queryComp)

.execute()

.actionGet();

SearchHits hits = response.getHits();

//Count of all Active Manufacturers:

long activeManCount=hits.getTotalHits();

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

---

<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:** [May 2, 2020, 12:57pm UTC](https://discuss.elastic.co/t/elastic-search-java-api-implementaion-to-get-no-of-hits-first-time-working-on-elastic-search/230794/2 "2020-05-02T12:57:07Z")

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

````

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

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/e/7e6e239431ec2d71cbf1beef741f2e93e7cc762c.jpg)

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.

---

<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:** [May 2, 2020, 12:58pm UTC](https://discuss.elastic.co/t/elastic-search-java-api-implementaion-to-get-no-of-hits-first-time-working-on-elastic-search/230794/3 "2020-05-02T12:58:14Z")

</div>

One small comments to start with:

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

---

<div class="post-metadata">

**Author:** ![abhir83](https://avatars.discourse-cdn.com/v4/letter/a/e99b99/32.png) [@abhir83](https://discuss.elastic.co/u/abhir83)\
**Post date:** [May 2, 2020, 2:00pm UTC](https://discuss.elastic.co/t/elastic-search-java-api-implementaion-to-get-no-of-hits-first-time-working-on-elastic-search/230794/4 "2020-05-02T14:00:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![abhir83](https://avatars.discourse-cdn.com/v4/letter/a/e99b99/32.png) [@abhir83](https://discuss.elastic.co/u/abhir83)\
**Post date:** [May 2, 2020, 1:47pm UTC](https://discuss.elastic.co/t/elastic-search-java-api-implementaion-to-get-no-of-hits-first-time-working-on-elastic-search/230794/5 "2020-05-02T13:47:22Z")

</div>

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

---

<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:** [May 2, 2020, 3:04pm UTC](https://discuss.elastic.co/t/elastic-search-java-api-implementaion-to-get-no-of-hits-first-time-working-on-elastic-search/230794/6 "2020-05-02T15:04:09Z")

</div>

The TransportClient documentation says it. Have a look at [https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html](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](https://github.com/dadoonet/legacy-search/tree/06-fuzziness?files=1)

Also an old video here

[https://player.vimeo.com/video/274560967](https://player.vimeo.com/video/274560967)

---

<div class="post-metadata">

**Author:** ![abhir83](https://avatars.discourse-cdn.com/v4/letter/a/e99b99/32.png) [@abhir83](https://discuss.elastic.co/u/abhir83)\
**Post date:** [May 3, 2020, 9:39am UTC](https://discuss.elastic.co/t/elastic-search-java-api-implementaion-to-get-no-of-hits-first-time-working-on-elastic-search/230794/7 "2020-05-03T09:39:30Z")

</div>

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

---

<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:** [May 3, 2020, 1:01pm UTC](https://discuss.elastic.co/t/elastic-search-java-api-implementaion-to-get-no-of-hits-first-time-working-on-elastic-search/230794/8 "2020-05-03T13:01:09Z")

</div>

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

---

<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:** [May 31, 2020, 1:01pm UTC](https://discuss.elastic.co/t/elastic-search-java-api-implementaion-to-get-no-of-hits-first-time-working-on-elastic-search/230794/9 "2020-05-31T13:01:09Z")

</div>

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