高亮
GET /suggest_carinfo/_search { "query": { "bool": { "should": [ { "match": { "title": "宝马" } }, { "match": { "content": "宝马" } } ] } }, "highlight": { "pre_tags": "<b>", #起始标签 "post_tags": "</b>",#结束标签 "fields": { #每个高亮字段都需要对应一个查询 "title": {}, "content": {} } } }
Suggest(搜索推荐)
- term suggester:根据词项的词频来推荐
- phrase suggester:短语词频搜索来推荐
- completion suggester:自动补全,自动完成,支持三种查询【前缀查询、模糊查询、正则表达式查询】
根据词项的词频来推荐
GET /news/_search { "suggest": { "my-suggestion": { "text": "baoqing baoqiang", "term": { "suggest_mode": "always", "field": "title", "max_edits":2, "max_term_freq":1 } } } }
自动补全推荐
#complate suggester PUT suggest_carinfo { "mappings": { "properties": { "title": { "type": "text", "analyzer": "ik_max_word", "fields": { "suggest": { "type": "completion", "analyzer": "ik_max_word" } } }, "content": { "type": "text", "analyzer": "ik_max_word" } } } } POST _bulk {"index":{"_index":"suggest_carinfo","_id":1}} {"title":"宝马X5 两万公里准新车","content":"这里是宝马X5图文描述"} {"index":{"_index":"suggest_carinfo","_id":2}} {"title":"宝马5系","content":"这里是奥迪A6图文描述"} {"index":{"_index":"suggest_carinfo","_id":3}} {"title":"宝马3系","content":"这里是奔驰图文描述"} {"index":{"_index":"suggest_carinfo","_id":4}} {"title":"奥迪Q5 两万公里准新车","content":"这里是宝马X5图文描述"} {"index":{"_index":"suggest_carinfo","_id":5}} {"title":"奥迪A6 无敌车况","content":"这里是奥迪A6图文描述"} {"index":{"_index":"suggest_carinfo","_id":6}} {"title":"奥迪双钻","content":"这里是奔驰图文描述"} {"index":{"_index":"suggest_carinfo","_id":7}} {"title":"奔驰AMG 两万公里准新车","content":"这里是宝马X5图文描述"} {"index":{"_index":"suggest_carinfo","_id":8}} {"title":"奔驰大G 无敌车况","content":"这里是奥迪A6图文描述"} {"index":{"_index":"suggest_carinfo","_id":9}} {"title":"奔驰C260","content":"这里是奔驰图文描述"} GET suggest_carinfo/_search?pretty { "suggest": { "car_suggest" : { "prefix" : "宝马X", "completion" : { "field" : "title.suggest" } } } } #1:内存代价太大,原话是:性能高是通过大量的内存换来的 #2:只能前缀搜索,假如用户输入的不是前缀 召回率可能很低 POST suggest_carinfo/_search { "suggest": { "car_suggest": { "prefix": "宝马5系", "completion": { "field": "title.suggest", "skip_duplicates":true, "fuzzy": { "fuzziness": 2 #允许后面2个字进行模糊匹配 } } } } }
1.经纬度坐标. 2.latitude:维度 缩写:lat 3.longitude:经度 缩写:lon 4.坐标范围Lat值为[-90,90],Lon为[-180,180]
geo_point 查询type
1.geo_bounding_box query(矩形查询):在同一个平面内,两个点确定一个矩形,搜索矩形内的坐标。 a.top_left:矩形左上点坐标 b.bottom_right:矩形右上角表 2.geo_distance query(半径查询):以某个点为圆心查找指定半径的圆内的坐标。 a.distance:距离单位,默认是米 3.geo_polygon query(多边形):查找给定多个点连成的多边形内的坐标。
ES的特殊类型之一,用来描述复杂的几何图形的类型,比如点、线、面,多边形等二维几何模型。常用来制作省市区地理位置、面积形状。
geo_shape type(地理形状类型)
1.点(point) 2.线段(linestring) 3.多边形(polygon) 2.矩形(envelope) 3.多边形 (polygon) 4.圆形(circle) a.圆的多边形的精度定义为error_distance。这种差异越小,多边形越接近理想圆。最小边数为4,最大为1000。 geo_shape query(特殊几何形状):支持指定几何图形相交、包含或是不相交等图形检索 a. intersects 有相交关系 b. disjoint 不想交 c. within 包含 d. contains 被包含
PUT geo_point { "mappings": { "properties": { "name": { "type": "text" }, "location": { "type": "geo_point" } } } } PUT geo_point/_doc/1 { "name": "天安门", "location": { "lat": 40.12, "lon": -71.34 } } PUT geo_point/_doc/2 { "name": "前门", "location": "25.23,50.54" } PUT geo_point/_doc/3 { "name": "后门", "location": [30.16,60.54] } #矩形搜索 GET /geo_point/_search { "query": { "geo_bounding_box": { "location": { "top_left": { "lat": 50.73, "lon": -74.1 }, "bottom_right": { "lat": 30.01, "lon": -61.12 } } } } } #半径搜索 GET /geo_point/_search { "query": { "bool": { "filter": [ { "geo_distance": { "distance": "50km", "location": { "lat": 40, "lon": -71 } } } ] } } } #多边形查找 GET geo_point/_search { "query": { "bool": { "must": [ { "match_all": {} } ], "filter": [ { "geo_polygon": { "location": { "points": [ { "lat": 40, "lon": -70 }, { "lat": 40, "lon": -71 }, { "lat": 50, "lon": -71.1 } ] } } } ] } } } #排序 GET /geo_point/_search { "query": { "bool": { "filter": [ { "geo_distance": { "distance": "500km", "location": { "lat": 40, "lon": -71 } } } ] } }, "sort": [ { "_geo_distance": { "location": { "lat": 40, "lon": -71 }, "order": "desc" } } ] }
#创建mapping PUT geo_shape { "mappings": { "properties": { "location": { "type": "geo_shape" } } } } #储存一个点 POST /geo_shape/_doc/1 { "name":"中国 香海", "location":{ "type":"point", "coordinates":[13.400544, 52.530286] } } #储存一个线段 POST /geo_shape/_doc/2 { "name":"随意", "location":{ "type":"linestring", "coordinates":[[13.400544, 52.530286],[-77.400544, 38.530286]] } } #存储一个多边形, 注意格式[[],[]],里面可以存放多个并列的面 POST /geo_shape/_doc/3 { "name": "河北省", "location": { "type": "polygon", "coordinates": [ [[100,0],[101,0],[101,1],[100,1],[100,0]], [[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]] ] } }
PUT _ingest/pipeline/polygonize_circles { "description": "圆圈转换成多边形", "processors": [ { "circle": { "field": "location", "error_distance": 0, "shape_type": "geo_shape" } } ] } #放入圆形 POST /geo_shape/_doc/4?pipeline=polygonize_circles { "name": "安全区", "location": { "type": "circle", "coordinates": [30.0, 10.0], "radius":"1m" } } #放入点 POST /geo_shape/_doc/1 { "name": "中国,香海", "location": { "type": "point", "coordinates": [13.400544, 52.530286] } } #查询矩形内包含的点线面 GET geo_shape/_search { "query": { "bool": { "filter": [ { "geo_shape": { "location": { "shape": { "type": "envelope", "coordinates": [ [ 13, 53 ], [ 14, 52 ] ] }, "relation": "within" #包含 } } } ] } } }
几何图形relation
PUT geo_shape_test { "mappings": { "properties": { "location": { "type": "geo_shape" } } } } #存矩形 POST /geo_shape_test/_doc/A { "location": { "type": "envelope", "coordinates": [[1,7],[6,1]] } } POST /geo_shape_test/_doc/B { "location": { "type": "envelope", "coordinates": [[4,8],[8,5]] } } POST /geo_shape_test/_doc/C { "location": { "type": "envelope", "coordinates": [[2,4],[4,2]] } } #P1 POST /geo_shape_test/_doc/P1 { "name":"P1", "location":{ "type":"point", "coordinates":[3, 3] } } #P2 POST /geo_shape_test/_doc/P2 { "name":"P2", "location":{ "type":"point", "coordinates":[5, 6] } } #P3 POST /geo_shape_test/_doc/P3 { "name":"P3", "location":{ "type":"point", "coordinates":[7, 7] } } #P4 POST /geo_shape_test/_doc/P4 { "name":"P4", "location":{ "type":"point", "coordinates":[3, 5] } } #P5 POST /geo_shape_test/_doc/P5 { "name":"P5", "location":{ "type":"point", "coordinates":[7, 3] } } GET /geo_shape_test/_search { "query": { "bool": { "filter": { "geo_shape": { "location": { "indexed_shape": { "index": "geo_shape_test", "id": "A", "path": "location" }, "relation": "contains" } } } } } }
疫情地图
#思路 1.设计mapping 使用地理类型 2.插入省市区各个图形形状 3.分组统计现有确诊 4.分组统计当日新增 5.搜索附近X公里(一个点+半径) 6.以上聚合多,开启正排索引(.keyword) 或者对聚合字段fielddata: true
# 不推荐,会大量消耗堆内存,最好创建的时候设置上
PUT product/_mapping
{
"properties": {
"tags": {
"type": "text",
"fielddata": true
}
}
}