一 環境搭建
系統環境:win10的linux子系統,Ubuntu子系統
依賴環境:java 8的環境
啟動命令:打開到解壓目錄,./bin/elasticsearch -d
遇到的問題:
1 es6無法使用root用戶:
-
創建elsearch用戶組及elsearch用戶
-
groupadd elsearch
-
adduser elsearch(useradd 會使得上下左右鍵失靈)
-
-
更改elasticsearch文件夾及內部文件的所屬用戶及組為elsearch:elsearch
-
cd /opt
-
chown -R elsearch:elsearch elasticsearch
-
-
切換到elsearch用戶再啟動
-
su elsearch cd elasticsearch/bin
-
./elasticsearch
-
2 文件夾無權限
可能是windows系統copy過去的,導致沒有讀寫權限,cd命令失效
chmod -R 0760 elasticsearch
3 后台關閉
- ps -ef | grep elastic
- kill -9 2382(進程號)
4 安裝header插件
4.1插件官網地址https://github.com/mobz/elasticsearch-head
4.2 node環境安裝
從官網下載release版本,解壓到相應目錄,運行以下命令
ln -s /opt/nodejs/bin/node /usr/bin/
ln -s /opt/nodejs/bin/npm /usr/bin/
4.1如果想查詢集群健康信息,那么需要在elasticsearch配置文件中授權
vim /opt/elasticsearch/config/elasticsearch.yml http.cors.enabled: true # elasticsearch中啟用CORS http.cors.allow-origin: "*" # 允許訪問的IP地址段,* 為所有IP都可以訪問
二 基本概念
2.1 Node 與 Cluster
Elastic 本質上是一個分布式數據庫,允許多台服務器協同工作,每台服務器可以運行多個 Elastic 實例。
單個 Elastic 實例稱為一個節點(node)。一組節點構成一個集群(cluster)。
2.2 Index
Elastic 會索引所有字段,經過處理后寫入一個反向索引(Inverted Index)。查找數據的時候,直接查找該索引。
所以,Elastic 數據管理的頂層單位就叫做 Index(索引)。它是單個數據庫的同義詞。每個 Index (即數據庫)的名字必須是小寫。
下面的命令可以查看當前節點的所有 Index。
$ curl -X GET 'http://localhost:9200/_cat/indices?v'
2.3 Document
Index 里面單條的記錄稱為 Document(文檔)。許多條 Document 構成了一個 Index。
Document 使用 JSON 格式表示,下面是一個例子。
{ "user": "張三", "title": "工程師", "desc": "數據庫管理" }
同一個 Index 里面的 Document,不要求有相同的結構(scheme),但是最好保持相同,這樣有利於提高搜索效率。
2.4 Type
Document 可以分組,比如weather
這個 Index 里面,可以按城市分組(北京和上海),也可以按氣候分組(晴天和雨天)。這種分組就叫做 Type,它是虛擬的邏輯分組,用來過濾 Document。
不同的 Type 應該有相似的結構(schema),舉例來說,id
字段不能在這個組是字符串,在另一個組是數值。這是與關系型數據庫的表的一個區別。性質完全不同的數據(比如products
和logs
)應該存成兩個 Index,而不是一個 Index 里面的兩個 Type(雖然可以做到)。
下面的命令可以列出每個 Index 所包含的 Type。
$ curl 'localhost:9200/_mapping?pretty=true'
根據規划,Elastic 6.x 版只允許每個 Index 包含一個 Type,7.x 版將會徹底移除 Type。
三 新建和刪除 Index
新建 Index,可以直接向 Elastic 服務器發出 PUT 請求。下面的例子是新建一個名叫weather
的 Index。
$ curl -X PUT 'localhost:9200/weather'
服務器返回一個 JSON 對象,里面的acknowledged
字段表示操作成功。
{ "acknowledged":true, "shards_acknowledged":true }
然后,我們發出 DELETE 請求,刪除這個 Index。
$ curl -X DELETE 'localhost:9200/weather'
四 新增或刪除index
新建 Index,可以直接向 Elastic 服務器發出 PUT 請求。下面的例子是新建一個名叫weather
的 Index。
$ curl -X PUT 'localhost:9200/weather'
服務器返回一個 JSON 對象,里面的acknowledged
字段表示操作成功。
{ "acknowledged":true, "shards_acknowledged":true }
然后,我們發出 DELETE 請求,刪除這個 Index。
$ curl -X DELETE 'localhost:9200/weather'
五 數據操作
6.X中需要指定header 添加如下命令 -H "Content-Type: application/json"
5.1 新增記錄
向指定的 /Index/Type 發送 PUT 請求,就可以在 Index 里面新增一條記錄。比如,向/accounts/person
發送請求,就可以新增一條人員記錄。
$ curl -H "Content-Type: application/json" -X PUT 'localhost:9200/accounts/person/1' -d ' { "user": "張三", "title": "工程師", "desc": "數據庫管理" }'
服務器返回的 JSON 對象,會給出 Index、Type、Id、Version 等信息。
{ "_index":"accounts", "_type":"person", "_id":"1", "_version":1, "result":"created", "_shards":{"total":2,"successful":1,"failed":0}, "created":true }
如果你仔細看,會發現請求路徑是/accounts/person/1
,最后的1
是該條記錄的 Id。它不一定是數字,任意字符串(比如abc
)都可以。
新增記錄的時候,也可以不指定 Id,這時要改成 POST 請求。
$ curl
-H "Content-Type: application/json"
-X POST 'localhost:9200/accounts/person' -d '
{ "user": "李四", "title": "工程師", "desc": "系統管理" }'
上面代碼中,向/accounts/person
發出一個 POST 請求,添加一個記錄。這時,服務器返回的 JSON 對象里面,_id
字段就是一個隨機字符串。
{ "_index":"accounts", "_type":"person", "_id":"AV3qGfrC6jMbsbXb6k1p", "_version":1, "result":"created", "_shards":{"total":2,"successful":1,"failed":0}, "created":true }
注意,如果沒有先創建 Index(這個例子是accounts
),直接執行上面的命令,Elastic 也不會報錯,而是直接生成指定的 Index。所以,打字的時候要小心,不要寫錯 Index 的名稱。
5.2 查看記錄
向/Index/Type/Id
發出 GET 請求,就可以查看這條記錄。
$ curl 'localhost:9200/accounts/person/1?pretty=true'
上面代碼請求查看/accounts/person/1
這條記錄,URL 的參數pretty=true
表示以易讀的格式返回。
返回的數據中,found
字段表示查詢成功,_source
字段返回原始記錄。
{ "_index" : "accounts", "_type" : "person", "_id" : "1", "_version" : 1, "found" : true, "_source" : { "user" : "張三", "title" : "工程師", "desc" : "數據庫管理" } }
如果 Id 不正確,就查不到數據,found
字段就是false
。
$ curl 'localhost:9200/weather/beijing/abc?pretty=true' { "_index" : "accounts", "_type" : "person", "_id" : "abc", "found" : false }
5.3 刪除記錄
刪除記錄就是發出 DELETE 請求。
$ curl -X DELETE 'localhost:9200/accounts/person/1'
這里先不要刪除這條記錄,后面還要用到。
5.4 更新記錄
更新記錄就是使用 PUT 請求,重新發送一次數據。
$ curl -X PUT 'localhost:9200/accounts/person/1' -d ' { "user" : "張三", "title" : "工程師", "desc" : "數據庫管理,軟件開發" }' { "_index":"accounts", "_type":"person", "_id":"1", "_version":2, "result":"updated", "_shards":{"total":2,"successful":1,"failed":0}, "created":false }
上面代碼中,我們將原始數據從"數據庫管理"改成"數據庫管理,軟件開發"。 返回結果里面,有幾個字段發生了變化。
"_version" : 2, "result" : "updated", "created" : false
可以看到,記錄的 Id 沒變,但是版本(version)從1
變成2
,操作類型(result)從created
變成updated
,created
字段變成false
,因為這次不是新建記錄。
六 數據查詢
6.1 返回所有記錄
使用 GET 方法,直接請求/Index/Type/_search
,就會返回所有記錄。
$ curl 'localhost:9200/accounts/person/_search' { "took":2, "timed_out":false, "_shards":{"total":5,"successful":5,"failed":0}, "hits":{ "total":2, "max_score":1.0, "hits":[ { "_index":"accounts", "_type":"person", "_id":"AV3qGfrC6jMbsbXb6k1p", "_score":1.0, "_source": { "user": "李四", "title": "工程師", "desc": "系統管理" } }, { "_index":"accounts", "_type":"person", "_id":"1", "_score":1.0, "_source": { "user" : "張三", "title" : "工程師", "desc" : "數據庫管理,軟件開發" } } ] } }
上面代碼中,返回結果的 took
字段表示該操作的耗時(單位為毫秒),timed_out
字段表示是否超時,hits
字段表示命中的記錄,里面子字段的含義如下。
total
:返回記錄數,本例是2條。max_score
:最高的匹配程度,本例是1.0
。hits
:返回的記錄組成的數組。
返回的記錄中,每條記錄都有一個_score
字段,表示匹配的程序,默認是按照這個字段降序排列。
6.2 全文搜索
Elastic 的查詢非常特別,使用自己的查詢語法,要求 GET 請求帶有數據體。
$ curl 'localhost:9200/accounts/person/_search' -d ' { "query" : { "match" : { "desc" : "軟件" }} }'
上面代碼使用 Match 查詢,指定的匹配條件是desc
字段里面包含"軟件"這個詞。返回結果如下。
{ "took":3, "timed_out":false, "_shards":{"total":5,"successful":5,"failed":0}, "hits":{ "total":1, "max_score":0.28582606, "hits":[ { "_index":"accounts", "_type":"person", "_id":"1", "_score":0.28582606, "_source": { "user" : "張三", "title" : "工程師", "desc" : "數據庫管理,軟件開發" } } ] } }
Elastic 默認一次返回10條結果,可以通過size
字段改變這個設置。
$ curl 'localhost:9200/accounts/person/_search' -d ' { "query" : { "match" : { "desc" : "管理" }}, "size": 1 }'
上面代碼指定,每次只返回一條結果。
還可以通過from
字段,指定位移。
$ curl 'localhost:9200/accounts/person/_search' -d ' { "query" : { "match" : { "desc" : "管理" }}, "from": 1, "size": 1 }'
上面代碼指定,從位置1開始(默認是從位置0開始),只返回一條結果。
6.3 邏輯運算
如果有多個搜索關鍵字, Elastic 認為它們是or
關系。
$ curl 'localhost:9200/accounts/person/_search' -d ' { "query" : { "match" : { "desc" : "軟件 系統" }} }'
上面代碼搜索的是軟件 or 系統
。
如果要執行多個關鍵詞的and
搜索,必須使用布爾查詢。
$ curl 'localhost:9200/accounts/person/_search' -d ' { "query": { "bool": { "must": [ { "match": { "desc": "軟件" } }, { "match": { "desc": "系統" } } ] } } }'
參考資料:http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html