Sorting by specific nested object

Hi everyone, I'm trying to sort by a nested object without any results.
This is the mapping of the object:

{
    "settings" : {
        "number_of_shards" : 5,
        "number_of_replicas": 1
    },
    "mappings" : {
        "properties" : {
            "id" : { "type" : "integer" },
             .....
            "fieldsets": {
                "type": "nested",
                "properties": {
                    "id": { "type": "integer" },
                    "unique_name": { "type": "keyword" },
                    "visible": { "type": "boolean" },
                    "fields": {
                        "type": "nested",
                        "properties": {
                            "id": { "type": "integer" },
                            "field": { "type": "keyword" },
                            "unique_name": { "type": "keyword" },
                            "input": {
                                "type": "nested",
                                "properties": {
                                    "id": { "type": "integer" },
                                    "type": { "type": "keyword" },
                                    "name": { "type": "keyword" },
                                    "required": { "type": "boolean" },
                                    "value": { "type": "keyword" }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

What I need to do is sorting root object by a specific nested fields object. For examples I have this two objects:

{
    "id": 1,
    "fieldsets": [
        {
            "id": 9275,
            "unique_name": "basic_personal_data",
            "fieldset": "Data",
            "visible": true,
            "fields": [
                {
                    "id": 24379,
                    "field": "Name",
                    "unique_name": "name",
                    "input": {
                        "id": 24379,
                        "type": "text",
                        "name": "Name",
                        "required": true,
                        "value": "Elon"
                    }
                },
                {
                    "id": 24389,
                    "field": "Surname",
                    "unique_name": "name",
                    "input": {
                        "id": 24379,
                        "type": "text",
                        "name": "Surname",
                        "required": true,
                        "value": "Musk"
                    }
                }
            ]
        }
    ]
}


{
    "id": 2,
    "fieldsets": [
        {
            "id": 9275,
            "unique_name": "basic_personal_data",
            "fieldset": "Data",
            "visible": true,
            "fields": [
                {
                    "id": 24379,
                    "field": "Name",
                    "unique_name": "name",
                    "input": {
                        "id": 24379,
                        "type": "text",
                        "name": "Name",
                        "required": true,
                        "value": "Bill"
                    }
                },
                {
                    "id": 24389,
                    "field": "Surname",
                    "unique_name": "name",
                    "input": {
                        "id": 24379,
                        "type": "text",
                        "name": "Surname",
                        "required": true,
                        "value": "Gates"
                    }
                }
            ]
        }
    ]
}

What I need is to sort the root objects by name or surname. This is what I tried without success:

{
  "sort": [
    {
      "fieldsets.fields.input.value": {
        "order": "asc",
        "nested": { 
          "path": "fieldsets.fields",
          "filter": {
            "term": {
              "fieldsets.fields.unique_name": "surname" 
            }
          }
        }
      }
    }
  ]
}

The other way i thought is to put the nested object "fields" in an index saving an identifier of my root object, order it, and then executing another in query to get the root object.
Doing this way isn't a real problem, just a replication of data and doubling the query. So i'm wondering if there is a way to do in the same index.

Any suggestions?
Thank you.

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