Java High Level Rest Client - Mutli Search API Not working with Elasticsearch 8.3.2 - elasticsearch

Im using Java High level rest client version 7.17.5. Im applying the same but it doesnt work. Please find the below code
SearchSourceBuilder metaDataSearch = new SearchSourceBuilder();
org.elasticsearch.action.search.SearchRequest metaSearch = new org.elasticsearch.action.search.SearchRequest().source(metaDataSearch).indices(indexName);
SearchSourceBuilder keywordExactRequestBuilder = new SearchSourceBuilder().size(10);
keywordExactRequestBuilder.query(QueryBuilders.matchAllQuery());
org.elasticsearch.action.search.SearchRequest keywordExactRequestSearch = new org.elasticsearch.action.search.SearchRequest().source(keywordExactRequestBuilder).indices(indexName);
MultiSearchRequest multiSearchRequest = new MultiSearchRequest()
.add(metaSearch)
.add(keywordExactRequestSearch);
MultiSearchResponse multiSearchResponse = null;
try {
multiSearchResponse = elasticClient.msearch(multiSearchRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
Im facing the below exception
org.jboss.resteasy.spi.UnhandledException: ElasticsearchStatusException[Elasticsearch exception [type=illegal_argument_exception, reason=key [types] is not supported in the metadata section]]
.......
Caused by:
ElasticsearchStatusException[Elasticsearch exception [type=illegal_argument_exception, reason=key [types] is not supported in the metadata section]]
......
Im enabled API compatibility mode. Im using elasticsearch version 8.3.2
Thanks

Related

No server chosen by com.mongodb.async.client.ClientSessionHelpe from cluster description ClusterDescription

I am trying to connect to aws DocumentDB with async mongoClient.
I created a DocumentDB cluster in aws and success connect via ssh command line.
I went over here and created MongoClient and success connected and insert events.
But when I tried create com.mongodb.async.client.MongoClient, connection failed with folowing error:
No server chosen by WritableServerSelector from cluster description
ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE,
serverDescriptions=[ServerDescription{address=aws-cluster:27017,
type=UNKNOWN, state=CONNECTING,
exception={com.mongodb.MongoSocketReadTimeoutException: Timeout while
receiving message}, caused by
{io.netty.handler.timeout.ReadTimeoutException}}]}. Waiting for 30000
ms before timing out.
ClusterSettings clusterSettings = ClusterSettings.builder()
.applyConnectionString(new ConnectionString(connectionString)).build();
List<MongoCredential> credentials = new ArrayList<>();
credentials.add(
MongoCredential.createCredential(
mongoUserName,
mongoDBName,
mongoPassword));
MongoClientSettings settings = MongoClientSettings.builder()
.credentialList(credentials)
.clusterSettings(clusterSettings)
.streamFactoryFactory(new NettyStreamFactoryFactory())
.writeConcern(WriteConcern.ACKNOWLEDGED)
.build();
com.mongodb.async.client.MongoClient mongoClient = MongoClients.create(settings);
MongoDatabase testDB = mongoClient.getDatabase("myDB");
MongoCollection<Document> collection = testDB.getCollection("test");
Document doc = new Document("name", "MongoDB").append("type", "database");
//**trying insert document => here I got an error**
collection.insertOne(doc, new SingleResultCallback<Void>() {
#Override
public void onResult(final Void result, final Throwable t) {
System.out.println("Inserted!");
}
});
Do you have any ideas, why does it happen?
I solved it by using uri:
String uri = "mongodb://<username>:<Password>#<hostname>:27017/?ssl=true&ssl_ca_certs=cert";
MongoClientSettings settings = MongoClientSettings.builder()
.streamFactoryFactory(new NettyStreamFactoryFactory())
.applyConnectionString(new ConnectionString(uri))
.build();
com.mongodb.async.client.MongoClient mongoClient = MongoClients.create(settings);
I encountered a similar error , for me it was related to the TLS configs.
I disabled the TLS in documentDB https://docs.aws.amazon.com/documentdb/latest/developerguide/security.encryption.ssl.html
In my case I had to restart the cluster after disabling the TLS. (TLS was not needed for the use case). After the restart the connection was established successfully.

Invalid NEST response built from a unsuccessful (401) low level call on HEAD

I am connecting to Elasticsearch cluster using Nest client and I'm getting this issue while adding Index records.
An i missing something?
Nest v 7.2.1
Elastic version 7.2.1
Exception
Failed to create DB Index for Alarm Event.Invalid NEST response built from a unsuccessful (401) low level call on HEAD: /
Audit trail of this API call
[1] BadResponse: Node: http://192.168.0.4:9200/ Exception: PipelineException Took: 00:00:00.0588591
var addresses = new[] {
new Uri("http://192.168.0.4:9200/"),
new Uri("http://192.168.0.5:9200/"),
new Uri("http://192.168.0.6:9200/"),
new Uri("http://192.168.0.7:9200/"),
};
connectionPool = new StaticConnectionPool(addresses);
connectionSettings = new ConnectionSettings(connectionPool).BasicAuthentication("testuser", "Something_123").DisablePing();
ElasticSearchClient = new ElasticClient(connectionSettings);
var indexes = GetAsxIndexes();
var response = ElasticSearchClient.Ping().DebugInformation;

ElasticsearchStatusException contains unrecognized parameter: [ccs_minimize_roundtrips]]]

