Skip to content

Instantly share code, notes, and snippets.

@spazm
Last active August 17, 2016 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spazm/efb652831e6fb0f9db4e6640a31e3817 to your computer and use it in GitHub Desktop.
Save spazm/efb652831e6fb0f9db4e6640a31e3817 to your computer and use it in GitHub Desktop.
elasticsearch: strings in integer field
curl -XPUT "https://localhost:9200/int_vs_str_test" -d '{"mappings": { "foo" : { "properties": { "label_id": { "type": "integer"}}}}}'
{"acknowledged":true}
curl -XGET "https://127.0.0.1:9200/int_vs_str_test/_mapping/foo?pretty=1"
{
"int_vs_str_test" : {
"mappings" : {
"foo" : {
"properties" : {
"label_id" : {
"type" : "integer"
}
}
}
}
}
}
curl -XPUT "https://localhost:9200/int_vs_str_test/foo/1" -d '{"label_id" : [1,"2",3]}'
{"_index":"int_vs_str_test","_type":"foo","_id":"1","_version":1,"_shards":{"total":2,"successful":2,"failed":0},"created"
:true}
curl -XGET "https://127.0.0.1:9200/int_vs_str_test/foo/1?pretty=1"
{
"_index" : "int_vs_str_test",
"_type" : "foo",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"label_id" : [ 1, "2", 3 ]
}
}
curl -XPUT "https://rdb.dev.ziprecruiter.com:9200/int_vs_str_test/foo/1?pretty=1" -d '
{"implicit_long" : [1,"2",3]}'
{
"error" : {
"root_cause" : [ {
"type" : "mapper_parsing_exception",
"reason" : "failed to parse"
} ],
"type" : "mapper_parsing_exception",
"reason" : "failed to parse",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "mapper [implicit_long] of different type, current_type [long], merged_type [string]"
}
},
"status" : 400
}
curl -XPUT "https://localhost:9200/int_vs_str_test/_mapping/foo?pretty=1" -d '
{"properties": {"explicit_long": {"type": "long"}}}'
{
"acknowledged" : true
}
curl -XPUT "https://localhost:9200/int_vs_str_test/foo/1?pretty=1" -d '{"explicit_long" : [1,"2",3]}'
{
"_index" : "int_vs_str_test",
"_type" : "foo",
"_id" : "1",
"_version" : 2,
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : false
}
curl -XGET "https://rdb.dev.ziprecruiter.com:9200/int_vs_str_test/foo/1?pretty=1"
{
"_index" : "int_vs_str_test",
"_type" : "foo",
"_id" : "1",
"_version" : 2,
"found" : true,
"_source" : {
"explicit_long" : [ 1, "2", 3 ]
}
}
@spazm
Copy link
Author

spazm commented Aug 17, 2016

Elasticsearch allows me to insert string values into integer fields. Why does this work? Can I make it store "2" as 2?

@spazm
Copy link
Author

spazm commented Aug 17, 2016

I run into an issue using groovy scripting and .contains()

ctx._source["label_ids"].contains(1) <== True
ctx._source["label_ids"].contains(2) <== False
ctx._source["label_ids"].contains("2") <== True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment