我在Elasticsearch上记录了一些数据。但是我的模板效果不佳,我的位置数据动态映射。我想使用查询脚本重新索引所有数据。如何将位置数据转换为地理形状?
示例数据:
{
"deviceId": "dev1",
"location": {
"type": "Point",
"coordinates": [
28.891983032226562,
41.02446333535115
]
}
}
type
字段映射text
和coordinates
映射float
。
test3
索引放置映射请求:
PUT /test3
{
"mappings": {
"doc": {
"properties": {
"location": {
"type": "geo_shape",
"tree": "quadtree",
"precision": "100m"
}
}
}
}
}
我的重新索引脚本(已编辑(:
POST _reindex
{
"size": 1000,
"source": {
"index": "test"
, "query": {
"constant_score": {
"filter": {
"exists": {
"field": "location"
}
}
}
}
},
"dest": {
"index": "test3"
},
"script":{
"inline": "if(ctx._source.location.size()>1) {ctx._source.templocation=ctx._source.remove('location'); ctx._source['location.type'] = 'Point'; ctx._source['location.coordinates'] = ctx._source.templocation; ctx._source.remove('templocation'); } "
}
}
弹性搜索响应:
{
"took": 172,
"timed_out": false,
"total": 2,
"updated": 0,
"created": 0,
"deleted": 0,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1,
"throttled_until_millis": 0,
"failures": [
{
"index": "test3",
"type": "data",
"id": "AWXsdV-z29dKQeP_vT68",
"cause": {
"type": "illegal_argument_exception",
"reason": "[location] is defined as an object in mapping [data] but this name is already used for a field in other types"
},
"status": 400
},
{
"index": "test3",
"type": "data",
"id": "AWXsdVAP29dKQeP_vT67",
"cause": {
"type": "illegal_argument_exception",
"reason": "[location] is defined as an object in mapping [data] but this name is already used for a field in other types"
},
"status": 400
}
]
}
curl -XGET localhost:9200/test3
的结果
{
"test3": {
"aliases": {},
"mappings": {
"doc": {
"properties": {
"location": {
"type": "geo_shape",
"tree": "quadtree",
"precision": "100.0m"
}
}
}
},
"settings": {
"index": {
"creation_date": "1537333568669",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "K78hBgskSkKIg-Itb7uqvA",
"version": {
"created": "5060499"
},
"provided_name": "test3"
}
}
}
}
您的脚本中有拼写错误,应删除一个悬挂的右方括号
"inline": "... ctx._source['location.coordinates'] = ctx._source.templocation]; ctx._source.remove('templocation'); } "
^
|
remove this