I am trying to do a simple search on ElasticSearch server and getting teh following error
ElasticsearchStatusException[Elasticsearch exception [type=illegal_argument_exception, reason=request [/recordlist1/_search] contains unrecognized parameter: [ccs_minimize_roundtrips]]]
The query String :
{"query":{"match_all":{"boost":1.0}}}
I am using :
elasticsearch-rest-high-level-client (maven artifact)
SearchRequest searchRequest = new SearchRequest(INDEX);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(searchSourceBuilder);
try
{
System.out.print(searchRequest.source());
SearchResponse response = getConnection().search(searchRequest,RequestOptions.DEFAULT);
SearchHit[] results=response.getHits().getHits();
for(SearchHit hit : results)
{
String sourceAsString = hit.getSourceAsString();
System.out.println( gson.fromJson(sourceAsString, Record.class).year);
}
}
catch(ElasticsearchException e)
{
e.getDetailedMessage();
e.printStackTrace();
}
catch (java.io.IOException ex)
{
ex.getLocalizedMessage();
ex.printStackTrace();
}
This usually occurs on porting from elastic-search version 6.X.X to 7.X.X.
You should reduce the elastic-search version to 6.7.1 and try running it.
Since you are using maven you should make sure your dependencies should be like:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.7.1</version>
</dependency>
I ran into this same issue when i had by mistake my 6.5 cluster still running while using the 7.2 API. Once I started up my 7.2 cluster the exception went away.
Problem here is the movement of version, probably you were using elastic search 6.x.x and now using 7.x.x
You can definitely solve this by having your elastic search server of 7.x.x.
Elasticsearch 6.x.x used to have type of document
(where you could give type to your documents)
but Elasticsearch 7.x.x onwards it has no type or
default type _doc, so you need to have _doc as your type
while creating mapping.
Maybe you can find this from stackTrace of exception:
Suppressed: org.elasticsearch.client.ResponseException: method [POST], host [http://127.0.0.1:9200], URI [/recordlist1/_search?rest_total_hits_as_int=true&typed_keys=true&ignore_unavailable=false&expand_wildcards=open%2Cclosed&allow_no_indices=true&ignore_throttled=false&search_type=query_then_fetch&batched_reduce_size=512], status line [HTTP/1.1 400 Bad Request]
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"request [/_search] contains unrecognized parameters: [ignore_throttled], [rest_total_hits_as_int]"}],"type":"illegal_argument_exception","reason":"request [/_search] contains unrecognized parameters: [ignore_throttled], [rest_total_hits_as_int]"},"status":400}
So, You can try this GET method by curl, which come to the same error message.
curl -XGET http://127.0.0.1:9200/recordlist1/_search?rest_total_hits_as_int=true&typed_keys=true&ignore_unavailable=false&expand_wildcards=open%2Cclosed&allow_no_indices=true&ignore_throttled=false&search_type=query_then_fetch&batched_reduce_size=512
I've tried delete 'rest_total_hits_as_int=true' ... Case Closed.
You should check your es-server's version by elasticsearch -V and client’s version in maven.
In high-level client, they add rest_total_hits_as_int=true by default, and I find no access to set it to false.
you can refer to
org.elasticsearch.client.RequestConverters#addSearchRequestParams Line:395 <v6.8.10>
I had no other choice but matching client to match server.
Why it's so Exciting ?
ehn... after all, it is "High Level".

Create java RestHighLevelClient in elastic cluster mode

