_search
Elasticsearch是一个近乎实时的搜索平台。它提供了一个分布式的全文搜索引擎,提供了REST API接口与用户交互。Elasticsearch是用Java语言开发的,基于Apache协议的开源项目,是目前最受欢迎的企业搜索引擎。Elasticsearch广泛运用于云计算中,能够达到实时搜索,具有稳定,可靠,快速的特点。
如何与Elasticsearch交流,Elasticsearch提供了一个非常全面和强大的REST API,您可以使用它
官网说明了以下概念。
注:
尽管作者前面写过Docker安装Elasticsearch、Kibana的文章,但是后期分析Docker方式体验很差,这里并不适用Docker安装,没有给我们带来方便,所以这里不推荐Docker安装方式而是使用安装包方式。
Elasticsearch和Kibana的版本要求保持一致。
Elasticsearch是近乎实时的搜索平台,提供了REST API接口与用户交互,所以后面的案例本可以只安装Elasticsearch就够了。但是为了方便起见,我们选择多安装一个Elasticsearch的可视化平台Kibana来操作后面的案例。以Elasticsearch6.6.2为例:
curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.2.tar.gz
tar -xvf elasticsearch-6.6.2.tar.gz
cd elasticsearch-6.6.2
./bin/elasticsearch
curl -O https://artifacts.elastic.co/downloads/kibana/kibana-6.6.2-darwin-x86_64.tar.gz
tar -xzf kibana-6.6.2-darwin-x86_64.tar.gz
cd kibana-6.6.2-darwin-x86_64/
./bin/kibana
能正常点击左侧菜单就没有问题,zipkin是我测试zipkin时的索引,可忽略
GET /_cat/health?v
GET /_cat/nodes?v
GET /_cat/indices?v
PUT /customer
GET /_cat/indices?v
DELETE /customer
GET /_cat/indices?v
PUT /customer/_doc/1
{
"name": "John Doe"
}
GET /customer/_doc/1
PUT /customer/_doc/1?pretty
{
"name": "John Doe"
}
POST /customer/_doc/1/_update?pretty
{
"doc": { "name": "Jane Doe" }
}
POST /customer/_doc/1/_update?pretty
{
"doc": { "name": "Jane Doe", "age": 20 }
}
POST /customer/_doc/1/_update?pretty
{
"script" : "ctx._source.age += 5"
}
修改跟替换是不同的
DELETE /customer/doc/1
POST /customer/doc/_bulk
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
查询表达式(Query DSL)是一种非常灵活又富有表现力的查询语言,Elasticsearch使用它可以以简单的JSON接口来实现丰富的搜索功能,下面的搜索操作都将使用它。
数据搜索才是Elasticsearch的重点内容。
{
"account_number": 0,
"balance": 16623,
"firstname": "Bradshaw",
"lastname": "Mckenzie",
"age": 29,
"gender": "F",
"address": "244 Columbus Place",
"employer": "Euron",
"email": "[email protected]",
"city": "Hobucken",
"state": "CO"
}
可以在当前目录用命令导入
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json"
也可以在Kibana的Dev Tools中批量导入。
bank
索引中已经创建了1000条文档GET /_cat/indices?v
match_all
来表示,例如搜索全部;GET /bank/_search
{
"query": { "match_all": {} }
}
from
表示偏移量,从0开始,size
表示每页显示的数量;GET /bank/_search
{
"query": { "match_all": {} },
"from": 0,
"size": 10
}
sort
表示,例如按balance
字段降序排列;GET /bank/_search
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
}
_source
表示,例如只返回account_number
和balance
两个字段内容:GET /bank/_search
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}
match
表示匹配条件,例如搜索出account_number
为20
的文档:GET /bank/_search
{
"query": { "match": { "account_number": 20 } }
}
address
字段中包含mill
的文档,对比上一条搜索可以发现,对于数值类型match
操作使用的是精确匹配,对于文本类型使用的是模糊匹配;GET /bank/_search
{
"query": { "match": { "address": "mill" } },
"_source": ["address", "account_number"]
}
match_phrase
表示,例如搜索address
字段中包含mill lane
的文档GET /bank/_search
{
"query": { "match_phrase": { "address": "mill lane" } }
}
bool
来进行组合,must
表示同时满足,例如搜索address
字段中同时包含mill
和lane
的文档;GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
should
表示满足其中任意一个,搜索address
字段中包含mill
或者lane
的文档;GET /bank/_search
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
must_not
表示同时不满足,例如搜索address
字段中不包含mill
且不包含lane
的文档;GET /bank/_search
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
must
和must_not
,例如搜索age
字段等于40
且state
字段不包含ID
的文档;GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
filter
来表示,例如过滤出balance
字段在20000~30000
的文档;GET /bank/_search
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
aggs
来表示,类似于MySql中的group by
,例如对state
字段进行聚合,统计出相同state
的文档数量;GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
}
}
}
}
类似于SQL语句中的
SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC LIMIT 10;
"size": 0
只要聚合结果
state
字段进行聚合,统计出相同state
的文档数量,再统计出balance
的平均值;GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
balance
的平均值降序排列;GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
age
字段的[20,30]
[30,40]
[40,50]
,之后按gender
统计文档个数和balance
的平均值;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"
}
}
}
}
}
}
}
}
官网入门案例:https://www.elastic.co/guide/en/elasticsearch/reference/6.6/getting-started.html