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

**URL:** https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055
**Category:** Elasticsearch
**Created:** [June 28, 2019, 1:22pm UTC](https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055 "2019-06-28T13:22:38Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![samyo](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@samyo](https://discuss.elastic.co/u/samyo)
#### Post date: [June 28, 2019, 1:22pm UTC](https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055/1 "2019-06-28T13:22:38Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.0/java-rest-high-document-get.html)

when i write this

```auto
GetResponse getResponse = client.get(getRequest);

```

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

```auto
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

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [June 29, 2019, 1:08pm UTC](https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055/2 "2019-06-29T13:08:42Z")

</div>

Some few things:

- Update to 6.8.0
- Read the documentation like: [Get API | Java REST Client [7.17] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-get.html) where you'll see that type of code:

```auto
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 [Index API | Java REST Client [7.17] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-index.html#_optional_arguments:)

```auto
request.opType(DocWriteRequest.OpType.CREATE);

```

---

<div class="post-metadata">

### Author: ![samyo](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@samyo](https://discuss.elastic.co/u/samyo)
#### Post date: [July 1, 2019, 11:31am UTC](https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055/3 "2019-07-01T11:31:28Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html)

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 1, 2019, 12:00pm UTC](https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055/4 "2019-07-01T12:00:58Z")

</div>

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):

```auto
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);
}

```

---

<div class="post-metadata">

### Author: ![samyo](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@samyo](https://discuss.elastic.co/u/samyo)
#### Post date: [July 3, 2019, 10:27am UTC](https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055/5 "2019-07-03T10:27:50Z")

</div>

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??

```auto
"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"
}
}
]
}

```

```auto
        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

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 3, 2019, 11:28am UTC](https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055/6 "2019-07-03T11:28:51Z")

</div>

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:

```auto
{ 
  "application_id":"6"
}

```

In Java, you are writing a document like:

```auto
{ 
  "application_id":"6"
}

```

The Json version is:

```auto
{
  "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.

---

<div class="post-metadata">

### Author: ![samyo](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@samyo](https://discuss.elastic.co/u/samyo)
#### Post date: [July 3, 2019, 12:02pm UTC](https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055/7 "2019-07-03T12:02:42Z")

</div>

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,

```auto
{ "_index": "posts","_type": "_doc","_id": "1","_score": 1,"_source": { "application_id": 6}}

```

```auto
        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.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 3, 2019, 12:15pm UTC](https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055/8 "2019-07-03T12:15:20Z")

</div>

> [@samyo](#):
>
> 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??

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

---

<div class="post-metadata">

### Author: ![samyo](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@samyo](https://discuss.elastic.co/u/samyo)
#### Post date: [July 3, 2019, 12:22pm UTC](https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055/9 "2019-07-03T12:22:00Z")

</div>

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 .

```auto
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

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 3, 2019, 12:48pm UTC](https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055/10 "2019-07-03T12:48:23Z")

</div>

```auto
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:

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

```

---

<div class="post-metadata">

### Author: ![samyo](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@samyo](https://discuss.elastic.co/u/samyo)
#### Post date: [July 3, 2019, 1:00pm UTC](https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055/11 "2019-07-03T13:00:02Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 3, 2019, 1:05pm UTC](https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055/12 "2019-07-03T13:05:27Z")

</div>

> [@samyo](#):
>
> It seems "IndexRequest" did not hold the Field from my Original JSON DATA , it just overwrite it.

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 [Update API | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html)

Which translates in Java to [Update API | Java REST Client [7.17] | Elastic](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

---

<div class="post-metadata">

### Author: ![samyo](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@samyo](https://discuss.elastic.co/u/samyo)
#### Post date: [July 3, 2019, 1:08pm UTC](https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055/13 "2019-07-03T13:08:32Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 3, 2019, 1:25pm UTC](https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055/14 "2019-07-03T13:25:34Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 31, 2019, 1:25pm UTC](https://discuss.elastic.co/t/any-functional-tutorial-about-getrequest-and-getresponse-elasticsearch-version-6-3-1-clientapi-java/188055/15 "2019-07-31T13:25:40Z")

</div>

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