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