映射geo-polygon Elastic Search/Kibana 5.3

2024-03-29 14:54:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有20000个geojson文件,格式如下:

{
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                [long,lat],
                [long,lat],
                [long,lat],
                [long,lat],
                [long,lat]
            ]
        ]
    },

我用geo_shape和geo_point尝试的所有映射要么没有出现在Kibana中,要么出现在Kibana中,但是没有数据。将此映射到索引多个文件的最佳方法是什么?(如果没有好的方法,我的下一个想法是,如果不能使用所有坐标,就为每个文件创建一个中心点。也许取第一个长的lat数组,使其成为每个json文件的geo_点中心点。也不知道该怎么做)

这是在不更改任何内容的情况下索引时ES的默认映射:

^{pr2}$

更新 这是我的新地图:

{
  "indexname" : {
    "mappings" : {
      "my_type" : {
        "properties" : {
          "geometry" : {
            "type" : "geo_shape",
            "tree" : "quadtree",
            "precision" : "1.0m"
          },
          "id" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },

但是,当我去kibana时,当我试图在增强的tilemap上可视化时,仍然会遇到一个错误:

No Compatible Fields: The "indexname" index pattern does not contain any of the following field types: geo_point

编辑2 这是我创建映射的命令:

curl -XPUT "http://localhost:9200/indexname" -d "{\"mappings\" : {\"my_type\" : {\"properties\" : {\"geometry\" : {\"type\":\"geo_shape\", \"tree\": \"quadtree\", \"precision\": \"1m\"}}}}}"

我正在通过循环并发送post请求来索引我的文件:

r = requests.post(url_of_index, data=file(jsonfiles).read()) 

当我试图将类型更改为geo_点,然后索引我的文件时,我遇到了mapper解析器异常。在


Tags: 文件方法mytypepropertieslongpointgeo
1条回答
网友
1楼 · 发布于 2024-03-29 14:54:17

您需要做的是创建包含^{}类型的自己的映射,因为ES不会从GeoJSON文档本身推断出这种类型。在

PUT indexname
{
  "mappings": {
    "my_type": {
      "properties": {
        "geometry": {
          "type": "geo_shape",
          "tree": "quadtree",
          "precision": "1m"
        }
      }
    }
  }
}

创建此索引和映射后,您将能够索引GeoJSON文件:

^{pr2}$

更新

根据我们的讨论,您可能需要在映射中创建一个新的geo_point字段,如下所示:

PUT indexname
{
  "mappings": {
    "my_type": {
      "properties": {
        "location": {
          "type": "geo_point"
        },
        "geometry": {
          "type": "geo_shape",
          "tree": "quadtree",
          "precision": "1m"
        }
      }
    }
  }
}

然后,从Python代码中,您需要通过从JSON文件中读取第一个坐标来创建新字段,类似于下面的伪代码:

import json

doc = json.loads(file(jsonfiles).read())
# create the new location field
doc['location'] = doc['geometry']['coordinates'][0][0]
r = requests.post(url_of_index, data=json.dumps(doc)) 

相关问题 更多 >