首先,默认情况下,elasticsearch 是不解析字符串的。因此,如果您将以下 JSON 传递给 elasticsearch:{"tweet": {"fooval": "1"}}
它将被tweet.fooval
视为字符串。如果 elaticsearch 正在解析字符串,请确保在映射numeric_detection
中将anddate_detection
设置为 false 。
另一方面,如果elasticsearch接收到一个像这样的JSON数字的值:{"tweet": {"fooval": 1}}
,elasticsearch确实会将这样的字段映射为long或double。您可以使用 dynamic_templates 覆盖此行为。这是一个例子:
curl -XPUT localhost:9200/test-idx -d '{
"settings": {
"index.number_of_replicas": 0,
"index.number_of_shards": 1
},
"mappings": {
"doc": {
"dynamic_templates" : [
{
"template_obj" : {
"match" : "*",
"match_mapping_type" : "object",
"mapping" : {
"type" : "object"
}
}
},
{
"template_str" : {
"match" : "*",
"mapping" : {
"type" : "string"
}
}
}
]
}
}
}'
curl -XPUT localhost:9200/test-idx/doc/1 -d '{
"count": 123,
"sold": false,
"date": "2009-11-15T14:12:12",
"price": 12.3,
"description": {
"weight": 42.3,
"size": {
"lenght": 30,
"width": 20,
"hight": 10
}
}
}'
echo
curl "localhost:9200/test-idx/doc/_mapping?pretty=true"