Any functional Tutorial about GetRequest and GetResponse Elasticsearch Version 6.3.1 ClientAPI Java

Hello Folks,
i am looking for a good Tutorial about GetRequest ,GetResponse and check Exists Field in JSON Data . I am using Elasticsearch 6.3.1 and Java 8.0 , i found this Page about Request and Response in Elasticsearch 6.0 , and some Functions are not compatible.

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.0/java-rest-high-document-get.html

when i write this

GetResponse getResponse = client.get(getRequest);

it could not recognise it , instead asked me to changed it to

ActionFuture<GetResponse> getResponse = client.get(getRequest);

Why is that?
And How can i improve it to check an existing Json Data in Elasticsearch ,
to avoid sending the same Data twice into Elastic???
I am using REST ClientAPI Java to creat Index in Elasticsearch and feed it with Json Data ,
those Data should not be similar, therefore i am asking for help , any Idea to check the Data bevor sending??

thx

Some few things:

GetResponse getResponse = client.get(request, RequestOptions.DEFAULT);

And How can i improve it to check an existing Json Data in Elasticsearch ,
to avoid sending the same Data twice into Elastic???

Well. What is the problem of sending it multiple times? As long as you are using the same id, the last call will just overwrite the existing one.
Not sure it is worth checking for every single call if the document already exist or not. You probably need to fix something in your application instead IMO.

Anyway, you can also use the Create API instead of Index API. That way if the document already exists the operation will just fail. See https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-index.html#_optional_arguments:

request.opType(DocWriteRequest.OpType.CREATE);

thx for Response ,
i need to check a specific field inside a JSON Data , so i can compare if other Data has the same field.
Like example , if i have two diffierent " Index id's " for two similar Data with the similar fields , this Case i want to avoid , the Question is how ?? And how to check if the similar Data in wich "id " exists?? overwriting in this Case is not helpfull.
Is the Elasticsearch Version 6.3.1 not compatible enough to handle this Case??

i want to translet this Example in this page in Java Code (the Exist Query) .
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html

To solve this, I'd probably generate an id which is a fingerprint of whatever content you want. So if 2 documents are similar (ie they are sharing the same fingerprint), you won't be able to index duplicates.

Is the Elasticsearch Version 6.3.1 not compatible enough to handle this Case??

The HL client 6.3.1 is probably not enough advanced but the 6.8.0 client might be useable with Elasticsearch Server 6.3.1. You need to test it.
Anyway, I'd recommend upgrading the server as well to 6.8.

Here is what I'd write with a 7.2.0 client (pretty similar with the 6.8.0 client):

try (RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(HttpHost.create("http://localhost:9200")))) {
    try {
        client.indices().delete(new DeleteIndexRequest("test"), RequestOptions.DEFAULT);
    } catch (ElasticsearchStatusException ignored) { }
    client.index(new IndexRequest("test").id("1").source("{\"foo\":\"bar\"}", XContentType.JSON), RequestOptions.DEFAULT);
    boolean exists1 = client.exists(new GetRequest("test", "1"), RequestOptions.DEFAULT);
    boolean exists2 = client.exists(new GetRequest("test", "2"), RequestOptions.DEFAULT);
    System.out.println("exists1 = " + exists1);
    System.out.println("exists2 = " + exists2);
} catch (Exception e) {
    e.printStackTrace(System.err);
}

thx for Response , i wrote a similar Code like yours, how can i get a specific Field from JSON Index, like follow , i have this example in Elasticsearch, after this Example i wrote a Request and Respons Index in order to get the "application_id": 6 field , but it did not print the "application_id": 6 on my Console , can you please check my Code down , am i missing anything?
am i using the source in the right why , give it the fieldname i want to look for and the value in that field??

"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "mytest",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"entity": {
"birthday_id": 0,
"application_id": 6,
"mission": [
"element 1"
,
"element 2"
]
},
"process": {
"login_id": 1311,
"page_name": "update"
},
"text": "hello world"
}
}
]
}
        try {
            client = new RestHighLevelClient(
                    RestClient.builder(
                            new HttpHost("localhost", 9200, "http")));
            IndexRequest request1 = new IndexRequest("mytest").id("1")
                    .source("{\"application_id\":\"6\"}", XContentType.JSON);
            System.out.println("request1 = " + request1);
            boolean exists1 = client.exists(new GetRequest("mytest", "1"), RequestOptions.DEFAULT);
            System.out.println("exists1 = " + exists1);
            GetRequest getrequest1 = new GetRequest("mytest", "1").routing("routing");
            System.out.println("response1 = " + getrequest1);
            IndexResponse response1 = client.index(request1, RequestOptions.DEFAULT);
            System.out.println("response1 = " + response1);
            client.close();

        } catch (Exception ep) {
            System.out.println("Error : " + ep.getMessage());
        }

thx

