elasticsearch+logstash+postgis读取shpfile建立索引提供搜索服务

以下操作前提确保电脑有jdk、postgresql、postGIS环境


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
          }
        }
      }
    }
  }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值