Sorting on nested array attributes
I have read this documentationand this ADR related to sorting.
The question I have is do we support sorting of records based on particular elements in the array with a filter condition?
We have a use case to sort the Wellbore ("osdu:wks:master-data--Wellbore:1.0.0") records based on the VerticalMeasurement value of type KB. VerticalMeasurements is an array in the data block in Wellbore records. Each record can have an array of VerticalMeasurements So if I have 5 records which contain values like below for VerticalMeasurements:
"id":"wellbore1",
"VerticalMeasurements": [
{
"VerticalMeasurementID": "well header elevation KB",
"VerticalMeasurementPathID": "tenant1:reference-data--VerticalMeasurementPath:ELEV:",
"VerticalMeasurement": 10,
"VerticalMeasurementTypeID": "tenant1:reference-data--VerticalMeasurementType:KB:"
},
{
"VerticalMeasurementID": "well header elevation GL",
"VerticalMeasurementPathID": "tenant1:reference-data--VerticalMeasurementPath:ELEV:",
"VerticalMeasurement": 50,
"VerticalMeasurementTypeID": "tenant1:reference-data--VerticalMeasurementType:GR:",
}]
"id":"wellbore2",
"VerticalMeasurements": [
{
"VerticalMeasurementID": "well header elevation KB",
"VerticalMeasurementPathID": "tenant1:reference-data--VerticalMeasurementPath:ELEV:",
"VerticalMeasurement": 20,
"VerticalMeasurementTypeID": "tenant1:reference-data--VerticalMeasurementType:KB:"
},
{
"VerticalMeasurementID": "well header elevation GL",
"VerticalMeasurementPathID": "tenant1:reference-data--VerticalMeasurementPath:ELEV:",
"VerticalMeasurement": 40,
"VerticalMeasurementTypeID": "tenant1:reference-data--VerticalMeasurementType:GR:",
}]
"id":"wellbore3",
"VerticalMeasurements": [
{
"VerticalMeasurementID": "well header elevation KB",
"VerticalMeasurementPathID": "tenant1:reference-data--VerticalMeasurementPath:ELEV:",
"VerticalMeasurement": 30,
"VerticalMeasurementTypeID": "tenant1:reference-data--VerticalMeasurementType:KB:"
},
{
"VerticalMeasurementID": "well header elevation GL",
"VerticalMeasurementPathID": "tenant1:reference-data--VerticalMeasurementPath:ELEV:",
"VerticalMeasurement": 30,
"VerticalMeasurementTypeID": "tenant1:reference-data--VerticalMeasurementType:GR:",
}]
"id":"wellbore4",
"VerticalMeasurements": [
{
"VerticalMeasurementID": "well header elevation KB",
"VerticalMeasurementPathID": "tenant1:reference-data--VerticalMeasurementPath:ELEV:",
"VerticalMeasurement": 40,
"VerticalMeasurementTypeID": "tenant1:reference-data--VerticalMeasurementType:KB:"
},
{
"VerticalMeasurementID": "well header elevation GL",
"VerticalMeasurementPathID": "tenant1:reference-data--VerticalMeasurementPath:ELEV:",
"VerticalMeasurement": 20,
"VerticalMeasurementTypeID": "tenant1:reference-data--VerticalMeasurementType:GR:",
}]
"id":"wellbore5",
"VerticalMeasurements": [
{
"VerticalMeasurementID": "well header elevation KB",
"VerticalMeasurementPathID": "tenant1:reference-data--VerticalMeasurementPath:ELEV:",
"VerticalMeasurement": 50,
"VerticalMeasurementTypeID": "tenant1:reference-data--VerticalMeasurementType:KB:"
},
{
"VerticalMeasurementID": "well header elevation GL",
"VerticalMeasurementPathID": "tenant1:reference-data--VerticalMeasurementPath:ELEV:",
"VerticalMeasurement": 10,
"VerticalMeasurementTypeID": "tenant1:reference-data--VerticalMeasurementType:GR:",
}]
then we want to sort the records based on value of data.VerticalMeasurements.VerticalMeasurement where data.VerticalMeasurement.VerticalMeasurementTypeID is tenant1:reference-data--VerticalMeasurementType:KB So the EXPECTED result of search query
{
"kind": "osdu:wks:master-data--Wellbore:1.0.0",
"query":"nested(data.VerticalMeasurements, (VerticalMeasurementTypeID:\"tenant1:reference-data--VerticalMeasurementType:KB\"))",
"sort": {
"field": [
"nested(data.VerticalMeasurements, VerticalMeasurement, min)"
],
"order": [
"DESC"
]
}
}
should be like below
{
"id":"wellbore5",
"id":"wellbore4",
"id":"wellbore3",
"id":"wellbore2",
"id":"wellbore1"
}
This currently doesn't seem possible today.
So the records get sorted on the minimum values from the VerticalMeasurements array and the ACTUAL response is
{
"id":"wellbore1",
"id":"wellbore2",
"id":"wellbore3",
"id":"wellbore4",
"id":"wellbore5"
}
This is because minimum values from VerticalMeasurements array are for VerticalMeasurementType:GR and sorting happens on the mode specified in the query which is 'min'.
The below query which specifies filter within sort clause also gives a similar response.
{
"kind": "osdu:wks:master-data--Wellbore:1.0.0",
"query":"nested(data.VerticalMeasurements, (VerticalMeasurementTypeID:\"tenant1:reference-data--VerticalMeasurementType:KB\"))",
"sort": {
"field": [
"nested(data.VerticalMeasurements, VerticalMeasurement, min)"
],
"order": [
"ASC"
],
"filter":["nested(data.VerticalMeasurements, VerticalMeasurementTypeID:\"tenant1:reference-data--VerticalMeasurementType:KB\", match)"]
}
}
Can the sorting be supported via search service where sorting of records happens on the field specified inside query or inside sort clause?
Let me know if this needs more clarification and requires an ADR or if this is something that can be fixed as an issue.