[ElasticSearch] 空间搜索 (一)
依据索引文档的地理坐标来进行搜索。Elasticsearch 也可以处理这种搜索。——空间搜索
一、为空间搜索准备映射
PUT my_space_test
{
"mappings": {
"poi": {
"properties": {
"name": {
"type": "string"
},
"locationpoint": {
"type":"geo_point" //随意的地理坐标
},
"locationshape": {
"type": "geo_shape" //随意的地理形状
}
}
}
}
}
二、批量加入数据
POST my_space_test/poi/_bulk
{"index":{"_id":1}}
{"name":"New York","locationpoint":"40.664167, -73.938611","locationshape":{"type":"polygon","coordinates":[[[4.8833,52.38617],[4.87463,52.37254],[4.87875,52.36369],[4.8833,52.38617]]]}}
{"index":{"_id":2}}
{"name":"London","locationpoint":[-0.1275,51.5072222],"locationshape":{"type":"polygon","coordinates":[[[0,0],[4.87463,52.37254],[4.87875,52.36369],[0,0]]]}}
{"index":{"_id":3}}
{"name":"Moscow","locationpoint":{"lat":55.75,"lon":37.616667},"locationshape":{"type":"polygon","coordinates":[[[22,22],[4.87463,52.37254],[4.87875,52.36369],[22,22]]]}}
{"index":{"_id":4}}
{"name":"Sydney","locationpoint":"-33.859972, 151.211111","locationshape":{"type":"polygon","coordinates":[[[4.8833,52.38617],[4.87463,52.37254],[4.87875,52.36369],[4.8833,52.38617]]]}}
{"index":{"_id":5}}
{"name":"Sydney","locationpoint":"eycs0p8ukc7v","locationshape":{"type":"polygon","coordinates":[[[4.8833,52.38617],[4.87463,52.37254],[4.87875,52.36369],[4.8833,52.38617]]]}}
细致观看locationpoint字段能够看到坐标能够使用多种形式来赋值,能够使用字符串、数组(仅仅能包括两个数值)、一个对象、地理散列值等来提供经纬度。
详细每种方式可能略有不同,详细使用再查相关资料。
再来看一下。locationshape,其形式就更加多样了,能够是一个点 ,即为一组数值对 [ 经度,维度 ] ,也能够是一个框 [ [左。上], [右,下] ],还能够是多边形。可是必须保证第一个坐标和最后一个坐标是同样的,从而保证是一个闭合的图形。[ [ [1,1],[2,2], [3,4] ,[1,1] ] ] ,能够发现多边形的定义中其能够是多个多边形,是一个可扩展的数组。
三、查询方式
3.1 基于距离排序
GET my_space_test/poi/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"_geo_distance": {
"locationpoint": {
"lat": 48.8567,
"lon": 2.3508
},
"unit": "km",
"order": "asc"
}
}
]
}
GET my_space_test/poi/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"_geo_distance":<{
"locationpoint": [ //或者是:"locationpoint":" 48.8567,2.3508"
2.3508,
48.8567
],
"unit": "km",
"order": "asc"
}
}
]
}
以上查询的结果是一样的(注意数组和字符串坐标的位置顺序是不同的)通过距离坐标点 [ 2.3508,48.8567 ]的大小来对查询文档进行排序。这在实际搜索中很实用,能够返回临近的一些坐标点。
3.2 边界框过滤(获得包括在指定区域内文档)
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{
"geo_bounding_box": {
"locationpoint": {
"top_left": "52.4796,-1.903",
"bottom_right": "48.8567,2.3508"
}
}
},
{
"geo_distance":{
"distance": 500,
"distance_unit": "km",
"locationpoint": "48.8567,2.3508"
}
}]}}}}
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{
"geo_shape": {
"locationshape": {
"indexed_shape": { //使用已经索引的形状
"index": "my_space_test",
"type": "poi",
"id": "4",
"path": "locationshape"
},
"relation": "within"
}
}
},
{
"geo_shape": {
"locationshape": { //自己定义形状——圆
"shape": {
"type": "circle",
"radius": "1km",
"coordinates": [
-45,
45
]
},
"relation": "within"
}
}
},{
"geo_shape": {
"locationshape": {
"shape": {
"type": "envelope", //自己定义的形状包络线,即:box(矩形)
"coordinates": [
[
-45,
45
],
[
45,
-45
]
]
},
"relation": "within"
}
}
},{
"geo_shape": {
"locationshape": { //自己定义的多边形。一定要注意,多边形的定义是包括在一个数组中的,是一个可扩展的数组
"shape": {
"type": "polygon",
"coordinates": [[[1,1],[2,3],[4,2],[1,1]]]
},
"relation": "within"
}
}
}
]
}
}
}
}
首先来说,过滤查询的字段locationshap 中包括多种形状类型。有点、包络线、多边形、甚至说多个多边形
[ElasticSearch] 空间搜索 (一)的更多相关文章
- Elasticsearch分布式搜索和数据分析引擎-ElasticStack(上)v7.14.0
Elasticsearch概述 **本人博客网站 **IT小神 www.itxiaoshen.com Elasticsearch官网地址 https://www.elastic.co/cn/elast ...
- 基于Solr的空间搜索
如果需要对带经纬度的数据进行检索,比如查找当前所在位置附近1000米的酒店,一种简单的方法就是:获取数据库中的所有酒店数据,按经纬度计算距离,返回距离小于1000米的数据. 这种方式在数据量小的时候比 ...
- R-tree 一种空间搜索的动态索引结构
译林:R-tree 一种空间搜索的动态索引结构Antonm Guttman 摘要为了有效地处理空间数据,正如在计算机辅助设计和地理数据应用中所要求的那样,数据库需要一种索引机制能根据它们的空间位置快速 ...
- solr特点八:Spatial(空间搜索)
前言 在美团CRM系统中,搜索商家的效率与公司的销售额息息相关,为了让BD们更便捷又直观地去搜索商家,美团CRM技术团队基于Solr提供了空间搜索功能,其中移动端周边商家搜索和PC端的地图模式搜索功能 ...
- ElasticSearch位置搜索
ElasticSearch位置搜索 学习了:https://blog.csdn.net/bingduanlbd/article/details/52253542 学习了:https://blog.cs ...
- Solr 空间搜索配置、按经纬度计算距离排序
Solr 空间搜索配置 1. 在solr目录下的找到conf文件夹下的schema.xml. <fields> <!-- 在fields元素中添加如下代码 --> <fi ...
- ElasticSearch入门-搜索(java api)
ElasticSearch入门-搜索(java api) package com.qlyd.searchhelper; import java.util.Map; import net.sf.json ...
- PHP使用ElasticSearch做搜索
PHP 使用 ElasticSearch 做搜索 https://blog.csdn.net/zhanghao143lina/article/details/80280321 https://www. ...
- 十九种Elasticsearch字符串搜索方式终极介绍
前言 刚开始接触Elasticsearch的时候被Elasticsearch的搜索功能搞得晕头转向,每次想在Kibana里面查询某个字段的时候,查出来的结果经常不是自己想要的,然而又不知道问题出在了哪 ...
随机推荐
- n2n搭建手记-1-V1
搭建环境 supernode :阿里云主机一台 aly1(Centos 6.5) edg2node:美团云机器两台 mty1,mty2(Centos 7.0) Step-1 各机器安装subviers ...
- CodeForces - 283E Cow Tennis Tournament
Discription Farmer John is hosting a tennis tournament with his n cows. Each cow has a skill level s ...
- 【kruscal】【最小生成树】poj2421 Constructing Roads
SB题,求最小生成树,其中有些边已经给您建好啦. 随意暴力即可. #include<cstdio> #include<algorithm> #include<cstrin ...
- 西电大第十六届程序设计竞赛 A-GRE
题目描述 不愤不启不悱不发,王萌萌为了能够成功上研,开始刻苦背GRE单词,但是由于她过于刻苦,在背会英语单词的同时,把中文读音忘了.于是王萌萌又开始复习起中文发音,她先从数字开始复习起... ...
- 手动编译含package的java源程序(包含外部包中定义的类)
1)定义一个GSM类,如下: 包名是“SRC.GSM”,并且此程序引用了外部jar包.使用javac命令对GSM.java进行编译: GSM.java所在的文件夹如下所示: 切换到这个目录为当前工作目 ...
- python可变的参数列表
一般的计算机语言中参数的个数是不能改变的,但是在python中实参的个数是可以改变的.主要是通过形参中的*arg和**arg来实现的,使用可变参数必须遵守下面规则: 1.位置参数必须出现在这*arg参 ...
- Debian6 安装Kscope(也适用于Ubuntu)
参考:http://soft.chinabyte.com/os/134/12307634.shtml kscope1.6.2在这里下载,下载后解压出kscope-1.6.2.tar.gz. 在ubun ...
- fedora25安装和docker-ce_清华源
docker-ce_清华源 https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/fedora/ fedora25 docker-ce版本: htt ...
- cdev结构体及其相关函数
一.在Linux2.6内核中一个字符设备用cdev结构来描述,其定义如下: struct cdev { struct kobject kobj; struct module *owner; //所属模 ...
- javascript快速入门22--Ajax简介
Ajax是什么? 首先,Ajax是什么?一个很酷的新兴词汇!仅仅是某种早就有了的技术的一种新说法而已! Ajax是指一种创建交互式网页应用的网页开发技术.要谈到网页应用程序,则必须从WEB的历史来讲: ...