# "sort" on multiple indices with a missing field

**URL:** <https://discuss.elastic.co/t/sort-on-multiple-indices-with-a-missing-field/100400>\
**Category:** Elasticsearch\
**Created:** [September 13, 2017, 6:47pm UTC](https://discuss.elastic.co/t/sort-on-multiple-indices-with-a-missing-field/100400 "2017-09-13T18:47:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimyoo](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jimyoo](https://discuss.elastic.co/u/jimyoo)\
**Post date:** [September 13, 2017, 6:47pm UTC](https://discuss.elastic.co/t/sort-on-multiple-indices-with-a-missing-field/100400/1 "2017-09-13T18:47:20Z")

</div>

Hi,

I see queries on multiple indices with "sort" with a missing field causes an exception (query and exception are attached). And,

- the same queries on individual index (either with or w/o a missing field) are fine
- tried a couple of scripts but can't make it work (scripts and logs are attached)
- Elasticsearch 5.3

Any insight, solution? Thanks in advance,

```
GET event-2017.09.1*/_search
{
  "sort":
     {
        "incidentLastSeen": {
           "order": "desc",
           "unmapped_type": "long"
        }
     }
}

{
   "error": {
      "root_cause": [],
      "type": "reduce_search_phase_exception",
      "reason": "[reduce] ",
      "phase": "query",
      "grouped": true,
      "failed_shards": [],
      "caused_by": {
         "type": "class_cast_exception",
         "reason": "java.lang.Long cannot be cast to java.lang.Float"
      }
   },
   "status": 503
}

== using script - using 'empty' ==
- index with a missing field: script_exception
- index w/o a missing field: OK

GET event-2017.09.10/_search
{
  "sort" : {
    "_script": {
        "script":"doc['incidentLastSeen'].empty ? 0 : doc['incidentLastSeen'].value",
        "type": "number",
        "order": "desc"
    }
  }
}

{
   "error": {
      "root_cause": [
         {
            "type": "script_exception",
            "reason": "runtime error",
            "script_stack": [
               "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:80)",
               "doc['incidentLastSeen'].empty ? 0 : doc['incidentLastSeen'].value",
               " ^---- HERE"
            ],
            "script": "doc['incidentLastSeen'].empty ? 0 : doc['incidentLastSeen'].value",
            "lang": "painless"
         }
      ],
      "type": "search_phase_execution_exception",
      "reason": "all shards failed",
      "phase": "query",
      "grouped": true,
      "failed_shards": [
         {
            "shard": 0,
            "index": "event-2017.09.10",
            "node": "wa6IdgVdSP2KuG6yZeU0xQ",
            "reason": {
               "type": "script_exception",
               "reason": "runtime error",
               "script_stack": [
                  "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:80)",
                  "doc['incidentLastSeen'].empty ? 0 : doc['incidentLastSeen'].value",
                  " ^---- HERE"
               ],
               "script": "doc['incidentLastSeen'].empty ? 0 : doc['incidentLastSeen'].value",
               "lang": "painless",
               "caused_by": {
                  "type": "illegal_argument_exception",
                  "reason": "No field found for [incidentLastSeen] in mapping with types []"
               }
            }
         }
      ],
      "caused_by": {
         "type": "script_exception",
         "reason": "runtime error",
         "script_stack": [
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:80)",
            "doc['incidentLastSeen'].empty ? 0 : doc['incidentLastSeen'].value",
            " ^---- HERE"
         ],
         "script": "doc['incidentLastSeen'].empty ? 0 : doc['incidentLastSeen'].value",
         "lang": "painless",
         "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "No field found for [incidentLastSeen] in mapping with types []"
         }
      }
   },
   "status": 500
}

== using script - using 'containsKey' ==
- script_exception on either with or w/o a missing field

GET event-2017.09.11/_search
{
  "sort" : {
    "_script": {
        "script":"doc.containsKey['incidentLastSeen'] ? doc['incidentLastSeen'].value : 0",
        "type": "number",
        "order": "desc"
    }
  }
}

{
   "error": {
      "root_cause": [
         {
            "type": "script_exception",
            "reason": "runtime error",
            "script_stack": [
               "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:80)",
               "doc.containsKey['incidentLastSeen'] ? doc['incidentLastSeen'].value : 0",
               " ^---- HERE"
            ],
            "script": "doc.containsKey['incidentLastSeen'] ? doc['incidentLastSeen'].value : 0",
            "lang": "painless"
         }
      ],
      "type": "search_phase_execution_exception",
      "reason": "all shards failed",
      "phase": "query",
      "grouped": true,
      "failed_shards": [
         {
            "shard": 0,
            "index": "event-2017.09.11",
            "node": "wa6IdgVdSP2KuG6yZeU0xQ",
            "reason": {
               "type": "script_exception",
               "reason": "runtime error",
               "script_stack": [
                  "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:80)",
                  "doc.containsKey['incidentLastSeen'] ? doc['incidentLastSeen'].value : 0",
                  " ^---- HERE"
               ],
               "script": "doc.containsKey['incidentLastSeen'] ? doc['incidentLastSeen'].value : 0",
               "lang": "painless",
               "caused_by": {
                  "type": "illegal_argument_exception",
                  "reason": "No field found for [containsKey] in mapping with types []"
               }
            }
         }
      ],
      "caused_by": {
         "type": "script_exception",
         "reason": "runtime error",
         "script_stack": [
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:80)",
            "doc.containsKey['incidentLastSeen'] ? doc['incidentLastSeen'].value : 0",
            " ^---- HERE"
         ],
         "script": "doc.containsKey['incidentLastSeen'] ? doc['incidentLastSeen'].value : 0",
         "lang": "painless",
         "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "No field found for [containsKey] in mapping with types []"
         }
      }
   },
   "status": 500
}
```

---

<div class="post-metadata">

**Author:** ![jimyoo](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jimyoo](https://discuss.elastic.co/u/jimyoo)\
**Post date:** [September 14, 2017, 8:44pm UTC](https://discuss.elastic.co/t/sort-on-multiple-indices-with-a-missing-field/100400/2 "2017-09-14T20:44:13Z")

</div>

Found the reason - in one of indices, the field was mapped to a different type (float). 😓

---

<div class="post-metadata">

**Author:** ![jimyoo](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jimyoo](https://discuss.elastic.co/u/jimyoo)\
**Post date:** [September 14, 2017, 11:54pm UTC](https://discuss.elastic.co/t/sort-on-multiple-indices-with-a-missing-field/100400/3 "2017-09-14T23:54:24Z")

</div>

Still, I can't figure why 1505172945000, 1111111543123 were mapped as 'float' (using dynamic mapping).

---

<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 12, 2017, 11:54pm UTC](https://discuss.elastic.co/t/sort-on-multiple-indices-with-a-missing-field/100400/4 "2017-10-12T23:54:27Z")

</div>

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