如何在 elasticsearch 中将位置设置为 geo_point?

How to setup location as a geo_point in elasticsearch?

我一直在 运行 解决这个问题 failed to find geo_point field [location]

这是我的流程。

  1. 导入 csv

    input {
        file {
            path => "test.csv"
            start_position => "beginning"
            sincedb_path => "/dev/null"
        }
    }
    
    filter {
      csv {
        separator => ","
        #zip,lat, lon
        columns => [ "zip" , "lat", "lon"]
      }
    
      mutate {
        convert => { "zip" => "integer" }
        convert => { "lon" => "float" }
        convert => { "lat" => "float" }
      }
    
      mutate {
        rename => {
            "lon" => "[location][lon]"
            "lat" => "[location][lat]"
        }
      }
    
      mutate { convert => { "[location]" => "float" } }
    }
    
    output {
        elasticsearch {
            hosts => "cluster:80"
            index => "data"
        }
        stdout {}
    }
    
  2. 测试记录

    GET data
    
    "hits": [
    {
      "_index": "data",
      "_type": "logs",
      "_id": "AVvQcOfXUojnX",
      "_score": 1,
      "_source": {
        "zip": 164283216,
        "location": {
          "lon": 71.34,
          "lat": 40.12
        }
      }
    },
    ...
    

如果我尝试 运行 一个 geo_distance 查询,我会得到 failed to find geo_point field [location]

那我试试运行

PUT data
{
  "mappings": {
        "location": {
            "properties": {
                "pin": {
                    "properties": {
                        "location": {
                            "type": "geo_point"
                        }
                    }
                }
            }
        }
    }
}

但我得到 index [data/3uxAJ4ISKy_NyVDNC] already exists

如何将 location 转换为 geo_point 以便我可以 运行 对其进行查询?

编辑:

我尝试在索引任何内容之前植入模板,但仍然出现同样的错误

PUT _template/template
{
  "template": "base_map_template",
  "order": 1,
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "node_points": {
      "properties": {
        "location": {
          "type": "geo_point"
        }
      }
    }
  }
}

您需要将模板命名为 data 而不是 base_map_template,因为这是您的索引的命名方式。此外,类型名称需要是 logs 而不是 node_points:

PUT _template/template
{
  "template": "data",             <--- change this
  "order": 1,
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "logs": {                     <--- and this
      "properties": {
        "location": {
          "type": "geo_point"
        }
      }
    }
  }
}