Sorting on dynamic nested object field

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

Creating index like below with mappers.

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_*"
               }
            }
         ]
      }
   }
}
PUT dynamic_index/dynamic_type/1
{
    "o_field" : "Kumar",
    "nested_field":{
    "meg1" : "msgl",
    "meg2" : "msg3"
    }
}

Using query like below:

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?

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

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

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

Throwing below error:

{
   "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:

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_*"
               }
            }
         ]
      }
   }
}

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