gpt4 book ai didi

java - 将 geo_shape 数据添加到elasticsearch

转载 作者:行者123 更新时间:2023-12-02 10:45:45 27 4
gpt4 key购买 nike

我是 Elasticsearch 新手。我正在尝试使用 Java elasticsearch-rest-high-level-client 版本 6.4.0geo_shape 数据添加到我的 Elasticsearch 索引中

这是我的索引映射

{
"elktest": {
"mappings": {
"test": {
"properties": {
"Location": {
"type": "geo_shape"
}
}
}
}
}
}

我正在尝试将此数据添加到索引中

{
"Location" : {
"type": "polygon",
"coordinates" : [[
[ -104.04405,45.01967 ],
[ -111.03084,44.99904 ],
[ -111.04131,41.011 ],
[ -104.03375,41.00193 ],
[ -104.04405,45.01967 ]
]]
}
}

如果我从 Kibana Dev Tools 控制台POST此数据,它就可以正常工作。

我正在尝试从Java POST

String jsonString = "{\"Location\":\"{type: geo_polygon, coordinates:    [[             [ -105.04405,47.01967 ],        [ -111.03084,44.99904 ],        [ -111.04131,41.011   ],        [ -104.03375,41.00193 ],        [ -105.04405,47.01967 ]]] }\"}";

IndexRequest request = new IndexRequest(indexName, indexType, 100);
request.source(jsonString, XContentType.JSON);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);


我收到此异常

Elasticsearch exception [type=mapper_parsing_exception, reason=failed to parse [Location]]
Elasticsearch exception [type=parse_exception, reason=expected word but found: '{']

最佳答案

您的字符串化 JSON 似乎是错误的。这里有一个额外的 ":

                         here ------v
String jsonString = "{\"Location\":\"{type: ...

从浏览器控制台

JSON.stringify({
"Location" : {
"type": "polygon",
"coordinates" : [[
[ -104.04405,45.01967 ],
[ -111.03084,44.99904 ],
[ -111.04131,41.011 ],
[ -104.03375,41.00193 ],
[ -104.04405,45.01967 ]
]]
}
})
# additional escaping needs to be done but ...
"{"Location":{"type":"polygon","coordinates":[[[-104.04405,45.01967],[-111.03084,44.99904],[-111.04131,41.011],[-104.03375,41.00193],[-104.04405,45.01967]]]}}"

关于java - 将 geo_shape 数据添加到elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52598665/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com