Search code examples
elasticsearchelasticsearch-6

Strange behavior when scaling_factor parameter is set


I'm using Elasticsearch 6.2.1 and now trying to understand how it works when scaling_factor paramerter is set to some value.

According to the documentation value of field is multiplied by scaling_factor and then stored as long at index time. When scaling_factor is 100 I expect that value 1.234 will be stored internally as 123 and after that can be found in index as 1.23. What I see now is value 1.234 does not lose its precision and I can get it from index in its original form: 1.234.

Is it a bug in this version of Elasticsearch? If not can someone explain what is happening under the hood?

UPDATE

@Val's answer clarified behavior of Elasticsearch but I'd like to understand why it is possible to find document by original raw value although index contains only scaled value. For example in case of the following mapping:

{
  "mappings": {
    "properties": {
      "num": {
        "type": "scaled_float",
        "scaling_factor": 100
      }
    }
  }
}

I can index document

{
  "num": 1.234
}

and then find it with the following query:

{
  "query": {
    "match": {
      "num": 1.234
    }
  }
}

I expected that query above would not find anything because index contains only rounded value 1.23.


Solution

  • If you see 1.234 in the source document, it's normal, ES never modifies anything in the source document itself.

    Given this mapping:

    PUT scaling
    {
      "mappings": {
        "properties": {
          "num": {
            "type": "scaled_float",
            "scaling_factor": 100
          }
        }
      }
    }
    

    If you store this document

    PUT scaling/_doc/1
    {
      "num": "1.234"
    }
    

    Then retrieving the document, will yield the same data as you indexed:

    GET scaling/_doc/1
    =>
    {
      "num": "1.234"
    }
    

    However, retrieving the doc value of the num field will yield what you expect:

    GET scaling/doc/_search
    {
      "docvalue_fields": ["num"],
      "script_fields": {
        "doc_value": {
          "script": {
            "source": "doc.num.value"
          }
        },
        "raw_value": {
          "script": {
            "source": "params._source.num"
          }
        }
      }
    }
    

    =>

      {
        "_index" : "scaling",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 1.0,
        "fields" : {
          "raw_value" : [
            "1.234"                  <--- raw value that was sent for indexing
          ],
          "doc_value" : [
            1.23                     <--- scaled doc value that was indexed
          ],
          "num" : [
            1.23                     <--- scaled doc value that was indexed
          ]
        }
      }