Sorting on an unmapped nested field throws an error

Hi!

I'm trying to sort on unmapped nested fields.
Due to our dynamic_template i run into the issue, that i do not have the specific nested field mapping until that field get's indexed in the feature.

Searching / etc. works, but sorting seems to be quite tricky in that case.
I've already tried to use the unmapped_type and missing property for the nested field. But it still throws an error!

E.g. when i try to add the following sort to the query:

"sort" : [
       {
        "NESTED.name" : {
              "order" : "desc",
              "nested_path": "NESTED"
              "unmapped_type" : "keyword",
              "missing": "_last"
            }
          }
         ]

I receive the following error:
[nested] failed to find nested object under path [NESTED]

My expected behaviour: Sorting against an unmapped nested field should be on the last position in this case.

Did i do something wrong? Please help me :slight_smile:

I am working with ES 5.5.1

Hi @clackner,

Looking at the error [nested] failed to find nested object under path [NESTED] it seems that your field is not properly mapped as nested.

Please, consider this full example:

PUT nested_example
{
  "mappings": {
    "doc": {
      "properties": {
        "offer": {
          "type": "nested" 
        }
      }
    }
  }
}

PUT nested_example/doc/1
{
  "offer" : [
    {
      "first" : "John",
      "last" :  "Smith",
      "price": 10
    }
  ]
}

PUT nested_example/doc/2
{
  "offer" : [
    {
      "first" : "John",
      "last" :  "Smith",
      "price": 20
    }
  ]
}

Sort on unmapped field:

GET nested_example/_search
{
  "sort": [
    {
      "offer.do_not_exists.keyword": {
        "unmapped_type": "keyword",
        "nested": {
          "path": "offer"
        }
      }
    }
  ]
}

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_index": "nested_example",
        "_type": "doc",
        "_id": "2",
        "_score": null,
        "_source": {
          "offer": [
            {
              "first": "John",
              "last": "Smith",
              "price": 20
            }
          ]
        },
        "sort": [
          null
        ]
      },
      {
        "_index": "nested_example",
        "_type": "doc",
        "_id": "1",
        "_score": null,
        "_source": {
          "offer": [
            {
              "first": "John",
              "last": "Smith",
              "price": 10
            }
          ]
        },
        "sort": [
          null
        ]
      }
    ]
  }
}

Hope it helps.

Cheers,
LG

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