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

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 ,

{
"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,

{
"_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

{
"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 ? ,

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?

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

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 for documents with a certain target.id you must refresh 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).

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 ,

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

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 . You may want to map target.id as keyword.

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