Terms aggs: only use ordinals on low-cardinality fields by default.

Close #5303
Fix sorting of NaN values in terms aggregations.

Closes #5236.
Added unit tests for terms and histogram sorting on sub aggregations that return NaN.
Fix NPE/AIOOBE when building a bucket which has not been collected.

Close #5048
Fix `metric` impl on unmapped fields.