# Sorting on an unmapped nested field throws an error

**URL:** <https://discuss.elastic.co/t/sorting-on-an-unmapped-nested-field-throws-an-error/112783>\
**Category:** Elasticsearch\
**Created:** [December 21, 2017, 9:35am UTC](https://discuss.elastic.co/t/sorting-on-an-unmapped-nested-field-throws-an-error/112783 "2017-12-21T09:35:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![clackner](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@clackner](https://discuss.elastic.co/u/clackner)\
**Post date:** [December 21, 2017, 9:35am UTC](https://discuss.elastic.co/t/sorting-on-an-unmapped-nested-field-throws-an-error/112783/1 "2017-12-21T09:35:43Z")

</div>

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 🙂

I am working with ES 5.5.1

---

<div class="post-metadata">

**Author:** ![luiz.santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luiz.santos/32/24664_2.png) [@luiz.santos](https://discuss.elastic.co/u/luiz.santos)\
**Post date:** [December 29, 2017, 12:43pm UTC](https://discuss.elastic.co/t/sorting-on-an-unmapped-nested-field-throws-an-error/112783/2 "2017-12-29T12:43:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 26, 2018, 12:43pm UTC](https://discuss.elastic.co/t/sorting-on-an-unmapped-nested-field-throws-an-error/112783/3 "2018-01-26T12:43:59Z")

</div>

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