有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

通过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) 个答案

  1. # 1 楼答案

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

    {
      "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