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