Comparison between two objects with in the nested array of each document in elastic search using script

Hi, I have a Scenario in elasticsearch where i need to compare two objects which lies inside a nested array with in a document in elastic search, i have json like this stored in elastic search

"_index": "index",
"_type": "data",
"_id": "stream+0+19",
"_score": 1,
"_source": {
"objects": [
{
"value": 32.8,
"Id": "5c73db4e7a0000b0ca3f0d89"
},
{
"value": 39.7,
"Id": "5ca1b924b400006dcf585f82",
}
]
}
},
{
"_index": "index",
"_type": "data",
"_id": "stream+0+2",
"_score": 1,
"_source": {
"objects": [
{
"value": 42.8,
"Id": "5c73db4e7a0000b0ca3f0d89"
},
{
"value": 39.7,
"Id": "5ca1b924b400006dcf585f82"
},
{
"value": 19.7,
"Id": "5ca1b924b400006dcf585f867"
}
]
}
}

now with in the object array for each document i need to compare two objects for their value, for example we need to find that if their exists any two objects in nested array such that object1 has id 5c73db4e7a0000b0ca3f0d89 and object 2 has ID 5ca1b924b400006dcf585f82 and value of object 1(32.8) is less than value of object 2(39.7)

till now i have tried these things

tried to use for loop to iterate over the objects array in painless script,but length of array always comes 1

POST _index/_search
{
"query": {

"bool" : {
"must" : [
{
"nested" : {
"query" : {
"term" : {
"objects.Id" : {
"value" : "5c73db4e7a0000b0ca3f0d89",
"boost" : 1.0
}
}
},
"path" : "objects",
"ignore_unmapped" : false,
"score_mode" : "none",
"boost" : 1.0
}
},
{
"bool" : {
"must" : [
{
"nested" : {
"query" : {
"term" : {
"objects.Id" : {
"value" : "5ca1b924b400006dcf585f82",
"boost" : 1.0
}
}
},
"path" : "objects",
"ignore_unmapped" : false,
"score_mode" : "none",
"boost" : 1.0
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
},
{
"bool" : {
"must" : [
{
"nested" : {
"query" : {
"script" : {
"script" : {
"source": """
double a = 0.0;
double b = 0.0;
for (int i = 0; i < doc['objects.Id'].length; ++i) {
if(doc['objects.Id'][i] == '5c73db4e7a0000b0ca3f0d89') {
a = doc['objects.value'][i];
}
if(doc['objects.Id'][i] == '5ca1b924b400006dcf585f82') {
b = doc['objects.Value'][i];
}
}
return a < b && a!=0.0 && b!=0.0;

"""
},
"boost" : 1.0
}
},
"path" : "objects",
"ignore_unmapped" : false,
"score_mode" : "none",
"boost" : 1.0
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
}

What is the best way to achieved this functionality any help will be appreciated

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