How to establish a connection from java client to elastic search version 8

A guide for establishing a connection of java client with elastic-search without using RestHighLevelClient method

Welcome!

Is there anything unclear in Connecting | Elasticsearch Java API Client [8.4] | Elastic?

Thanks dadoonet for replying.
But when i follow the code snippets given there, it is so much confusing for a new bee to get started with. It throws unable to find valid certification path to requested target error.

public static void main(String[] args) throws CertificateException {
		// TODO Auto-generated method stub
		
		RestHighLevelClient client = new RestHighLevelClient(
				RestClient.builder(new HttpHost("localhost", 9200, "https")));
		
		SearchRequest searchRequest = new SearchRequest();
	    searchRequest.indices("tesla_employees");
	    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
	    searchSourceBuilder.query(QueryBuilders.matchAllQuery());
	    searchRequest.source(searchSourceBuilder);
	    Map<String, Object> map=null;
		Path caCertificatePath = Paths.get("/Users/chirag_gupta/Downloads/elasticsearch-8.4.1/config/certs/http_ca.crt");
		CertificateFactory factory =
				CertificateFactory.getInstance("X.509");
		Certificate trustedCa;
	    try {
	        SearchResponse searchResponse = null;
	        searchResponse =client.search(searchRequest, RequestOptions.DEFAULT);
	        if (searchResponse.getHits().getTotalHits().value > 0) {
	            SearchHit[] searchHit = searchResponse.getHits().getHits();
	            for (SearchHit hit : searchHit) {
	                map = hit.getSourceAsMap();
	                  System.out.println("map:"+Arrays.toString(map.entrySet().toArray()));
	                    
	                
	            }
	        }
	    } catch (IOException e) {
	        e.printStackTrace();
	    }

	}

You declared a client but you are not using it.

Please don't post images of text as they are hard to read, may not display correctly for everyone, and are not searchable.

Instead, paste the text and format it with </> icon or pairs of triple backticks (```), and check the preview window to make sure it's properly formatted before posting it. This makes it more likely that your question will receive a useful answer.

It would be great if you could update your post to solve this.

What values to be added into the trusStorePath?

Path trustStorePath = Paths.get("/path/to/truststore.p12");
KeyStore truststore = KeyStore.getInstance("pkcs12");

I believe you need to follow the guide?

Thanks

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