SpringBoot实战电商项目mall(30k+star)地址:github.com/macrozheng/…html
记得刚接触Elasticsearch的时候,没找啥资料,直接看了遍Elasticsearch的中文官方文档,中文文档好久没更新了,一直都是2.3的版本。最近又从新看了遍6.0的官方文档,因为官方文档介绍的内容比较多,每次看都很费力,因此此次整理了其中最经常使用部分,写下了这篇入门教程,但愿对你们有所帮助。node
Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式的全文搜索引擎,基于restful web接口。Elasticsearch是用Java语言开发的,基于Apache协议的开源项目,是目前最受欢迎的企业搜索引擎。Elasticsearch普遍运用于云计算中,可以达到实时搜索,具备稳定,可靠,快速的特色。git
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.2/elasticsearch-analysis-ik-6.2.2.zip
复制代码
docker pull elasticsearch:6.4.0
复制代码
sysctl -w vm.max_map_count=262144
复制代码
docker run -p 9200:9200 -p 9300:9300 --name elasticsearch \
-e "discovery.type=single-node" \
-e "cluster.name=elasticsearch" \
-v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
-d elasticsearch:6.4.0
复制代码
/usr/share/elasticsearch/data
目录没有访问权限,只须要修改该目录的权限,再从新启动便可;chmod 777 /mydata/elasticsearch/data/
复制代码
docker exec -it elasticsearch /bin/bash
#此命令须要在容器中运行
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.0/elasticsearch-analysis-ik-6.4.0.zip
docker restart elasticsearch
复制代码
docker pull kibana:6.4.0
复制代码
docker run --name kibana -p 5601:5601 \
--link elasticsearch:es \
-e "elasticsearch.hosts=http://es:9200" \
-d kibana:6.4.0
复制代码
elasticsearch
的群集。注意
:在Elasticsearch 6.0.0及更高的版本中,一个索引只能包含一个类型。GET /_cat/health?v
复制代码
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1585552862 15:21:02 elasticsearch yellow 1 1 27 27 0 0 25 0 - 51.9%
复制代码
GET /_cat/nodes?v
复制代码
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1 23 94 28 mdi * KFFjkpV
复制代码
GET /_cat/indices?v
复制代码
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open pms xlU0BjEoTrujDgeL6ENMPw 1 0 41 0 30.5kb 30.5kb
green open .kibana ljKQtJdwT9CnLrxbujdfWg 1 0 2 1 10.7kb 10.7kb
复制代码
PUT /customer
GET /_cat/indices?v
复制代码
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open customer 9uPjf94gSq-SJS6eOuJrHQ 5 1 0 0 460b 460b
green open pms xlU0BjEoTrujDgeL6ENMPw 1 0 41 0 30.5kb 30.5kb
green open .kibana ljKQtJdwT9CnLrxbujdfWg 1 0 2 1 10.7kb 10.7kb
复制代码
DELETE /customer
GET /_cat/indices?v
复制代码
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open pms xlU0BjEoTrujDgeL6ENMPw 1 0 41 0 30.5kb 30.5kb
green open .kibana ljKQtJdwT9CnLrxbujdfWg 1 0 2 1 10.7kb 10.7kb
复制代码
GET /bank/account/_mapping
复制代码
{
"bank": {
"mappings": {
"account": {
"properties": {
"account_number": {
"type": "long"
},
"address": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"age": {
"type": "long"
},
"balance": {
"type": "long"
},
"city": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"email": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"employer": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"firstname": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"gender": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"lastname": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"state": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
复制代码
PUT /customer/doc/1
{
"name": "John Doe"
}
复制代码
{
"_index": "customer",
"_type": "doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
复制代码
GET /customer/doc/1
复制代码
{
"_index": "customer",
"_type": "doc",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"name": "John Doe"
}
}
复制代码
POST /customer/doc/1/_update
{
"doc": { "name": "Jane Doe" }
}
复制代码
{
"_index": "customer",
"_type": "doc",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
复制代码
DELETE /customer/doc/1
复制代码
{
"_index": "customer",
"_type": "doc",
"_id": "1",
"_version": 3,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
复制代码
POST /customer/doc/_bulk
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
复制代码
{
"took": 45,
"errors": false,
"items": [
{
"index": {
"_index": "customer",
"_type": "doc",
"_id": "1",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 5,
"_primary_term": 1,
"status": 200
}
},
{
"index": {
"_index": "customer",
"_type": "doc",
"_id": "2",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1,
"status": 201
}
}
]
}
复制代码
查询表达式(Query DSL)是一种很是灵活又富有表现力的查询语言,Elasticsearch使用它能够以简单的JSON接口来实现丰富的搜索功能,下面的搜索操做都将使用它。github
{
"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"
}
复制代码
咱们先复制下须要导入的数据,数据地址: github.com/macrozheng/…web
而后直接使用批量操做来导入数据,注意本文全部操做都在Kibana的Dev Tools中进行;docker
POST /bank/account/_bulk
{
"index": {
"_id": "1"
}
}
{
"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"
}
......省略若干条数据
复制代码
bank
索引中已经建立了1000条文档。GET /_cat/indices?v
复制代码
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open bank HFjxDLNLRA-NATPKUQgjBw 5 1 1000 0 474.6kb 474.6kb
复制代码
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"
}
}
}
}
复制代码
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"
}
}
}
}
}
}
}
}
复制代码
www.elastic.co/guide/en/el…数据库
mall项目全套学习教程连载中,关注公众号第一时间获取。json