Search code examples
sortingnullopensearch

If all fields for a column are null, OpenSearch does not include that field, and so sorting on that field fails


When adding sorting configuration for data in OpenSearch, I came across a situation where the data's field that I want to sort on had only null values. OpenSearch return an error that says [query_shard_exception] Reason: No mapping found for [MY_NULL_FIELD] in order to sort on. That being said, if I add ONE value, then the sort functions as expected. Is there a way around this?


Solution

  • If field values are all null in all documents, the field probably does not exist in the mapping. Using unmapped_type in the sort query should work.

    {
      "sort": [
        {
          "some_missing_field": {
            "order": "asc",
            "unmapped_type" : "long"
          }
        }
      ]
    }