您的位置:首页 > 其它

Elasticsearch 6.1官方参考手册(一)入门(5)数据高级操作入门

2017-12-29 15:51 501 查看

探究你的数据

样本数据集

既然我们已经了解了基础知识,让我们来尝试操作一些更真实的数据集。我已经预先准备好了一些虚拟的顾客银行账户信息JSON文档样本。每一个文档都有如下的结构:

{
"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"
}


出于好奇,这些数据是在www.json-generator.com生成的。所有请忽略这些数据值的实际意义,因为它们都是随机生成的。

加载样本数据集

你可以从这里下载样本数据集(accounts.json)。把它放到我们当前的目录下,然后使用如下的命令把它加载到我们得集群中:

curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"
curl 'localhost:9200/_cat/indices?v'


返回结果为:

health status index uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   bank  l7sSYV2cQXmu6_4rJWVIww   5   1       1000            0    128.6kb        128.6kb


这意味着我们刚刚成功将1000个文档批量放入bank索引中(在account类型下)。

搜索API

现在,让我们从一些简单的搜索指令开始。执行搜索有两种基础的方式,一种是在请求的URL中加入参数来实现,另一种方式是将请求内容放到请求体中。使用请求体可以让你的JSON数据以一种更加可读和更加富有展现力的方式发送。我们将会在一开始演示一次使用请求URI的方式,然后在本教程剩余的部分,我们将统一使用请求体的方式发送。

REST API可以使用
_search
端点来实现搜索。如下的示例将返回bank索引的所有的文档:

HTTP请求:

GET /bank/_search?q=*&sort=account_number:asc&pretty


Curl命令:

curl -XGET 'localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty&pretty'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_the_search_api/1.json


让我们首先来详细分析一下这个搜索请求。这个请求在bank索引中进行搜索(使用
_search
端点),然后
q=*
参数命令Elasticsearch匹配索引中的全部文档。
sort=account_number:asc
参数表示按
account_number
属性升序排列返回的结果。
pretty
参数之前已经提到过,就是将返回结果以美观的格式返回。

返回结果为(展示部分):

{
"took" : 63,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : null,
"hits" : [ {
"_index" : "bank",
"_type" : "account",
"_id" : "0",
"sort": [0],
"_score" : null,
"_source" : {"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"}
}, {
"_index" : "bank",
"_type" : "account",
"_id" : "1",
"sort": [1],
"_score" : null,
"_source" : {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
}, ...
]
}
}


关于返回结果,我们看到了如下的部分:

took
- Elasticsearch执行此次搜索所用的时间(单位:毫秒)

timed_out
- 告诉我们此次搜索是否超时

_shards
- 告诉我们搜索了多少分片,还有搜索成功和搜索失败的分片数量

hits
- 搜索结果

hits.total
- 符合搜索条件的文档数量

hits.hits
- 实际返回的搜索结果对象数组(默认只返回前10条)

hits.sort
- 返回结果的排序字段值(如果是按score进行排序,则没有)

hits._score
max_score
- 目前先忽略这两个字段

如下是相同效果的另一种将数据放入请求体的方式:

HTTP请求内容:

GET /bank/_search
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_the_search_api/2.json


这里的不同点在于我们使用一个JSON格式的请求体代替了在URI中的
q=*
参数。我们将在下一节详细讨论这种JSON格式的查询方式。

一旦你得到了返回结果,Elasticsearch就完全执行结束,不会保持任何的服务器资源或者往你的结果里加入开放的游标,理解这一点是非常重要的。这同狠多其他的平台比如SQL数据库的一些特性形成了鲜明的对比,比如在SQL数据库中你可能在查询时,会首先得到查询结果的一部分,然后你需要通过一些有状态的服务端游标不断地去请求服务端来取得剩余的查询结果。

介绍查询语言

Elasticsearch提供了一种JSON格式的领域特定语言,你可以使用它来执行查询。这个通常叫做Query DSL。这门查询语言相当的全面以至于你第一次看到它时会被它吓住,不过学习它最好的方式就是从一些简单的示例程序开始。

回到我们上个例子,我们执行了这个查询:

HTTP请求内容:

GET /bank/_search
{
"query": { "match_all": {} }
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} }
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_introducing_the_query_language/1.json


