# Sorting on dynamic nested object field

**URL:** <https://discuss.elastic.co/t/sorting-on-dynamic-nested-object-field/99570>\
**Category:** Elasticsearch\
**Created:** [September 6, 2017, 11:34am UTC](https://discuss.elastic.co/t/sorting-on-dynamic-nested-object-field/99570 "2017-09-06T11:34:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yesu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yesu/32/48857_2.png) [@yesu](https://discuss.elastic.co/u/yesu)\
**Post date:** [September 6, 2017, 11:34am UTC](https://discuss.elastic.co/t/sorting-on-dynamic-nested-object-field/99570/1 "2017-09-06T11:34:14Z")

</div>

Can I use sort on dynamic nested object field, indexing data like below.

Creating index like below with mappers.

```auto
PUT dynamic_index
{
   "mappings": {
      "dynamic_type": {
         "dynamic": true,
         "properties": {
            "o_field": {
               "type": "string",
               "fielddata": true
            }
         },
         "dynamic_templates": [
            {
               "nested_field": {
                  "mapping": {
                     "type": "nested",
                     "fielddata": true
                  },
                  "match": "nested_*"
               }
            }
         ]
      }
   }
}

```

```auto
PUT dynamic_index/dynamic_type/1
{
    "o_field" : "Kumar",
    "nested_field":{
    "meg1" : "msgl",
    "meg2" : "msg3"
    }
}

```

Using query like below:

```auto
POST dynamic_index/dynamic_type/_search
{
    "query": {
        "match_all": {}
    },
    "sort": [
       {
          "nested_field.meg1": {
             "order": "desc"
          }
       }
    ]
}

```

Can somebody help me to correct mapper and query both?

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [September 6, 2017, 4:34pm UTC](https://discuss.elastic.co/t/sorting-on-dynamic-nested-object-field/99570/2 "2017-09-06T16:34:34Z")

</div>

Your nested sort is not correct, it should give the nested path: [https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-request-sort.html#nested-sorting](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-request-sort.html#nested-sorting)

---

<div class="post-metadata">

**Author:** ![yesu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yesu/32/48857_2.png) [@yesu](https://discuss.elastic.co/u/yesu)\
**Post date:** [September 6, 2017, 5:07pm UTC](https://discuss.elastic.co/t/sorting-on-dynamic-nested-object-field/99570/3 "2017-09-06T17:07:56Z")

</div>

Though it has been used like below its throwing some error related to fielddata.

```auto
POST dynamic_index/dynamic_type/_search
{
   "query": {
      "match_all": {}
   },
   "sort": [
      {
         "nested_field.meg1": {
            "nested_path": "nested_field",
            "order": "desc"
         }
      }
   ]
}

```

Throwing below error:

```auto
{
   "error": {
      "root_cause": [
         {
            "type": "illegal_argument_exception",
            "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [nested_field.meg1] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
         }
      ],
      "type": "search_phase_execution_exception",
      "reason": "all shards failed",
      "phase": "query",
      "grouped": true,
      "failed_shards": [
         {
            "shard": 0,
            "index": "dynamic_index",
            "node": "g35VL1_iSDiHYrvC9t_i6w",
            "reason": {
               "type": "illegal_argument_exception",
               "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [nested_field.meg1] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
            }
         }
      ],
      "caused_by": {
         "type": "illegal_argument_exception",
         "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [nested_field.meg1] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
      }
   },
   "status": 400
}

```

Maybe i am doing something wrong while creating index like below in "dynamic\_templates" section:

```auto
PUT dynamic_index
{
   "mappings": {
      "dynamic_type": {
         "dynamic": true,
         "properties": {
            "o_field": {
               "type": "string",
               "fielddata": true
            }
         },
         "dynamic_templates": [
            {
               "nested_field": {
                  "mapping": {
                     "type": "nested",
                     "fielddata": true
                  },
                  "match": "nested_*"
               }
            }
         ]
      }
   }
}

```

---

<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:** [October 4, 2017, 5:08pm UTC](https://discuss.elastic.co/t/sorting-on-dynamic-nested-object-field/99570/4 "2017-10-04T17:08:09Z")

</div>

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