0

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 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The nested path thing wasn't required, that's for pre 6.5 I believe. Got it to work though, will mark yours up and add the solution, looks like I pasted the query a bit long.
Nested path will be required if the field is of nested doc.
0

Apparently changing the mapping to this:

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

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

1 Comment

The reason this worked is because metrics is now of object type rather than nested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.