elasticsearch:无法使用 XContentBuilder 设置 geo_shape 值

标签 elasticsearch elasticsearch-geo-shape

我在 Elasticsearch 中有以下映射。我可以使用 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"
            }
        }
    }
}

感谢任何反馈?

谢谢

最佳答案

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

根据文档 streets 的索引映射,我们有这些属性:idshape

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

shape must be an object consisting of type and coordinates

因此对于您的具体情况,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 数据库中的人。

关于elasticsearch:无法使用 XContentBuilder 设置 geo_shape 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40553975/

相关文章:

elasticsearch - 在Elasticsearch搜索中,GET请求的查询字符串参数和POST请求的 “Query DSL”在功能上是否等效?

elasticsearch - 如何在汇总期间考虑的存储桶中显示值?

mysql - Elasticsearch : "reason": "failed to find geo_point field"

c# - 具有geo_shape字段的文档不能反序列化?

amazon-web-services - 字符串数据类型的 Elasticsearch 查询

amazon-web-services - Docker容器启动后立即退出

elasticsearch - Logstash文件输入未写入AWS EC2上的Elasticsearch安装

elasticsearch - 读取geo_shape作为GeoJSON文本

elasticsearch - 给定纬度/经度,找到纬度/经度所在的每个圆圈