Elastic stores string in numeric (long) field

Hi just my 2 cents... got me thinking a bit

When a normal search is performed in elasticsearch it returns the source document as the _source, what the actual source document that was indexed .... the actual values are stored in the elsewhere (example) doc_values so as you can see below when I do a search that returns both. In this case when you send in a "2" elasticsearch can properly convert that to a number upon indexing but it does not alter the source, that is all that is happening if it is desired to for the source to be correct ... then the correct source needs to be indexed, elasticsearch does not alter the source document, it is not a compiler like C++, if it can index it will, it has some "liberalness" to it to ease compatibility with JSON etc. and ease ingestion, I think that is perhaps considered a feature not a bug.

However when you look at the actual doc_values you can see that in fact both are actually numeric fields.

DELETE my-index

PUT my-index

PUT my-index
{
  "mappings": {
    "properties": {
      "testId": {
        "type": "long"
      }
    }
  }
}

PUT my-index/_doc/1
{
  "testId": 1
}

PUT my-index/_doc/2
{
  "testId": "2"
}

PUT my-index/_doc/3
{
  "testId": "three"
}

GET my-index/_search
{
  "docvalue_fields": ["testId"], 
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "testId": {
              "gt": 0
            }
          }
        }
      ]
    }
  }
}


# Result
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "my-index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "testId" : 1
        },
        "fields" : {
          "testId" : [
            1
          ]
        }
      },
      {
        "_index" : "my-index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "testId" : "2"
        },
        "fields" : {
          "testId" : [
            2
          ]
        }
      }
    ]
  }
}