elasticsearch:无法使用XContentBuilder设置geo_shape值



我在弹性搜索中有以下映射。我能够使用Sense插件放置文档,但无法使用XContentBuilder设置geo_shape字段值。我得到以下错误:

错误:

[106]: index [streets], type [street], id [{dc872755-f307-4c5e-93f6-bba9c95791c7}], message [MapperParsingException[failed to parse [shape]]; nested: ElasticsearchParseException[shape must be an object consisting of type and coordinates];]

映射:

PUT /streets
    {
       "mappings": {
          "street": {
             "properties": {
                "id": {
                   "type": "string"
                },
                "shape": {
                   "type": "geo_shape",
                   "tree": "quadtree"
                }
             }
          }
       }
    }
代码:

val bulkRequest:BulkRequestBuilder = esClient.prepareBulk()
//inloop
    xb = jsonBuilder().startObject()      
    xb.field("id", guid)
    xb.field("shape", jsonString) // removing this line creates the index OK but without the geo_shape
    xb.endObject()
    bulkRequest.add(esClient.prepareIndex("streets", "street", guid).setSource(xb))
//end loop

    val bulkResponse:BulkResponse = bulkRequest.execute().actionGet()
    if(bulkResponse.hasFailures){
          println(bulkResponse.buildFailureMessage())
    }

jsonString:

{
    "id": "{98b8fd8d-074c-4349-a83b-6e892bf2d0ef}",
    "shape": {
        "type": "LineString",
        "coordinates": [
            [-70.81866815832467, 43.12187109162505],
            [-70.83054813653018, 43.15917412985851],
            [-70.81320737213957, 43.23522269547419],
            [-70.90108590067649, 43.28102004268419]
        ],
        "crs": {
            "type": "name",
            "properties": {
                "name": "EPSG:4326"
            }
        }
    }
}

感谢任何反馈?

谢谢

这对你来说可能有点晚了,但这可以帮助那些面临类似问题的人,即使是现在。

在文档streets的索引映射之后,我们有这些属性:idshape

在你的错误信息中,它被描述为:

shape必须是由类型和坐标组成的对象

所以对于你的具体情况,crs数组只是不被接受(不知道为什么你不能添加额外的参数)。

这是一个如何使用CURL将文档添加到streets索引的示例:

curl -X POST "localhost:9200/streets/_doc?pretty" -H 'Content-Type: application/json' -d '
{
"id": 123,
"shape": {
    "type": "Polygon",
    "coordinates": [
        [
            [
                32.85444259643555,
                39.928694653732364
            ],
            [
                32.847232818603516,
                39.9257985682691
            ],
            [
                32.837791442871094,
                39.91947941109337
            ],
            [
                32.837276458740234,
                39.91579296675271
            ],
            [
                32.85392761230469,
                39.913423004886894
            ],
            [
                32.86937713623047,
                39.91329133793421
            ],
            [
                32.88036346435547,
                39.91539797880347
            ],
            [
                32.85444259643555,
                39.928694653732364
            ]
        ]
    ]
}
}'

如果您需要添加LineString,而不是Polygon,只需从'shape'更改'type'属性。

我希望这能帮助人们在ElasticSearch数据库中添加带有形状的文档。

相关内容

  • 没有找到相关文章