您收到此错误消息是因为字段位置未正确映射。有可能在某个时间点,您尝试在该字段中索引一个字符串,现在它被映射为一个字符串。Elasticsearch 无法自动检测字段是否包含 geo_point。它必须在映射中明确指定。否则,Elasticsearch 会根据您在第一条索引记录中使用的 geo_point 表示类型将此类字段映射为字符串、数字或对象。一旦将字段添加到映射中,就不能再更改其类型。因此,为了解决这种情况,您需要删除此类型的映射并重新创建它。以下是为 geo_point 字段指定映射的示例:
curl -XDELETE "localhost:9200/geo-test/"
echo
# Set proper mapping. Elasticsearch cannot automatically detect that something is a geo_point:
curl -XPUT "localhost:9200/geo-test" -d '{
"settings": {
"index": {
"number_of_replicas" : 0,
"number_of_shards": 1
}
},
"mappings": {
"doc": {
"properties": {
"location" : {
"type" : "geo_point"
}
}
}
}
}'
echo
# Put some test data in Sydney
curl -XPUT "localhost:9200/geo-test/doc/1" -d '{
"title": "abcccc",
"price": 3300,
"price_per": "task",
"location": { "lat": -33.8756, "lon": 151.204 },
"description": "asdfasdf"
}'
curl -XPOST "localhost:9200/geo-test/_refresh"
echo
# Search, and calculate distance to Brisbane
curl -XPOST "localhost:9200/geo-test/doc/_search?pretty=true" -d '{
"query": {
"match_all": {}
},
"script_fields": {
"distance": {
"script": "doc['\''location'\''].arcDistanceInKm(-27.470,153.021)"
}
},
"fields": ["title", "location"]
}
'
echo