以下操作前提确保电脑有jdk、postgresql、postGIS环境
elasticsearch+logstash+postgis读取shpfile建立索引提供搜索服务
ES相关的这里面基本都有的: https://elasticsearch.cn/download/.
1.postgresql
1.1建立gis类型数据库
建立数据库,起名字,设置编码,在此库中执行PostGIS初始化语句:
- 启用PostGIS
CREATE EXTENSION postgis;
- 启用拓扑
CREATE EXTENSION postgis_topology;
- Tiger
CREATE EXTENSION fuzzystrmatch ;
2.PostGIS
2.1导入shpfile数据
打开PostGIS PostGIS Bundle 3 for PostgreSQL x64 12 Shapefile and DBF Loader Exporter
- 提示Shapefile import completed.导入成功
注意:要导入的文件路径中不能包含中文
3.elasticsearch
3.1建立索引与数据结构
PUT /index_name
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"map": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "text"
},
"area": {
"type": "float"
},
"location": {
"type": "geo_point"
}
}
}
}
}
4.logstash
4.1数据导入ES
- 配置文件
在logstash安装目录中bin下创建 xxx.conf 文件
input {
stdin {
}
jdbc {
jdbc_connection_string => "jdbc:postgresql://localhost:5432/xxx"
jdbc_user => "postgres"
jdbc_password => "password"
#驱动jar包路径
jdbc_driver_library => "D:\variable\maven\repository\org\postgresql\postgresql\42.2.16\postgresql-42.2.16.jar"
jdbc_driver_class => "org.postgresql.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "1000"
use_column_value => "true"
tracking_column => "gid"
statement => "SELECT gid as id, name, geo_area as area, concat(st_y(ST_Transform(geom, 4326)), ',', st_x(ST_Transform(geom, 4326))) as location FROM public.count_featuretopoint2"
}
}
filter{
}
output {
elasticsearch {
#es服务器
hosts => ["localhost:9200"]
#ES索引名称
index => "index_name"
document_id => "%{id}"
}
stdout {
codec => json_lines
}
}
- 启动
在bin中启动cmd,执行:
logstash -f xxx.conf
5.elasticsearch搜索
5.1两点矩形搜索
GET /index_name/map/_search
{
"query": {
"bool": {
"must": [
{"match_all": {}}
],
"filter": {
"geo_bounding_box": {
"location": {
"top_left" : {
"lat" : xxx,
"lon" : yyy
},
"bottom_right" : {
"lat" : xxx,
"lon" : yyy
}
}
}
}
}
}
}
5.2 多边形搜索
GET /index_name/map/_search
{
"query": {
"bool": {
"must": [
{"match_all": {}}
],
"filter": {
"geo_polygon": {
"location": {
"points": [
{"lat" : xxx, "lon" : yyy},
{"lat" : xxx, "lon" : yyy},
......等等
]
}
}
}
}
}
}
5.3 范围搜索
GET /index_name/map/_search
{
"query": {
"bool": {
"must": [
{"match_all": {}}
],
"filter": {
"geo_distance": {
"distance": "1000km",
"location": {
"lat": xxx,
"lon": yyy
}
}
}
}
}
}