【问题标题】:Elasticsearch Reindex dynamic mapped location data to geoshapeElasticsearch 将动态映射的位置数据重新索引到 geoshape
【发布时间】:2018-09-18 11:15:15
【问题描述】:

我将一些数据记录到 Elasticsearch。但是我的模板效果不佳,并且我的位置数据映射为动态的。我想用查询脚本重新索引所有数据。如何将位置数据转换为 geoshape?

样本数据:

{
  "deviceId": "dev1",
  "location": {
     "type": "Point",
     "coordinates": [
          28.891983032226562,
          41.02446333535115
     ]
  }
}

type 字段映射 textcoordinates 映射 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'); } "
  }
}

Elasticsearch 响应:

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

【问题讨论】:

  • 到目前为止您尝试过什么?你需要展示你的尝试。
  • @AshwaniShakya 问题已更新。谢谢
  • @AshwaniShakya 页面包含简单的数据类型转换。我需要geoshape转换。

标签: elasticsearch typeconverter reindex elasticsearch-geo-shape


【解决方案1】:

您的脚本中有错字,应删除一个悬空的右方括号

"inline": "... ctx._source['location.coordinates'] = ctx._source.templocation]; ctx._source.remove('templocation'); } "
                                                                             ^
                                                                             |
                                                                        remove this

【讨论】:

  • 谢谢。查询异常已更改。新结果:“原因”:“[位置] 被定义为映射 [数据] 中的对象,但此名称已用于其他类型的字段”
  • 是的,这是因为在您的目标索引中,您需要确保在运行重新索引之前将location 正确映射为geo_shape
  • 是的,在我的目标索引中,location 字段映射为 geo_shape。我确定
  • 你能用你从curl -XGET localhost:9200/test2得到的结果更新你的问题吗?
  • 这不是我问的,检查我提到的 curl 但使用 test3 而不是 test2
猜你喜欢
  • 1970-01-01
  • 2020-01-16
  • 1970-01-01
  • 2017-03-11
  • 1970-01-01
  • 2015-11-03
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多