elasticsearch:无法使用XContentBuilder设置geo_shape值

时间:2016-11-11 18:22:38

标签: elasticsearch elasticsearch-geo-shape

我在弹性搜索中有以下映射。我能够使用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())
    }

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

感谢任何反馈?

由于

1 个答案:

答案 0 :(得分:0)

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

根据文档 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,只需更改“形状”中的“类型”属性即可。

我希望这可以帮助那些必须将带有形状的文档添加到 ElasticSearch 数据库中的人。