目录
1.1 数据准备
1.2 geo_distance query
1.3 geo_bounding_box query
1.4 geo_polygon query
1.5 geo_shape query
2.ElasticSearch 特殊查询
2.1 more_like_this query
2.2 script query
2.3 percolate query
1.1 数据准备创建一个索引:
PUT geo { "mappings": { "properties": { "name":{ "type": "keyword" }, "location":{ "type": "geo_point" } } } }
准备一个 geo.json 文件:
{"index":{"_index":"geo","_id":1}} {"name":"西安","location":"34.288991865037524,108.9404296875"} {"index":{"_index":"geo","_id":2}} {"name":"北京","location":"39.926588421909436,116.43310546875"} {"index":{"_index":"geo","_id":3}} {"name":"上海","location":"31.240985378021307,121.53076171875"} {"index":{"_index":"geo","_id":4}} {"name":"天津","location":"39.13006024213511,117.20214843749999"} {"index":{"_index":"geo","_id":5}} {"name":"杭州","location":"30.259067203213018,120.21240234375001"} {"index":{"_index":"geo","_id":6}} {"name":"武汉","location":"30.581179257386985,114.3017578125"} {"index":{"_index":"geo","_id":7}} {"name":"合肥","location":"31.840232667909365,117.20214843749999"} {"index":{"_index":"geo","_id":8}} {"name":"重庆","location":"29.592565403314087,106.5673828125"}
最后,执行如下命令,批量导入 geo.json 数据:
curl -XPOST "http://localhost:9200/geo/_bulk?pretty" -H "content-type:application/json" --data-binary @geo.json
可能用到的工具网站:
http://geojson.io/#map=6/32.741/116.521
1.2 geo_distance query给出一个中心点,查询距离该中心点指定范围内的文档:
GET geo/_search { "query": { "bool": { "must": [ { "match_all": {} } ], "filter": [ { "geo_distance": { "distance": "600km", "location": { "lat": 34.288991865037524, "lon": 108.9404296875 } } } ] } } }
以(34.288991865037524,108.9404296875) 为圆心,以 600KM 为半径,这个范围内的数据。
1.3 geo_bounding_box query在某一个矩形内的点,通过两个点锁定一个矩形:
GET geo/_search { "query": { "bool": { "must": [ { "match_all": {} } ], "filter": [ { "geo_bounding_box": { "location": { "top_left": { "lat": 32.0639555946604, "lon": 118.78967285156249 }, "bottom_right": { "lat": 29.98824461550903, "lon": 122.20642089843749 } } } } ] } } }
以南京经纬度作为矩形的左上角,以舟山经纬度作为矩形的右下角,构造出来的矩形中,包含上海和杭州两个城市。
1.4 geo_polygon query在某一个多边形范围内的查询。
GET geo/_search { "query": { "bool": { "must": [ { "match_all": {} } ], "filter": [ { "geo_polygon": { "location": { "points": [ { "lat": 31.793755581217674, "lon": 113.8238525390625 }, { "lat": 30.007273923504556, "lon":114.224853515625 }, { "lat": 30.007273923504556, "lon":114.8345947265625 } ] } } } ] } } }
给定多个点,由多个点组成的多边形中的数据。
1.5 geo_shape querygeo_shape 用来查询图形,针对 geo_shape,两个图形之间的关系有:相交、包含、不相交。
新建索引:
PUT geo_shape { "mappings": { "properties": { "name":{ "type": "keyword" }, "location":{ "type": "geo_shape" } } } }
然后添加一条线:
PUT geo_shape/_doc/1 { "name":"西安-郑州", "location":{ "type":"linestring", "coordinates":[ [108.9404296875,34.279914398549934], [113.66455078125,34.768691457552706] ] } }
接下来查询某一个图形中是否包含该线:
GET geo_shape/_search { "query": { "bool": { "must": [ { "match_all": {} } ], "filter": [ { "geo_shape": { "location": { "shape": { "type": "envelope", "coordinates": [ [ 106.5234375, 36.80928470205937 ], [ 115.33447265625, 32.24997445586331 ] ] }, "relation": "within" } } } ] } } }
relation 属性表示两个图形的关系:
-
within 包含
-
intersects 相交
-
disjoint 不相交
more_like_this query 可以实现基于内容的推荐,给定一篇文章,可以查询出和该文章相似的内容。
GET books/_search { "query": { "more_like_this": { "fields": [ "info" ], "like": "大学战略", "min_term_freq": 1, "max_query_terms": 12 } } }
-
fields:要匹配的字段,可以有多个
-
like:要匹配的文本
-
min_term_freq:词项的最低频率,默认是 2。特别注意,这个是指词项在要匹配的文本中的频率,而不是 es 文档中的频率
-
max_query_terms:query 中包含的最大词项数目
-
min_doc_freq:最小的文档频率,搜索的词,至少在多少个文档中出现,少于指定数目,该词会被忽略
-
max_doc_freq:最大文档频率
-
analyzer:分词器,默认使用字段的分词器
-
stop_words:停用词列表
-
minmum_should_match
脚本查询,例如查询所有价格大于 200 的图书:
GET books/_search { "query": { "bool": { "filter": [ { "script": { "script": { "lang": "painless", "source": "if(doc['price'].size()!=0){doc['price'].value > 200}" } } } ] } } }2.3 percolate query
percolate query 译作渗透查询或者反向查询。
-
正常 *** 作:根据查询语句找到对应的文档 query->document
-
percolate query:根据文档,返回与之匹配的查询语句,document->query
应用场景:
-
价格监控
-
库存报警
-
股票警告
-
...
例如阈值告警,假设指定字段值大于阈值,报警提示。
percolate mapping 定义:
PUT log { "mappings": { "properties": { "threshold":{ "type": "long" }, "count":{ "type": "long" }, "query":{ "type":"percolator" } } } }
percolator 类型相当于 keyword、long 以及 integer 等。
插入文档:
PUT log/_doc/1 { "threshold":10, "query":{ "bool":{ "must":{ "range":{ "count":{ "gt":10 } } } } } }
最后查询:
GET log/_search { "query": { "percolate": { "field": "query", "documents": [ { "count":3 }, { "count":6 }, { "count":90 }, { "count":12 }, { "count":15 } ] } } }
查询结果中会列出不满足条件的文档。
查询结果中的 _percolator_document_slot 字段表示文档的 position,从 0 开始计。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)