2018-09-06【Elasticsearch】Shapefile 导入ES并进行空间查询

参考 : https://www.gdal.org/drv_elasticsearch.html

  1. 下载GDAL
  2. 下载Elasticsearch
    版本的差异可能会导致失败,这次下载的是gdal 2.3.2 ,es5.3
    两个文件的win64 二进制包 链接:https://pan.baidu.com/s/149AYKckrdc5mKnCK3wmYGg 密码:a8vm
  3. 将gdal的bin目录添加到path环境变量,将GDAL\bin\gdal\apps也添加到path变量。主要是利用ogr2ogr.
  4. es的配置需要java环境,具体参考官网 https://www.elastic.co/guide/en/elasticsearch/reference/current/setup.html
  5. 运行es,在cmd用ogrinfo验证
D:\es5.3>ogrinfo ES:http://localhost:9200
INFO: Open of `ES:http://localhost:9200'
      using driver `ElasticSearch' successful.
  1. 在cmd生成mapping 文件
    > ogr2ogr -progress --config ES_WRITEMAP \map.txt -f "ElasticSearch" http://localhost:9200 \Address_SpatialJoin2new.shp
    map文件如下,中间部分字段没贴。如果需要分词或拼音,可在这个文件修改,再重新更新es。
{
    "FeatureCollection": {
        "properties": {
            "type": {
                "type": "string"
            },
            "properties": {
                "properties": {
                    "OBJECTID": {
                        "type": "integer"
                    },

                //...一系列其他字段


                }
            },
            "geometry": {
                "properties": {
                    "type": {
                        "type": "string"
                    },
                    "coordinates": {
                        "type": "geo_point"
                    }
                }
            }
        },
        "_meta": {
            "fid": "ogc_fid"
        }
    }
}
  1. 在cmd导入shapefile数据 >ogr2ogr -progress -lco BULK_SIZE=5000000 -f "ElasticSearch" http://localhost:9200 <路径>\Address_SpatialJoin2new.shp
  2. 根据空间查询,使用postman发post请求
    url :
    http://localhost:9200/address_spatialjoin2new/_search
    body:
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "100m",
                    "geometry.coordinates": [
                            119.976261537,
                            30.528380685
                        ]
                }
            }
        }
    }
}

这个query的geometry.coordinates的写法需要注意一下,包括坐标的形式,可以参考search直接返回的结果。

  1. 查询结果
{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 331,
        "max_score": 1,
        "hits": [
            {
                "_index": "address_spatialjoin2new",
                "_type": "FeatureCollection",
                "_id": "AWWtx3n1BbfRLhXuj20e",
                "_score": 1,
                "_source": {
                    "ogc_fid": 36749,
                    "geometry": {
                        "type": "POINT",
                        "coordinates": [
                            119.976261537,
                            30.528380687
                        ]
                    },
                    "type": "Feature",
                    "properties": {
                        "OBJECTID": 36749,
                        "Join_Count": 1,
                        "TARGET_FID": 36821,
                        "JOIN_FID": 305640,
                        "ID": "n320138",
                        "ADDENTIID": "23305210020000020138",
                        "POIENTIID": null,
                        "STAADDRESS": "湖州市德清县舞阳街道御景公馆诚园5幢3单元506室",
                        "CITYNAME": "湖州市",
                        "COUNTYNAME": "德清县",
                        "TOWNNAME": "舞阳街道",
                        "VILLAGNAME": null,
                        "DEVREGNAME": null,
                        "STREETNAME": null,
                        "DISTRINAME": "御景公馆",
                        "RESNAME": "诚园",
                        "DOORPLATE1": null,
                        "DOORPLATE2": "5幢",
                        "DOORPLATE3": "3单元",
                        "DOORPLATE4": "506室",
                        "CUSTOMCODE": null,
                        "X": "119.976261537",
                        "Y": "30.528380687",
                        "COLNOTE": null,
                        "PEOPLE": "王醒",
                        "BLOCK": "N3",
                        "COLDATA": "2017/04/19",
                        "STODATA": null,
                        "SUBMITID": "第一批次",
                        "FSCALE": "999",
                        "FCODE": "3103011500",
                        "NAME": null,
                        "OGLAYER": 18,
                        "UGLAYER": 0,
                        "OPENLAYER": 0,
                        "CONSTRUCT": null,
                        "FTYPE": null,
                        "BASEAREA": 0,
                        "USETYPE": null,
                        "ENTIID": "d048fc83d497407297ff28d53ac70c99",
                        "SUPENTIID": null,
                        "ELEMID": "d048fc83d497407297ff28d53ac70c99",
                        "CLASID": "310300",
                        "FROMVERSIO": 1,
                        "TOVERSION": 2
                    }
                }
            },
.....

你可能感兴趣的:(2018-09-06【Elasticsearch】Shapefile 导入ES并进行空间查询)