【问题标题】:Error on inserting geo_point in elasticsearch through java通过java在elasticsearch中插入geo_point时出错
【发布时间】:2016-12-01 07:04:10
【问题描述】:

我在 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 中是 String 但它是用于弹性搜索的 GeoPointFieldType。

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

【问题讨论】:

    标签: java elasticsearch logstash geocoding elastic-stack


    【解决方案1】:

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

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

    只需打印您的jj.toString(),您就会发现这里没有生成它。

    另请注意,我将使用 double 而不是 String 用于纬度/经度字段。

    更新:

    根据你的 cmets,我试过了

    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是什么吗?

    【讨论】:

    • 我在关注这个链接elastic.co/guide/en/elasticsearch/reference/current/… 它说我的 message_geocoordinates 字段也可以是字符串。不过我会试试你的建议。
    • 然后呢? jj.toString() 打印什么?
    • 我根据你的建议做了修改 double latitude=41.589;双经度=28.59;字符串消息地理坐标=空; JSONObject jj = new JSONObject(); message_geocoordinates=纬度+","+经度; jj.put("纬度", 纬度); jj.put("lon", 经度); jj.put("message_geocoordinates", message_geocoordinates); System.out.println(jj.toString());并将输出打印为 {"lon":28.59,"message_geocoordinates":"41.589,28.59","lat":41.589}
    • 我不明白您的代码如何生成“message_geocordinates”,因为您在创建 jj 对象时不使用它。或者我的眼睛有问题可能是......
    【解决方案2】:

    问题已由我解决。

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

    【讨论】:

    • 在摄取数据时,当我们必须将JSON对象摄取到elasticsearch索引中时,json对象必须使用库com.google.gson.JsonObject而不是org.json.JSONObject。如果我们使用org.json .JSONObject 它会给 MapperParsingExceptionfailed 解析;嵌套:IllegalStateException [混合字段类型:类 org.elasticsearch.index.mapper.core.StringFieldMapper$StringFieldType != 字段 message_geocoordinates 上的类 org.elasticsearch.index.mapper.geo.BaseGeoPointFieldMapper$GeoPointFieldType] 映射为的字段elasticsearch映射中的geo_point
    猜你喜欢
    • 2015-06-14
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    相关资源
    最近更新 更多