先查找docker鏡像有哪些
docker search elasticsearch
下載鏡像
docker pull elasticsearch:6.5.0
根據鏡像啓動容器
docker run --name es -d -p 9200:9200 -p 9300:9300 elasticsearch:6.5.0
- 查看es的基本信息
列出所有的index
添加index
curl -X PUT ‘localhost:9200/weather’
刪除index
curl -X DELETE ‘localhost:9200/weather’
安裝中文分詞插件IK
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.0/elasticsearch-analysis-ik-6.5.0.zip
重啓es容器
docker restart es
兩種分詞模式:
ik_smart:會做最粗粒度的拆分
ik_max_word:會將文本做最細粒度的拆分
創建一箇中文分詞
curl -X PUT -H 'Content-Type:application/json' 'localhost:9200/accounts -d' '
{
"mappings": {
"person": {
"properties": {
"user": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
}
}'
向Index增加記錄:
curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/accounts/person/1' -d '
{
"user": "張三",
"title": "工程師"
}'
POST方式(POST方式不需要傳id,id隨機生成)
curl -X POST -H 'Content-Type: application/json' 'localhost:9200/accounts/person' -d '
{
"user": "李四",
"title": "工程師"
}'
注意:如果沒有先創建 Index(這個例子是accounts),直接執行上面的命令,Elastic 也不會報錯,而是直接生成指定的 Index。所以,打字的時候要小心,不要寫錯 Index 的名稱。
查看指定條目的記錄
`curl ‘localhost:9200/accounts/person/1?pretty=true’
刪除一條記錄
curl -X DELETE 'localhost:9200/accounts/person/1'
更新一條記錄
curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/accounts/person/1' -d '
{
"user" : "張三",
"title" : "軟件開發"
}'
查詢所有記錄
`curl ‘localhost:9200/accounts/person/_search?pretty=true’``
簡單查詢
curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true' -d '
{
"query" : { "match" : { "title" : "工程" }},
"from": 1, #0開始
"size": 1, #返回幾條數據
}'
OR查詢
curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true' -d '
{
"query" : { "match" : { "title" : "工程 哈哈" }}
}'
AND查詢
curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "title": "工程" } },
{ "match": { "title": "哈哈" } }
]
}
}
}'