0
votes

ElasticSearch 6.5.2 Given the mapping and query, the document order is not effected by changing 'desc' to 'asc' and vice versa. Not seeing any errors, just sort: [Infinity] in the results.

Mapping:

{
  "mappings": {
    "_doc": {
      "properties": {
        "tags": {
          "type": "keyword"
        },
        "metrics": {
          "type": "nested",
          "dynamic": true
        }
      }
    }
  }
}

Query

{
  "query": {
    "match_all": {
    }
  },
  "sort": [
    {
      "metrics.http.test.value": {
        "order": "desc"
      }
    }
  ]
}

Document structure:

{
  "tags": ["My Tag"],
  "metrics": {
    "http.test": {
      "updated_at": "2018-12-08T23:22:07.056Z",
      "value": 0.034
    }
  }
}
2

2 Answers

3
votes

When sorting by nested field it is necessary to tell the path of nested field using nested param. One thing more you were missing in the query is the field on which to sort. Assuming you want to sort on updated_at the query will be:

{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "metrics.http.test.updated_at": {
        "order": "desc",
        "nested": {
          "path": "metrics"
        }
      }
    }
  ]
}

One more thing that you should keep in mind while sorting using nested field is about filter clause in sort. Read more about it here.

0
votes

Apparently changing the mapping to this:

"metrics": {
  "dynamic": true,
  "properties": {}
}

Fixed it and allowed sorting to happen in the correct order.