通过 java 在 elasticsearch 中插入 geo_point 时出错

Error on inserting geo_point in elasticsearch through java

我在 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 代码插入 message_geocoordinates。

所以我写了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 中的字符串,但它是弹性搜索的 GeoPointFieldType。

如何使用 java 代码将地理点字段插入到 elasticsearch 中。

您需要生成如下所示的数据结构:

{
  "message_geocoordinates": {
    "lat": 0.00,
    "lon": 0.00
  }
}

只需打印您的 jj.toString(),您会发现您没有在此处生成它。

另请注意,对于 lat/lon 个字段,我将使用 double 而不是 String

更新:

根据您的意见,我尝试了这个

DELETE test_geo5
PUT test_geo5
{
  "mappings": {
    "test_geo5": {
      "properties": {
        "lat": {
          "type": "string",
          "index": "not_analyzed"
        },
        "lon": {
          "type": "string",
          "index": "not_analyzed"
        },
        "message_geocoordinates": {
          "type": "geo_point"
        }
      }
    }
  }
}
PUT test_geo5/test_geo5/1
{
  "lon": 28.59,
  "message_geocoordinates": "41.589,28.59",
  "lat": 41.589
}

这工作正常。

你能检查一下 GET test_geo5/_mapping 是什么吗?

问题已被我解决

基本上我创建的 JSONObject jj 必须使用 gson 库,但我使用的是 JSON 简单库。