Elasticsearch Java High Level Rest client (deprecated) VS Java Client API - java

I am new to ElasticSearch and with our team we are developing a spring-boot application which communicates with an elasticsearch server. Actually the aim of the application is to map rest methods exposed by Elasticsearch in order to call the ES server from a Controller class with postman...
I've seen there is a brand new Java Client Api 8.4, but I cannot find in the documentation how to delete an indexed document for example. It seems that the Java Client Api is not such complete as the Rest Client Api.
So question is: what's the difference beetween Java Rest Client a Java Client API? which one should I use?
I know High level client is deprecated but as I mentioned I don't know how to call methods such as Delete By Query for examle...
I know also there is spring data elastic search for spring-boot but I would use the Java cliet which allows to work with raw json format
Thanks,
Saverio

Tldr;
The java API client documentation is purposefully short. On purpose it seems as it is already described in the main documentation of elasticsearch.
For a full reference, see the Elasticsearch documentation and in particular the REST APIs section. The Java API Client follows closely the JSON structures described there, using the Java API conventions.
Solution
Delete by query
It would most likely be like a search query by with a different function name instead.
SearchResponse<Product> response = esClient.search(s -> s // delete_by_query ?
.index("products")
.query(q -> q
.match(t -> t
.field("name")
.query(searchText)
)
),
Product.class
);

ElasticsearchClient has a delete() api (see javadoc) to remove documents from indexes and usage of it is not so different than the others.
For eg:
DeleteRequest request = DeleteRequest.of(i -> i.index("your-index").id("document-id"))
DeleteResponse<Product> response = client.delete(request);
should work.

Related

elastic search - Java api client range query

I am migrating a java application from elastic search high level client to java api client.
There is a range query like this in the code.
QueryBuilders.rangeQuery("startDate").lte(dateUtils.today())
Need to change this to java api client code.
Could someone help on this?
https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/searching.html
Query dateRangeQuery = RangeQuery.of(r -> r
.field("startDate")
.lte(dateUtils.today())
)._toQuery();

Java REST api PATCH request

I have to modify java based old project(servlet , Gradle project) which was not integrated with any of Java framework. For a recent project integration requirement, needs to call a external Api' PATCH request and change some value(owner ID) time to time on that external api hosted web application.
Endpoint looks like following
https://reverinapi/privivo/api/deys#/v1/drive/maks/{id}
Need to change owner id time to time and JSON should following,
{ "meta": { "ownerId": "smtip|appownid1" } }
I tried following way,
com.google.gson.JsonObject mainObject=new com.google.gson.JsonObject();
com.google.gson.JsonObject meta=new com.google.gson.JsonObject();
meta.addProperty("ownerId", "smtip|appownid1");
mainObject.add("meta", meta);
I don't familiar with how to call the api endpoint and please let me know if there any other efficient way to do this api call and change the value.
You need to use some HTTP client library to make the request. There are likely many available for Java, but Apache's is one.
Ah, I also just learnt that as of Java 11, there's an HTTP client included: https://www.baeldung.com/java-9-http-client.

How to pass the credential to Translation API v3?

I am using Google Cloud Translate v3 API Java Client. The Java Client code samples work great. The authentication of the code samples is done using the GOOGLE_APPLICATION_CREDENTIALS environment variable.
I need to develop code that will run in an application server that I have no control over the environment variables. So I need to use another way to get the call authenticated.
Setting Up Authentication for Server to Server Production Applications, under "Passing the path to the service account key in code" has a code sample (choose the JAVA tab) that works for the Storage service, but I can't find a similar way to pass the GoogleCredentials to Translation v3 API Java Client. Does anyone know?
Also, I can't find the Java doc for v3 API. https://googleapis.dev/java/google-cloud-clients/latest/index.html shows version "0.119.0-alpha" and it does not list the package com.google.cloud.translate.v3. Is there a separate java doc page ?
I think I found a solution.
The client API javadoc doesn't list com.google.cloud.translate.v3 but it does list com.google.cloud.translate.v3beta1. In the javadoc for TranslationServiceClient, i.e. https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/translate/v3beta1/TranslationServiceClient.html
there's a mention of setting credential, and this method worked!
TranslationServiceSettings translationServiceSettings =
TranslationServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
.build();
TranslationServiceClient translationServiceClient =
TranslationServiceClient.create(translationServiceSettings);

Magento REST API details or swagger doc

How to find the correct json schema to post to the magento endpoint and create an account?
Am able to create account only from the front end, I need to be able to do from the backend using curl or Postman.
http://{ipaddress}/index.php/customer/account/create/
This is an external api , i need to consume.
I want to post to this end point from java, however am not able to find the correct request schema of the json endpoint.
Any help to find the swagger end point or schema would be great
I am assuming you are talking about magento 1? Have you tried the REST API by chance? It's well written out on how you should make a call to it here.
If you need more functionality than just creating an account, you can also take a look at the SOAP API which handles more things, but does not support JSON from the box. They did implement the WS-I complaint mode to support Java calls, so maybe that's something you'd be interested in as well.
Good luck!

Vtiger CRM REST API using Spring RestTemplate

I am trying to create a Java wrapper for Vtiger REST API. I want to avoid the use of vtwsclib library because I could not find its Maven artifact. I want to use Spring RestTemplate. Actually, I don't understand why I need some special library to access REST API of a webservice.
First a got into problem with login process. Even when I followed the instructions from this link, I was not be able to retrieve sessionName. Finally, I resolved it after some research of vtwsclib library.
Next problem is with the retrieve operation. Even when I had sessionName and tried to retrieve some object by id with request (a ticket I can see in client app)
GET .../webservice.php?operation=retrieve&sessionName=xxxxx&id=xxxxx
I got:
{"success":false,"error":{"code":"ACCESS_DENIED","message":"Permission to perform the operation is denied for id"}}
Last problem is documentation, even when I visited their wiki Vtiger WIKI I could not find attributes of Ticket entity to create fields map.
So the work with this API is a bit painful for me. My questions are:
Is there some tutorial how to obtain sessionName using only Spring RestTemplate?
Why retrieve operation failed? update: bad id format
This is the main question. Is there some better documentation (tutorial, blog, file, ect.) for Vtiger REST API and descriptions of objects like Ticket?
I am also using Spring RestTemplate for Vtiger rest api.
For Retrieving details of record using REST API we need to pass id as moduleId x recordID (2x1234) format, otherwise it will give ACCESS_DENIED Error

Categories

Resources