分析以上查询,
query
部分告诉我们我们的查询定义是什么,
match_all
部分简单指定了我们想去执行的查询类型,意思就是在索引中搜索所有的文档。

除了
query
参数,我们还可以通过其他的参数影响搜索结果。在上一节的示例中我们使用了
sort
来指定搜索结果的顺序,这里我们指定
size
来指定返回的结果数量:

HTTP请求内容:

GET /bank/_search
{
"query": { "match_all": {} },
"size": 1
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"size": 1
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_introducing_the_query_language/2.json


注意如果
size
没有指定,它默认为10。

如下的示例使用
match_all
并返回了11到20的文档:

HTTP请求内容:

GET /bank/_search
{
"query": { "match_all": {} },
"from": 10,
"size": 10
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"from": 10,
"size": 10
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_introducing_the_query_language/3.json


from
参数(从0开始)指定了从哪个文档索引开始,
size
参数指定了从
from
指定的索引开始返回多少个文档。这个特性在实现分页搜索时很有用。注意如果
from
参数没有指定,它默认为0。

如下示例使用
match_all
并且按账户的balance值进行倒序排列后返回前10条文档:

HTTP请求内容:

GET /bank/_search
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_introducing_the_query_language/4.json


执行搜索

既然我们已经了解了一些基础的搜索参数,那就让我们来深入学习一下Query DSL吧。首先,我们来关注一下返回的文档属性。默认情况下,文档会作为搜索结果的一部分返回所有的属性值。这个文档的JSON内容被称为source(返回结果中的hits的_source属性值)。如果我们不需要返回所有的source文档属性,我们可以在请求体中加入我们需要返回的属性名。

如下的示例演示了如何返回两个属性,
account_number
balance
(在
_source
中):

HTTP请求内容:

GET /bank/_search
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_executing_searches/1.json


注意上面的例子仅仅只是减少了
_source
里的属性。它仍然会返回
_source
属性,只不过
_source
属性中之包含
account_number
balance
两个属性。

如果你之前学过SQL,上面的示例有点像SQL中的
SELECT
FROM
中指定返回的字段列表。

现在,让我们的视线转到查询部分。之前我们已经看到如何使用
match_all
来匹配所有的文档。现在让我们介绍一个新的查询叫做
match
查询
,它可以被认为是基本的属性搜索查询(就是通过特定的一个或多个属性来搜索)。

如下的示例返回account_number为20的文档:

HTTP请求内容:

GET /bank/_search
{
"query": { "match": { "account_number": 20 } }
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": { "match": { "account_number": 20 } }
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_executing_searches/2.json


如下示例返回所有的address字段中包含“mill”这个单词的账户文档:

HTTP请求内容:

GET /bank/_search
{
"query": { "match": { "address": "mill" } }
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": { "match": { "address": "mill" } }
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_executing_searches/3.json


如下示例返回所有的address字段中包含“mill”或者是“lane”的账户文档:

HTTP请求内容:

GET /bank/_search
{
"query": { "match": { "address": "mill lane" } }
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": { "match": { "address": "mill lane" } }
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_executing_searches/4.json


如下示例是
match
的一种变体(
match_phrase
),这个将返回所有address中包含“mill lane”这个短语的账户文档:

HTTP请求内容:

GET /bank/_search
{
"query": { "match_phrase": { "address": "mill lane" } }
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": { "match_phrase": { "address": "mill lane" } }
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_executing_searches/5.json


现在让我们介绍
bool
查询
bool
查询允许我们使用布尔逻辑将小的查询组成大的查询。

如下的示例组合两个
match
查询并且返回所有address属性中包含 “mill” “lane” 的账户文档:

HTTP请求内容:

GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_executing_searches/6.json


在上述示例中,
bool
must
子句指定了所有匹配文档必须满足的条件。

相比之下,如下的示例组合两个
match
查询并且返回所有address属性中包含 “mill” “lane” 的账户文档:

HTTP请求内容:

GET /bank/_search
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_executing_searches/7.json


在上述的例子中,
bool
should
子句指定了匹配文档只要满足其中的任何一个条件即可匹配。

如下示例组合两个
match
查询并且返回所有address属性中既不包含 “mill” 也不包含 “lane” 的账户文档:

HTTP请求内容:

GET /bank/_search
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_executing_searches/8.json


在上述例子中,
bool
must_not
子句指定了其中的任何一个条件都不满足时即可匹配。

我们可以在一个
bool
查询中同时指定
must
should
must_not
子句。此外,我们也可以在一个
bool
子句中组合另一个
bool
来模拟任何复杂的多重布尔逻辑。

如下的示例返回所有age属性为40,并且state属性不为ID的账户文档:

HTTP请求内容:

GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_executing_searches/9.json


执行过滤

在之前的章节中,我们跳过了一个叫做文档得分(在搜索结果中的
_score
属性)的小细节。这个得分是一个数值,它是一个相对量,用来衡量搜索结果跟我们指定的关键字的相关程度。分数越高,说明这个文档的相关性越大,分数越低,说明这个文档的相关性越小。

但是一些查询结果并不总是需要产生得分,尤其是当他们仅仅被用来过滤文档集的时候。Elasticsearch会检测这种情况并自动优化查询以免计算无用的分数。

我们在前面章节介绍的
bool
查询
也支持
filter
子句,它允许我们可以在不改变得分计算逻辑的的情况下限制其他子句匹配的查询结果。为了示例说明,让我们介绍一下
range
查询
,它允许我们通过一个值区间来过滤文档。这个通常用在数值和日期过滤上。

如下的示例使用bool查询返回所有余额在20000到30000之间的账户(包含边界)。换句话说,我们想查询账户余额大于等于20000并且小于等于30000的用户。

HTTP请求内容:

GET /bank/_search
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_executing_filters/1.json


仔细分析一下上面的例子,bool查询在查询部分使用
match_all
,在过滤部分使用
range
。我们可以使用任何的查询来代替查询部分和过滤部分。在上面的例子中,range查询让结果更加合乎情理,因为文档在这个区间中一定是符合的,就是说,没有比这些相关性更大的了。

除了
match_all
match
bool
,和
range
查询之外,还有很多其他的查询类型,在这里我们就不一一介绍了。当我们对这些基础的理解了之后,再去学习和使用其他的查询类型应该是不会太难了。

执行聚合

聚合提供了功能可以分组并统计你的数据。理解聚合最简单的方式就是可以把它粗略的看做SQL的GROUP BY操作和SQL的聚合函数。在Elasticsearch中,你可以在执行搜索后在一个返回结果中同时返回搜索结果和聚合结果。你可以使用简洁的API执行搜索和多个聚合操作,并且可以一次拿到所有的结果,避免网络切换,就此而言,这是一个非常强大和高效功能。

作为开始,如下的例子将账户按state进行分组,然后按count降序(默认)返回前10组(默认)states。

HTTP请求内容:

GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
}
}
}
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
}
}
}
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_executing_aggregations/1.json


上面的聚合的例子跟如下的SQL类似:

SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC


返回结果为(展示部分):

{
"took": 29,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped" : 0,
"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
} ]
}
}
}


我们可以看到有27个账户在
ID
(爱达荷州),然后27个在
TX
(得克萨斯州),还有25个在
AL
(亚拉巴马州),等等。

注意我们设置了
size=0
来不显示hits搜索结果,因为我们这里只关心聚合结果。

如下示例我们在上一个聚合的基础上构建,这个示例计算每个state分组的平均账户余额(还是使用默认按count倒序返回前10个):

HTTP请求内容:

GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_executing_aggregations/2.json


注意我们是怎么嵌套
average_balance
聚合到
group_by_state
聚合中的。

这是一个适用于所有聚合操作的通用模式。你可以任意嵌套聚合,从你的数据中提取你需要的主题汇总。

如下例子依然是在之前的聚合上构建,我们现在来按平均余额倒序排列:

HTTP请求内容:

GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_executing_aggregations/3.json


如下示例演示我们如何按年龄区间分组(20-29,30-39,40-49),然后按性别,最后获取每个年龄区间,每个性别的平均账户余额:

HTTP请求内容:

GET /bank/_search
{
"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"
}
}
}
}
}
}
}
}


curl命令:

curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
{
"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"
}
}
}
}
}
}
}
}
'


Kibana Console:

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_executing_aggregations/4.json


还有很多其它的聚合功能在这里我们就不去详细介绍了。如果你想了解更多,可以参考聚合参考手册
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息