The Java example you shared is different than the JSON one (BTW please indent your code to make it readable).

In Java, you are writing a document like:

{ 
  "application_id":"6"
}

In Java, you are writing a document like:

{ 
  "application_id":"6"
}

The Json version is:

{
  "entity": {
    "application_id": 6
  }
}

Not the same field name application_id vs entity.application_id, not the same format number vs string... So probably not the same mapping.

About you Java code unless you have only one primary shard, the code is not going to work and probably to compile.
You are calling index method with a GetRequest. I doubt this can work.

thx for Advice , i modified the Code like below , but it updated the all JSON DATA and wrote inside the source just the "application_id": 6, so now my source is completely changed, that is not what i actually wanted, i just want to get the field "application_id": 6 from the JSON Index and print it to my Console.
Here what i got from the below Code,

{ "_index": "posts","_type": "_doc","_id": "1","_score": 1,"_source": { "application_id": 6}}
        try {
            client = new RestHighLevelClient(
                    RestClient.builder(
                            new HttpHost("localhost", 9200, "http")));
            boolean exists1 = client.exists(new GetRequest("mytest", "1"), RequestOptions.DEFAULT);

            IndexRequest request1 = new IndexRequest("mytest").id("1")
                    .source("{\"application_id\": 6}", XContentType.JSON);

          
                IndexResponse response1 = client.index(request1, RequestOptions.DEFAULT);

                System.out.println("request1 = " + response1);
                System.out.println("exists1 = " + exists1);
                System.out.println("request1 = " + request1);
                client.close();
} catch (Exception ep) {
            System.out.println("Error : " + ep.getMessage());
        }
        

Please any last Suggestion to hold just the field "application_id": 6 from my JSON Data and print it to my Console without changing the Original JSON Data in Elasticsearch??
thx.

I'm not sure I understand what it means. Do you have an example of what you'd like as an output?

yes the Output should just be the field "application_id": 6 from my JSON Data , printed to my Console. without chaning or updating the Original JSON Data in Elastic .

Output =>    {"application_id": 6}
or
Output=> { "_index": "posts","_type": "_doc","_id": "1","_score": 1,"_source": { "application_id": 6}}
or
Output=> ["_index": "posts","_type": "_doc","_id": "1","_score": 1,"_source": { "application_id": 6}]

one of those Output and i am happy .
thx

try (RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(HttpHost.create("http://localhost:9200")))) {
    try {
        client.indices().delete(new DeleteIndexRequest("test"), RequestOptions.DEFAULT);
    } catch (ElasticsearchStatusException ignored) { }
    client.index(new IndexRequest("test").id("1").source("{\"foo\":\"bar\", \"application_id\": 6}", XContentType.JSON), RequestOptions.DEFAULT);
    GetResponse getResponse = client.get(new GetRequest("test", "1").fetchSourceContext(
            new FetchSourceContext(true, new String[]{"application_id"}, null)
    ), RequestOptions.DEFAULT);
    System.out.println("doc = " + getResponse);
} catch (Exception e) {
    e.printStackTrace(System.err);
}

gives:

doc = {"_index":"test","_type":"_doc","_id":"1","_version":1,"_seq_no":0,"_primary_term":1,"found":true,"_source":{"application_id":6}}

sorry but the Code update always the Original JSON Data in Elasticsearch , that means, when i write in your Code

client.index(new IndexRequest("test").id("1").source("{\"foo\":\"bar\", \"application_id\": 10000}", XContentType.JSON), RequestOptions.DEFAULT);

Output is,
doc = {"_index":"test","_type":"_doc","_id":"1","_version":13,"found":true,"_source":{"application_id":10000}}

it print me the result with value 10000 , and change(update) my already exists Original JSON DATA in Elasticsearch. That what i wanted to avoid.
It seems "IndexRequest" did not hold the Field from my Original JSON DATA , it just overwrite it.
thx.

That's true. It's "updating" the whole document. Actually it is a Delete and an Index all together.
If your question was "how can I do partial updates of a document?" then have a look at https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html

Which translates in Java to https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-update.html

TBH I don't really like this API and found it useful if and only if you have very big documents to update like 100's of kb of JSON. In that unique moment it makes sense to me to avoid sending too much data on the network.
For smaller documents, then I always prefer sending the whole document again.

An Update API is basically doing:

  • GET the existing document
  • Merge it with what you sent within the Update Request
  • DELETE the existing document
  • INDEX the new version of the document

Where an Index API is doing:

  • DELETE the existing document if any
  • INDEX the new version of the document

My 2 cents

is there no Hold API from JSON Data without Updating or Deleting the existing Data?? Just hold the JSON DAta or a specific Part or Field from it , without doing anything else??
Cause i do not want to Update or change existing Data.
thx.

So I have absolutely no idea about what you want to do.
Could you share a full example, pure JSON based without any Java code and explain the result you are getting today and what do you want to get instead?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.