Strings cooerced to integers are still stored and retrieved as strings


(Renewooller) #1

Hi,
Not sure if this is a bug, just wanted to check here first:

  1. create an index with an integer mapping:

    curl -XPUT 'http://localhost:9200/shmap/' -d '{
    "settings" : {
    "number_of_shards" : 1,
    "number_of_replicas" : 0
    },
    "mappings" : {
    "type" : {
    "dynamic" : false,
    "properties" : {
    "shint" : {
    "type" : "integer",
    "index" : "not_analyzed",
    }
    }
    }
    }
    }'

  2. insert a string that is made of integers:

    curl -XPOST 'http://localhost:9200/shmap/type' -d '{
    "shint" : "00000001"
    }'

  3. Get it back. Note that the string is still a string, even though the type is integer:

    {"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"shmap","_type":"type","_id":"AVfgtbAbNiGlzqOiG3dD","_score":1.0,"_source":{
    "shint" : "00000001"
    }}]}}

Is this a bug? If not, why not?


(Mark Harwood) #2

We don't monkey with the original JSON.
But internally in the index we treat ints as ints and strings as strings.
See the difference in these results with zero-prefixed strings vs Ints:

DELETE test
PUT /test/
{
	"settings" : {
		"number_of_shards" : 1,
		"number_of_replicas" : 0
	},
	"mappings" : {
		"type" : {
			"dynamic" : false,
			"properties" : {
				"shint" : { 
					"type" : "integer", 
					"index" : "not_analyzed"
				},
				"shstring" : { 
					"type" : "keyword"
				}                
			}
		}
	}
}
POST test/type
{
	"shint" : "00000002",
	"shstring" : "00000002" 
}
POST test/type
{
	"shint" : "1",
	"shstring" : "1" 
}
//Sorts 0000002 before 1
GET test/_search
{
	"sort":["shstring"  ]
}    
//Sorts 1 before 0000002 
GET test/_search
{
	"sort":["shint"  ]
}

(system) #3