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
Related
The OpenSearch Java Client GitHub repo states that the Java Client is the replacement for the High Level Java Client.
https://github.com/opensearch-project/opensearch-java
I'm trying to create an index with this client, but can't find any way to actually set the mappings, and there are no examples.
RestClient restClient = RestClient
.builder(new HttpHost(host, port, scheme))
.build();
Transport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
OpenSearchClient client = new OpenSearchClient(transport);
CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder()
.index(index)
// What do I do here?
.build();
CreateIndexResponse response = client.indices().create(createIndexRequest);
This is using the OpenSearch Java Client, not the High Level Client.
I'm sending REST requests in Java using a feign client which works perfectly fine, however when additionally using an OkHttpClient I get an error message
Caused by: feign.RetryableException: Malformed reply from SOCKS server executing GET
I identified the line of code causing this error which is
OkHttpClient.Builder builder = new OkHttpClient.Builder();
...
builder.proxy(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(socksProxyHost, socksProxyPort)));
However since I both need the OkHttpClient and to send requests using the feign client I can't just remove this but instead have to find a workaround. Is there a way to reset the proxy settings for as long as I'm sending the requests via feign and set them back afterwards? I tried setting the default proxy server to null using
proxySelector.setDefault(null)
but that unfortunately didn't resolve my issue.
Thanks for your help!
Create two instances of OkHttpClient, one with the proxy configured and one with none.
builder.proxy(Proxy.NO_PROXY);
If you use OkHttpClient.newBuilder() to create one client from the other they'll share an ExecutorService and other resources.
I fixed the Problem by assigning a new ProxySelector instead of the actual proxy. When overriding the select method I made sure the proxy list is empty for requests sent using the feign client:
ProxySelector proxySelector = new ProxySelector() {
#Override
public List<Proxy> select(URI uri) {
if (uri.toString().contains("HOST_NAME")) {
return List.of();
}
return List.of(
new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(socksProxyHost, socksProxyPort)));
}
#Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
}
};
builder.proxySelector(proxySelector);
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();
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);