简体   繁体   中英

Elasticsearch Java client: can't connect to the remote server

I need to connect to the remotely located ElasticSearch index, using the url provided here: http://api.exiletools.com/info/indexer.html

However, I can't figure out how to do this in Java.

Docs on ES Java Client don't really have much info at all.
I also did not find any JavaDocs for it, do they exist?

Now, there are working examples written in Python, which confirm that the server is up and running, connection part looks like this:

es = Elasticsearch([{
  'host':'api.exiletools.com',
  'port':80,
  'http_auth':'apikey:DEVELOPMENT-Indexer'
}])

What I tried to do:

client = new TransportClient()
                    .addTransportAddress(new InetSocketTransportAddress("apikey:DEVELOPMENT-Indexer@api.exiletools.com/index", 9300));

also tried ports 9200 and 80

This results in: java.nio.channels.UnresolvedAddressException
and NoNodeAvailableException

The Shop Indexer API offers an HTTP entry point on port 80 to communicate with their ES cluster via the HTTP protocol. The ES TransportClient is not the correct client to use for that as it will only communicate via TCP.

Since Elasticsearch doesn't provide an HTTP client out of the box, you need to either use a specific library for that (like eg Jest ) or you can roll up your own REST client.

An easy way to achieve the latter would be using Spring's RestTemplate :

// create the REST template
RestTemplate rest = new RestTemplate()
// add the authorization header
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "DEVELOPMENT-Indexer");

// define URL and query
String url = "http://api.exiletools.com/index/_search";
String allQuery = "{\"query\":{\"matchAll\":{}}}";

// make the request
HttpEntity<String> httpReq = new HttpEntity<String>(allQuery, headers);
ResponseEntity<String> resp = rest.exchange(url, HttpMethod.POST, httpReq, String.class)

// retrieve the JSON response
String body = resp.getBody();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM