首页 > 解决方案 > 在 _reindex api 中的字段上使用 geo_point 数据类型

问题描述

我的索引包含两个字段:存储为浮点数的经度和纬度。我想创建新索引并从第一个索引复制数据,但映射不同。我使用带有弹性处理器的 reindex api,它可以重命名字段并赋予它们不同的数据类型。当我尝试创建类型为“geo_point”的字段时,它失败了。

"type": "parse_exception",
       "reason": "[type] type [geo_point] not supported, cannot convert field.",

但是当我创建新索引时,我能够创建具有“geo_point”类型的字段。我尝试了不同的解决方法,但文档说使用地理查询只能使用“geo_point”类型。有什么解决办法吗?

{
  "description": "test pipe",
  "processors": [
    {
      "convert": {
        "field": "location", 
        "type": "geo_point"
      }
    }
  ]
}

添加了管道定义。

标签: elasticsearchkibana

解决方案


好的,假设您当前的索引映射如下所示:

PUT oldindex
{
  "mappings": {
    "doc": {
      "properties": {
        "latitude": {
          "type": "float"
        },
        "longitude": {
          "type": "float"
        }
      }
    }
  }
}

您需要使用正确的映射创建一个新索引,如下所示

PUT newindex
{
  "mappings": {
    "doc": {
      "properties": {
        "location": {
          "type": "geo_point"
        }
      }
    }
  }
}

然后,您可以简单地利用重新索引 API将旧索引复制到新索引中,并使用一些额外的脚本来创建位置字段:

POST _reindex
{
  "source": {
    "index": "oldindex",
  },
  "dest": {
    "index": "newindex"
  },
  "script": {
    "source": "ctx._source.location = ['lat': ctx._source.latitude, 'lon': ctx._source.longitude]; ctx._source.remove('latitude'); ctx._source.remove('longitude'); "
  }
}

而且您可以在新的闪亮索引中使用位置字段!


推荐阅读