3

我在弹性搜索中有以下映射。我可以使用 Sense 插件 PUT 文档,但无法使用 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())
    }

json字符串:

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

感谢任何反馈?

谢谢

4

1 回答 1

0

对您来说可能有点晚了,但这可以帮助即使在今天也面临类似问题的人。

根据您对文档的索引映射streets,我们有以下属性:idshape

在您的错误消息中,描述为:

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

因此,对于您的具体情况,该crs数组不被接受(不知道为什么不能添加额外的参数)。

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

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
            ]
        ]
    ]
}
}'

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

我希望这有助于人们将带有形状的文档添加到 ElasticSearch 数据库中。

于 2021-05-24T09:47:06.890 回答