Elastic search(构建于Lucene之上)在一个容易管理的包中提供了高性能的全文搜索功能,支持开箱即用地集群化扩展。能够经过标准的REST API或从特定于编程语言的客户端库与Elasticsearch进行交互。数据库
一、首先了解一下es的主要构成:编程
①索引(index)我的能够理解为关系型数据库的库app
②类型(type)我的能够理解为关系型数据库的表编程语言
③文档(document)我的能够理解为关系型的惟一主键性能
二、es一些关键词code
query下面能够加xml
②bool 布尔值索引
③must 至关因而关系型数据库的andci
④filter 和bool的做用差很少,可是filter的查询效率更高文档
⑤term 指定的要查询的某个字段
agg 下面能够加
①term 指定的字段
②agg 至关因而关系型数据库的groupby
③cardinality 去重至关因而关系型的distinct
④sum max min
等等
三、去重例子,在kibana执行以下
GET antifraud_enriched_prod-2017.06.17/_search { "size": 0 , "aggs": { "res_distinct": { "cardinality": { "field": "unstruct_event_com.cid" , "precision_threshold": 40000 } } } , "query": { "bool": { "must": [ { "range": { "derived_tstamp": { "gte": "2017-06-17T10:49:46.685Z", "lte": "2017-06-17T10:51:46.685Z" } } } ] } } }
执行以下:
field表明是存放在es的字段,这个字段我在mapping的时候没有分词,若是分词的话要在field后面加上.raw
右边total表明的是返回值,res_distinct表明的是根据cid去重后的结果
es的distinct自己是存在问题的,precision_threshold:指定的去重范围,若是查出来的数据超出了你指定的这个范围,查出来的数据就是有点不许确了,默认好像是100
四、指定字段查询
①一种查法:bool
GET antifraud_enriched_prod-2017.06.18/_search { "size": 0, "query": { "bool": { "must": [ { "term": { "event_name": { "value": "login" } } } ] } } }
执行结果:
②另外一种查法:filter
GET antifraud_enriched_prod-2017.06.18/_search { "size": 0, "query": { "filtered": { "query": { "match_all": {} }, "filter": { "term": { "event_name": "login" } } } } }
执行结果
五、agg的例子
GET antifraud_enrich-2017.06a/_search { "size": 0, "aggs": { "mobile_city": { "terms": { "field": "unstruct_event_com_xhqb.mobileCity" , "size": 5 } , "aggs": { "avg": { "sum": { "field": "unstruct_event_com_xhqb.loanAmount" } } } } } , "query": { "bool": { "must": [ { "range": { "derived_tstamp": { "gte": "2017-06-01T10:25:23.947Z", "lte": "2017-06-01T10:28:23.947Z" } } } ] } } }
执行结果:
其中结果:sum_other_doc_count 表明是groupby mobilcity字段的结果,key是sum_other_doc_count 的前提后groupby loanmount字段的结果
注意通常使用agg的话不要指定size=0,若是这样的话,数据会所有刷出kibana,数据量过大的话,kibana直接挂掉,曾经出现过这些问题
五、地理位置查询
GET antifraud_enrich-2017.06a/_search { "size": 0, "query": { "bool": { "must": [ { "term": { "event_name": { "value": "app_apply" } } },{ "range": { "collector_tstamp": { "gte": "2017-06-17T09:02:26.548", "lte": "2017-06-17T09:22:26.548" } } },{ "geo_distance" : { "unstruct_event_com_xhqb_app_apply_1.geolocation" : [ 121.499754782832099, 31.267086143900748 ], "distance" : "1.0km" } } ] } } }
查询结果
其中geolocation是地理位置字段, distance是1km的范围内,也就是说这个点的一千米范围里查出来有几个,这个字段比较特殊必定要是geo_point类型的,看例子
今天先写到这里