# How to Get a specific Field Id from JSON Index using Low Level Rest Client API

**URL:** https://discuss.elastic.co/t/how-to-get-a-specific-field-id-from-json-index-using-low-level-rest-client-api/189318
**Category:** Elasticsearch
**Created:** [July 8, 2019, 9:55am UTC](https://discuss.elastic.co/t/how-to-get-a-specific-field-id-from-json-index-using-low-level-rest-client-api/189318 "2019-07-08T09:55:18Z")
**Posts on this page:** 5
**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: [July 8, 2019, 9:55am UTC](https://discuss.elastic.co/t/how-to-get-a-specific-field-id-from-json-index-using-low-level-rest-client-api/189318/1 "2019-07-08T09:55:18Z")

</div>

Hello Folks,  
i have this JSON DATA in Elasticsearch , and i want to get a specific "id" from it using Low Level Rest Clien API in java.  
this is the JSON DATA ,

```auto
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "try1",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"target": {
"id": 2,
"array_id": [
"element 1"
,
"element 2"
]
},
"declaration": {
"nummber_id": 1311,
"name": "lucas",
},
"text": "welcome to the new world"
}
}
]
}
}

```

i want to get the all Content of this Index at first.  
And i want to get after that just ("id": 2) under the target Field.  
is that possible in Java Low Level Rest client API ? and how ?

my Output should be like example,

```auto
{
"_index": "try1",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"target": {
"id": 2,
"array_id": [
"element 1"
,
"element 2"
]
},
"declaration": {
"nummber_id": 1311,
"name": "lucas",
},
"text": "welcome to the new world"
}
}

```

OR just the "id": 2

```auto
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "try1",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"target": {
"id": 2
}
}
}
]
}

```

can i start with somthing like this bellow ? ,

```auto
GetResponse response = client.prepareGet("try1", "_doc",
                            "1").setFields("target", "id")
                            .execute().actionGet();

```

The Goal from all this Question is , i do not want to send JSON Data wich have the same "id" inside it into Elasticsearch. is that possible to do using Client API low Level???  
Èxample bellow to avoid two "\_source" in one Index, wich they have under "target" the same ("id": 2 ), this case i want to avoid , is that possible?

```auto
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "try1",
"_type": "_doc",
"_id": "2",
"_score": 1,
"_source": {
"target": {
"id": 2,
"array_id": [
"element 1"
,
"element 2"
]
},
"declaration": {
"nummber_id": 1311,
"name": "lucas"
},
"text": "welcome to the new world"
}
}
,
{
"_index": "try1",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"target": {
"id": 2,
"array_id": [
"element 1"
,
"element 2"
]
},
"declaration": {
"nummber_id": 1311,
"name": "lucas",
},
"text": "hallo here is the text"
}
}
]
}
}

```

thx.

---

<div class="post-metadata">

### Author: ![HenningAndersen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henningandersen/32/48188_2.png) [@HenningAndersen](https://discuss.elastic.co/u/HenningAndersen)
#### Post date: [July 8, 2019, 12:44pm UTC](https://discuss.elastic.co/t/how-to-get-a-specific-field-id-from-json-index-using-low-level-rest-client-api/189318/2 "2019-07-08T12:44:15Z")

</div>

Hi @samyo,

it sounds like you want `target.id` to be unique within your index? ES does not have unique constraints, but you might be able to query for data having the same `target.id` before adding data. I think that is your suggestion?

This will require any indexing into this index to be serialized outside ES, since otherwise you risk two processes inserting the same `target.id` concurrently.

If you want to [search](https://www.elastic.co/guide/en/elasticsearch/reference/7.2/search-request-body.html) for documents with a certain `target.id` you must [refresh](https://www.elastic.co/guide/en/elasticsearch/reference/7.2/indices-refresh.html) the index before searching or maintain a data structure with documents indexed since last refresh.

If possible, promoting your `target.id` to be the document `_id` would be the easiest solution. Remember you can still search for documents with your original `id` (after a refresh).

---

<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 8, 2019, 1:26pm UTC](https://discuss.elastic.co/t/how-to-get-a-specific-field-id-from-json-index-using-low-level-rest-client-api/189318/3 "2019-07-08T13:26:51Z")

</div>

thx for Response,

**yes** i want to avoid sending two Data having the same **traget.id** into my Index.  
Is that possible to do in Low Level Rest Client API ? and how?

I tried something like this , but i do not know to to get the target.id from each IndexResponse from bellow Example, it is Pseudo Code right now, it does not work yet ,

```auto
String json1 = {"_index": "try1","_type": "_doc","_id": "1","_score": 1,"_source": {"target": {"id": 2,"array_id": ["element 1","element 2"]}}};

String json2 = {"_index": "try1","_type": "_doc","_id": "2","_score": 1,"_source": {"target": {"id": 2,"array_id": ["element 1","element 2"]}}};

IndexResponse response1 = client.prepareIndex("try1", "_doc", "1")
                                .setSource(json1, XContentType.JSON)
                                .execute().actionGet();

IndexResponse response2 = client.prepareIndex("try1", "_doc", "2")
                                .setSource(json2, XContentType.JSON)
                                .execute().actionGet();

if(target.id from the response1 == target.id from the response2){
than delete the response2, and send the response1.
}

```

How can i do the If query in Correct Why, so the Programm understand how to get **target.id** from the JSON Data json1 , and json2???

---

<div class="post-metadata">

### Author: ![HenningAndersen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henningandersen/32/48188_2.png) [@HenningAndersen](https://discuss.elastic.co/u/HenningAndersen)
#### Post date: [July 8, 2019, 1:53pm UTC](https://discuss.elastic.co/t/how-to-get-a-specific-field-id-from-json-index-using-low-level-rest-client-api/189318/4 "2019-07-08T13:53:34Z")

</div>

Hi @samyo,

rather than index the second doc and delete it, it is probably better to just avoid indexing the second document. You can simply search for docs having the same target id before indexing any doc. How to do that is given in the reference documentation here: [https://www.elastic.co/guide/en/elasticsearch/reference/7.2/search-request-body.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.2/search-request-body.html) . You may want to map `target.id` as [keyword](https://www.elastic.co/guide/en/elasticsearch/reference/7.2/analysis-keyword-analyzer.html).

---

<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: [August 5, 2019, 1:53pm UTC](https://discuss.elastic.co/t/how-to-get-a-specific-field-id-from-json-index-using-low-level-rest-client-api/189318/5 "2019-08-05T13:53:34Z")

</div>

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