'How to store BigDecimal type data in Elastic Search and sort by that value?

How do I store BigDecimal type data in Elastic Search and sort by that value?



Solution 1:[1]

Tldr;

I am afraid it is not supported. The closest you will get is double.

To test

In this quick test, I create 4 documents (that goes from bigger to smaller sorted on the _id)

POST /_bulk/
{"index":{"_index":"72344005","_id":"2"}}
{"data":123456789.123456789}
{"index":{"_index":"72344005","_id":"3"}}
{"data":-123456789.123456789}
{"index":{"_index":"72344005","_id":"1"}}
{"data":223456789.123456789}
{"index":{"_index":"72344005","_id":"0"}}
{"data":223456789.123456889}

GET /72344005/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "data": {
        "order": "desc"
      }
    }
  ]
}

The query give back documents sort like so 1, 0, 2, 3. Supporting the claim that we loose some precision. during the indexation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paulo