Hello,
this is Michael from germany and i am reaching out to you because i have to finish my master thesis before 31.10.2022.
I had working code in july 2022 which i didn't touch since then. I used the online 2 weeks trial vom elastic.co to connect and do my stuff. Now i want to execute the code again on a new 2 weeks trial but i am facing an error, which i can't solve.
What happened in the meanwhile:
a) in july i used version 8.2.0 of the software while meanwhile Elasticsearch company released
version 8.4.0 of their software.
b) i changed password in the code, because i have a new 2 weeks trial now.
Here are some short example codes which all fail (actually my real code is bigger but these are small examples). Only the code after the line // "productive code"
changes
Creating an index:
package Kapselung;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
public class create_index {
public static void main(String[] args) {
String username = "elastic";
String password = "JotkB5QY5v9IN0qRtvf6d5dL";
String host = "gattinger-fourth.kb.us-central1.gcp.cloud.es.io";
int port = 9243;
RestClient restClient;
ElasticsearchClient ESClient;
// https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/connecting.html
// https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_basic_authentication.html
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
HttpClientConfigCallback httpClientConfigCallback = new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
};
// Create the low-level client
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(host, port, "https"));
restClientBuilder.setHttpClientConfigCallback(httpClientConfigCallback);
restClient = restClientBuilder.build();
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ESClient = new ElasticsearchClient(transport);
// "productive code"
try {
ESClient.indices().create(c -> c.index("products100"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Failure is:
co.elastic.clients.transport.TransportException: [es/indices.create] Failed to decode error response
at co.elastic.clients.transport.rest_client.RestClientTransport.getHighLevelResponse(RestClientTransport.java:290)
at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:147)
at co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient.create(ElasticsearchIndicesClient.java:266)
at co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient.create(ElasticsearchIndicesClient.java:282)
at Kapselung.create_index.main(create_index.java:51)
Caused by: org.elasticsearch.client.ResponseException: method [PUT], host [https://gattinger-fourth.kb.us-central1.gcp.cloud.es.io:9243], URI [/products100], status line [HTTP/1.1 404 Not Found]
... 5 more
Or posting a document:
package Kapselung;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import com.fasterxml.jackson.databind.ObjectMapper;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
public class post_document {
public static void main(String[] args) {
String username = "elastic";
String password = "JotkB5QY5v9IN0qRtvf6d5dL";
String host = "gattinger-fourth.kb.us-central1.gcp.cloud.es.io";
int port = 9243;
RestClient restClient;
ElasticsearchClient ESClient;
// https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/connecting.html
// https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_basic_authentication.html
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
HttpClientConfigCallback httpClientConfigCallback = new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
};
// Create the low-level client
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(host, port, "https"));
restClientBuilder.setHttpClientConfigCallback(httpClientConfigCallback);
restClient = restClientBuilder.build();
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ESClient = new ElasticsearchClient(transport);
// "productive code"
try {
Request request = new Request("POST", "/products100/_doc");
String document = "{\"sku\": \"21847816\",\"name\": \"Rasierapparat\",\"price\": 23.99}";
ObjectMapper mapper = new ObjectMapper(); // Jackson Json
request.setJsonEntity(mapper.writeValueAsString(document));
Response response = restClient.performRequest(request);
} catch (Exception e) {
e.printStackTrace();
}
}
}
failure:
org.elasticsearch.client.ResponseException: method [POST], host [https://gattinger-fourth.kb.us-central1.gcp.cloud.es.io:9243], URI [/products100/_doc], status line [HTTP/1.1 404 Not Found]
{"statusCode":404,"error":"Not Found","message":"Not Found"}
at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:346)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:312)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:287)
at Kapselung.post_document.main(post_document.java:59)
Or just checking the Elasticsearch version:
package Kapselung;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
public class getESClient_info_version_number {
public static void main(String[] args) {
String username = "elastic";
String password = "JotkB5QY5v9IN0qRtvf6d5dL";
String host = "gattinger-fourth.kb.us-central1.gcp.cloud.es.io";
int port = 9243;
RestClient restClient;
ElasticsearchClient ESClient;
// https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/connecting.html
// https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_basic_authentication.html
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
HttpClientConfigCallback httpClientConfigCallback = new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
};
// Create the low-level client
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(host, port, "https"));
restClientBuilder.setHttpClientConfigCallback(httpClientConfigCallback);
restClient = restClientBuilder.build();
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ESClient = new ElasticsearchClient(transport);
// "productive code"
try {
System.out.println(ESClient.info().version().number());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Failure:
co.elastic.clients.transport.TransportException: [es/info] Missing [X-Elastic-Product] header. Please check that you are connecting to an Elasticsearch instance, and that any networking filters are preserving that header.
at co.elastic.clients.transport.rest_client.RestClientTransport.checkProductHeader(RestClientTransport.java:351)
at co.elastic.clients.transport.rest_client.RestClientTransport.getHighLevelResponse(RestClientTransport.java:253)
at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:147)
at co.elastic.clients.elasticsearch.ElasticsearchClient.info(ElasticsearchClient.java:976)
at Kapselung.getESClient_info_version_number.main(getESClient_info_version_number.java:51)
Caused by: org.elasticsearch.client.ResponseException: method [GET], host [https://gattinger-fourth.kb.us-central1.gcp.cloud.es.io:9243], URI [/], status line [HTTP/1.1 200 OK]
And this is my pom.xml:
4.0.0
00003
00004
0.0.1-SNAPSHOT
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.2.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
<!-- benötigt falls ClassNotFoundException: jakarta.json.spi.JsonProvider
https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/installation.html -->
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.0.1</version>
</dependency>
<!-- -->
```
```
Changing dependency from 8.2.0 to 8.4.0 didn't help.
So i don't know what actually to do. Do you?
Yes i know that i spread the password, it is intentional so somebody could help me by actually executing it. It is only a 2 weeks trial so i don't care about the logindata.
Thanks - Enomine