Elasticsearch 系列导航
elasticsearch 与 elasticsearch-head 的安装
ElasticSearch Index API && Mapping
持续更新中
正文
假设你已经有一份数据保存在Elasticsearch里,类似于下面这种schema,如果没有参考导入测试数据
{
"account_number": 0,
"balance": 16623,
"firstname": "Bradshaw",
"lastname": "Mckenzie",
"age": 29,
"gender": "F",
"address": "244 Columbus Place",
"employer": "Euron",
"email": "bradshawmckenzie@euron.com",
"city": "Hobucken",
"state": "CO"
}
那么我们接下来就可以 过滤,搜索,聚合来获取到我们想要的数据。
Elasticsearch提供了一套Json风格的领域特定语言来帮助查询,被称为Query DSL.
搜索通过在URL结尾加_search
来指定,具体查询提交通过Request Body来指定,
比如下面的Request Body:
query: 用来指定查询条件
from:从第几个开始取
size:取多少条记录,默认10条,比如这个例子有13条记录满足条件,但是只返回1条记录
sort:用来指定排序规则
OK,通过刚才的实验,我们对查询有了一个基本的认识,下面让我们来继续认识更加有趣的查询:
- 减少返回字段的个数(默认情况下是返回一个文档的所有字段信息)
{ "query": { "match_all": {} }, "_source": ["account_number", "balance"] }
- 返回account_number等于20的account
{ "query": { "match": { "account_number": 20 } } }
match是一个模糊匹配,但是由于account_number是long类型,所以这里当做精确匹配来过滤
- 返回address字段中包含mill的account
{ "query": { "match": { "address": "mill" } } }
由于address是text类型,所以这里说的是包含mill而不是等于mill.
- 返回address字段中包含"mill" 或 "lane"的account
{ "query": { "match": { "address": "mill lane" } } }
由于address是text类型,而且"mill lane"这里在查询的时候被当作两个词来分别进行查询
- 返回address字段中包含"mill lane"的account
这里使用match_phrase(短语匹配)查询类型,把"mill lane"当作一个整体来查询
{ "query": { "match_phrase": { "address": "mill lane" } } }
- 返回address字段中同时包含"mill" 和 "lane"的account
{ "query": { "bool": { "must": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }
这里使用了bool查询语句,它允许我们组合多个小的查询一起来完成稍微复杂的查询,
bool must
要求所有子查询返回true,所有子查询之间可以理解为一个and的操作。 - 返回address字段中包含"mill" 或 "lane"的account
bool should 要求子查询中的任一个满足条件,可以理解为或的关系
{ "query": { "bool": { "should": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }
- 返回address字段中既不包含"mill" 也不包含 "lane"的account
bool must_not
子句之间是或的关系{ "query": { "bool": { "must_not": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }
- 返回年龄等于40 且不住在ID地区的account
{ "query": { "bool": { "must": [ { "match": { "age": "40" } } ], "must_not": [ { "match": { "state": "ID" } } ] } } }
我们可以同时联合must
, should
, and must_not子句在一个bool语句内,
也可以继续在bool子句下面继续嵌套使用bool子句来完成更加复杂的查询需求。
Filter 过滤
在返回的结果中有一个_score字段,score是一个数值,表示查询条件和这个文档的相关度,分数越高,说明某个文档的相关度越高,
反之,相关度越低,但是查询 并不总是产生分数,尤其当你使用过滤子句来过滤文档的时候,Elasticsearch会自动检测这些场景,
自动优化查询,让他不要去计算无用的分数,之前我们使用的bool查询也支持filter子句,
例如我们想获取账户余额大于等于20000 小于等于30000的账户信息
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
上面的这个例子其实挺好理解的,所有在这个range范围内的文档都具有相等的匹配度,
没有哪一个文档比其他的文档匹配度更高,要么在这个范围内,要么不在,所以相关度是相等的,
就没有必要再去计算这个score.
Aggregations聚合
聚合允许你给你的数据分组并获取他们的统计信息,你可以把它和SQL里面的goup by 以及SQL的聚合函数联系起来,
在Elasticsearch,你可以在一个响应里同时返回聚合信息和结果明细,
比如我们使用state来给所有的accounts分组,默认返回前10条聚合记录,顺序按照组内文档数量的倒序排列
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
}
}
}
}
你可以结合下面的SQL语句更好理解上面的语句
SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC
部分返回结果 如下显示:
{
"took": 29,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits" : {
"total" : 1000,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"group_by_state" : {
"doc_count_error_upper_bound": 20,
"sum_other_doc_count": 770,
"buckets" : [ {
"key" : "ID",
"doc_count" : 27
}, {
"key" : "TX",
"doc_count" : 27
}, {
"key" : "AL",
"doc_count" : 25
}, {
"key" : "MD",
"doc_count" : 25
}, {
"key" : "TN",
"doc_count" : 23
}, {
"key" : "MA",
"doc_count" : 21
}, {
"key" : "NC",
"doc_count" : 21
}, {
"key" : "ND",
"doc_count" : 21
}, {
"key" : "ME",
"doc_count" : 20
}, {
"key" : "MO",
"doc_count" : 20
} ]
}
}
}
你可以观察到,上面的聚合我们设置size=0,不去显示符合条件的原始记录,
因为我们这次仅仅需要聚合的结果信息,如果你也需要原始记录信息,那么你可以重新指定size的大小
下面这个例子我们来求余额的平均值
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
返回如下的结果,可以看到这里我们在group_by_state
里面嵌套使用了average_balance
,这是一种比较通用的做法,
你可以在任意聚合内嵌套任意聚合来获取需要的统计信息。
下面这个例子演示根据年龄组来分组,然后根据性别来分组最后求账户余额的平均值
{
"size": 0,
"aggs": {
"group_by_age": {
"range": {
"field": "age",
"ranges": [
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 40
},
{
"from": 40,
"to": 50
}
]
},
"aggs": {
"group_by_gender": {
"terms": {
"field": "gender.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
}
}
下面是年龄组分组 计算聚合的部分返回结果:
下面