I'm trying to connect from Java to ElasticSearch but I can only connect over HTTP. I can't use the TransportClient. Is there a Java client wrapper around the ElasticSearch REST APIs? If so, how do I use it?
Hi There is a brand new project just matching your needs. It Java based Rest API for Elasticsearch
Check it out! its name JEST
A new "official" REST-based java client will be available starting with v5.0.0-alpha4.
We just open sourced Flummi, a Java HTTP/REST client for Elastic Search. It imitates the transport client's API as closely as possible, making it easy to port existing code. It also provides a better abstraction level than Jest, because it reports all the errors with Exceptions. Give it a try!
Simple usage example:
Flummi flummi = new Flummi("http://elasticsearch.base.url:9200");
SearchResponse searchResponse = flummi
.prepareSearch("products")
.setQuery(
QueryBuilders.termQuery("color", "yellow").build()
)
.execute();
System.out.println("Found "
+ searchResponse.getHits().getTotalHits()
+ " products");
searchResponse.getHits()
.stream().map(hit -> hit.getSource().get("name").getAsString())
.forEach(name -> System.out.println("Name: " + name));
Since version 5.6 of the Elasticsearch Java SDK they provide a Java REST Client.
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")).build();
// for the RestHighLevelClient
RestHighLevelClient client =
new RestHighLevelClient(restClient);
Related
I'm using org.elasticsearch.client.RestHighLevelClient to get data from elasticsearch.
I want to know is it possible to get all documents using RestHighLevelClient for given index?
like http://localhost:9200/test/_search?
It is definitely possible.
First of all you need to initialize the client
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
then you need to execute a search query.
If you would like to fetch all docs you will have to use the scrolling API.
You can find a complete example here.
If you do not need all, you can simply use the search API.
And don't forget to close the connection when the work is done
client.close();
I am using elasticsearch 7.3 version, I am very new to this, all i want is to connect to elasticsearch node, i have installed ealasticsearch in one of the google cloud instance now i want to connect to elasticsearch and create index, delete index using java, how can i achieve that, i used this code but its full of error
public class ElasticConnection {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName("host1"), 9300))
.addTransportAddress(new TransportAddress(InetAddress.getByName("host2"), 9300));
// on shutdown
client.close();
}
}
tell what jar should i add or what should i do, please help me out with this.
Thanks.
public RestHighLevelClient createESRestClient() {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(esUserName, esPassword));
RestClientBuilder restClientBuilder = RestClient
.builder(new HttpHost(esRestclientHost, 9200, "http"));
// Use this one if your ElasticSearch server is setup to use username & password authentication
if (esAuthentication) {
restClientBuilder.setHttpClientConfigCallback(h -> h.setDefaultCredentialsProvider(credentialsProvider));
}
return new RestHighLevelClient(restClientBuilder);
}
You can use the following code to create your ElasticSearch connection. RestHighLevelClient is the new client which replaced TransportClient, I suggest that you use RestHighLevelClient.
For your reference this is listed in the ElasticSearch documentation guide: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.3/java-rest-high-getting-started-initialization.html
Don't use the TransportClient - it is deprecated. use the high level rest client: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.3/java-rest-high.html.
Also, note that the rest port is 9200, for when using the java high level rest client.
another option that you can use and worth mentioning is Jest: https://github.com/searchbox-io/Jest/tree/master/jest
I want to know the count of all the documetns present in an index,is it possible to get the count using java high level rest client COUNT API?
You can get the count of all the documents in an index either using cat count or Count API.
If you're using elasticsearch version 6.6 and above then you can follow this link to get the count using the Java High Level REST Client's Count API.
If you're using older versions then you have to use the Java Low Level REST Client to get the doc count.
As a RestHighLevelClient is built on top of Low Level REST Client.
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
you can use this to get the low level client from RestHighLevelClient:
RestClient lowLevelClient = client.getLowLevelClient();
Perform the following command for elasticsearch version 6.3 and lower:
Response response = client.getLowLevelClient().performRequest("GET", indexName+"/_count");
Perform the following for elasticsearch version 6.3 till 6.5:
Request request = new Request("GET", indexName+"/_count");
client.getLowLevelClient().performRequest(request);
Convert the response into String:
String responseBody = EntityUtils.toString(response.getEntity());
Then you can parse the responseBody to get the count value.
For example: I can get 'max_result_window' value using an HTTP request, like http://esIp:9200/index/_settings.
But I want to get this value in Java runtime environment using [elasticsearch client, Java Api]. I look through the official document but result in finding no relevant infomation about it.
So, How can I get 'max_result_window' value using [Elasticsearch Clients, Java API]?
If you are talking about native transport client you can use admin API. Here is the example:
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getLoopbackAddress(), 9300));
GetSettingsResponse response = client.admin().indices().prepareGetSettings("test").get();
String maxResultWindow = response.getSetting("test", "index.max_result_window");
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 e.g. 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();