如何在elasticsearch中保存geo数据

How to save geo data in elasticsearch

如何在 elasticsearch(geo 数据类型)中为包含以下数据的文档编制索引?

<west>5.8663152683722</west>
<north>55.0583836008072</north>
<east>15.0418156516163</east>
<south>47.2701236047002</south>

我试过 geo_point 并且它适用于经度和纬度,但不确定如何保存此数据。非常感谢任何帮助。

您必须使用 geo_shape datatype 并在同步之前将您的 XML(我假设)半点转换为线串或多边形。


我要在这里使用多边形。 让我们想象一下传统的 cardinal directions:

             North (+90)
               |
(-180) West  ——+—— East (+180)
               |
             South (-90)

geo_shape 需要类似 GeoJSON 的输入,因此您需要五个坐标点,第一个和最后一个坐标点相同(根据 GeoJSON spec)。

因此,借鉴TurfJS,从左下角逆时针方向,

const lowLeft = [west, south];
const topLeft = [west, north];
const topRight = [east, north];
const lowRight = [east, south];

return 
[ 
  [
    lowLeft,
    lowRight,
    topRight,
    topLeft,
    lowLeft
  ]
]

最后,让我们创建索引并插入您的号码

PUT /example
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      }
    }
  }
}
POST /example/_doc
{
  "location":{
    "type":"polygon",
    "coordinates":[
      [
        [
          5.8663152683722,
          47.2701236047002
        ],
        [
          15.0418156516163,
          47.2701236047002
        ],
        [
          15.0418156516163,
          55.0583836008072
        ],
        [
          5.8663152683722,
          55.0583836008072
        ],
        [
          5.8663152683722,
          47.2701236047002
        ]
      ]
    ]
  }
}

然后验证正方形的中心是否确实在索引多边形内:

GET example/_search
{
  "query": {
    "geo_shape": {
      "location": {
        "shape": {
          "type": "point",
          "coordinates": [
            10.45406545999425,
            51.1642536027537
          ]
        },
        "relation": "intersects"
      }
    }
  }
}