通过java在elasticsearch中插入geo_点时出错
我在Elasticsearch中有一个索引,其中有一个geo_point字段。 映射文件有点像这样
curl -XPUT 'localhost:9200/test_geo5' -d'
{
"mappings":{
"test_geo5":{
"properties":{
"lat":{ "type": "string","index": "not_analyzed"},
"lon":{ "type": "string","index": "not_analyzed"},
"message_geocoordinates":{ "type": "geo_point"}
}
}
}
}'
我想用java代码插入消息_geocardios
所以我写了这样的java代码
String latitude= xxxxx;
String longitude= yyyy;
String message_geocordinates= latitude+","+longitude;
JSONObject jj = new JSONObject();
jj.put("lat", latitude);
jj.put("lon", longitude);
jj.put("message_geocordinates", message_geocordinates);
IndexResponse response = client.prepareIndex("test_geo5", "iiiii").setSource(jj.toString()).execute().actionGet();
我试着把它插入Elasticsearch。但我收到了以下例外
MapperParsingExceptionfailed to parse; nested: IllegalStateException[Mixing up field types: class org.elasticsearch.index.mapper.core.StringFieldMapper$StringFieldType != class org.elasticsearch.index.mapper.geo.BaseGeoPointFieldMapper$GeoPointFieldType on field message_geocoordinates];
所以它说我的message_geocoordinates字段在java中是字符串,但在elatic搜索中是GeoPointFieldType
如何使用java代码将地质点字段插入elasticsearch
# 1 楼答案
您需要生成一个如下所示的数据结构:
只需打印
jj.toString()
,你就会发现你并没有在这里生成它还请注意,对于lat/lon字段,我将使用
double
而不是String
更新:
根据你的评论,我试过这个
这工作正常
你能查一下什么给
GET test_geo5/_mapping