If elasticsearch runs on single mode, I can easily establish the RestHighLevel connection with this line of code:
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
But if my elastic cluster has 3 machines, e.g., "host1", "host2", "host3", how to create the rest high level client in cluster mode ?
Thanks
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("host1", 9200, "http"),
new HttpHost("host2", 9200, "http"),
new HttpHost("host2", 9200, "http")
)
);
As the doc it looks like you were referencing states, RestClient.builder accepts an array of HttpHosts to connect to. The client (which under the hood is the ES low-level REST client) will round-robin requests to these hosts. See also the Javadoc.
As per the Elasticsearch docs you can pass multiple Elasticsearch hosts in RestClient.builder().
The better solution is to load the Elasticsearch hosts from configuration(application.conf in case of Scala-based application) instead of hardcoding it in the codebase.
Here is the Scala-based solution using Java Varargs(:_*).
application.conf
es_hosts = ["x.x.x.x","x.x.x.x","x.x.x.x"] // You can even use service-name/service-discovery
es_port = 9200
es_scheme = "http"
Code snippet
import collection.JavaConverters._
import com.typesafe.config.ConfigFactory
import org.apache.http.HttpHost
import org.elasticsearch.client.{RestClient, RestHighLevelClient}
val config = ConfigFactory.load()
val port = config.getInt(ES_PORT)
val scheme = config.getString(ES_SCHEME)
val es_hosts = config.getStringList(ES_HOSTS).asScala
val httpHosts = es_hosts.map(host => new HttpHost(host, port, scheme))
val low_level_client = RestClient.builder(httpHosts:_*)
val high_level_client: RestHighLevelClient = new RestHighLevelClient(low_level_client)
To create High level REST client using multiple hosts, you can do something like following:
String[] esHosts = new String[]{"node1-example.com:9200", "node2-example.com:9200",
"node3-example.com:9200"};
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo(esHosts)
.build();
RestHighLevelClient restClient = RestClients.create(clientConfiguration).rest();
// Hostnames used for building client can be verified as following
List<Node> nodes = restClient.getLowLevelClient().getNodes();
nodes.forEach(node -> System.out.println(node.toString()));
References:
Docs for High Level REST Client
Source code for ClientConfigurationBuilder
I am creating my elastic REST client with below steps:
RestHighLevelClient client=null;
List<HttpHost> hostList = new ArrayList<>();
for (String host : hosts) {
String[] hostDetails = host.split("\\:");hostList.add(new
HttpHost(hostDetails[0],Integer.parseInt(hostDetails[1]),https));
}
try(RestHighLevelClient client1 = new RestHighLevelClient(
RestClient.builder(hostList.toArray(new
HttpHost[hostList.size()]))
.setHttpClientConfigCallback(
httpClientBuilder ->
// to do this only if auth is enabled
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))))
{
client = client1;
}
} catch (IOException e) {
log.error("exception occurred while setting elastic client");
}

DeleteByQueryPlugin cannot be resolved to a type && FRAMELESS_COMPILE_ENABLED : NoSuchFieldError

Iam having the following error when i upgraded the elasticsearch to 2.3.2 and used delete-by-query plugin . The code is something like this
Settings settings = Settings.settingsBuilder().build();
TransportClient transportClient = null;
for (String node : nodes.trim().split(",")) {
String[] parts = node.split(":");
transportClient=TransportClient.builder().settings(settings)
.addPlugin(DeleteByQueryPlugin.class).build();
transportClient = transportClient.addTransportAddress(
new InetSocketTransportAddress(
InetAddress.getByName(parts[0]),Integer.valueOf(parts[1])));
And the Error:
play.exceptions.CompilationException: DeleteByQueryPlugin cannot be resolved to a type
at play.classloading.ApplicationCompiler$2.acceptResult(ApplicationCompiler.java:246)
at org.eclipse.jdt.internal.compiler.Compiler.handleInternalException(Compiler.java:676)
at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:520)
at play.classloading.ApplicationCompiler.compile(ApplicationCompiler.java:282)
at play.classloading.ApplicationClasses$ApplicationClass.compile(ApplicationClasses.java:281)
at play.classloading.ApplicationClassloader.loadApplicationClass(ApplicationClassloader.java:166)
at play.classloading.ApplicationClassloader.loadClass(ApplicationClassloader.java:84)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at play.plugins.PluginCollection.loadPlugins(PluginCollection.java:168)
at play.Play.init(Play.java:303)
at play.server.Server.main(Server.java:162)
Exception in thread "main" java.lang.NoSuchFieldError: FRAMELESS_COMPILE_ENABLED
at play.modules.sass.Engine.<init>(Engine.java:44)
at play.modules.sass.Plugin.onLoad(Plugin.java:15)
at play.plugins.PluginCollection.initializePlugin(PluginCollection.java:251)
at play.plugins.PluginCollection.loadPlugins(PluginCollection.java:185)
at play.Play.init(Play.java:303)
at play.server.Server.main(Server.java:162)
Can any one give me any pointers how can i solve this error

Resources