Elastic Search 入門

Elastic Search 入門

安裝Elastic Search#

安裝成功之後驗證

<code>curl -X GET http://localhost:9200/?pretty
{
"name" : "ZKKlE0t",
"cluster_name" : "es",
"cluster_uuid" : "sI09KISmQNaGMgbuN7S4gQ",
"version" : {
"number" : "6.6.1",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "1fd8f69",
"build_date" : "2019-02-13T17:10:04.160291Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}/<code>

分片#

分片是Elasticsearch在集群中分發數據的關鍵,分片是數據的容器,文檔都存儲在問片中,然後分片被分配到集群中的節點上, 當你的集群擴容或縮小,ES會自動在節點之間遷移分片, 使集群保持平衡

分片可以是主分片(primary shard)或者是複製分片。索引中每個文檔屬於一個單獨的主分片,複製分片是主分片的一個單獨的副本, 作為冗餘數據,防止硬件故障導致數據丟失。

當索引創建完成時, 主分片的數量就固定了,但是複製分片的數量可以隨時調整。

創建索引:

<code>curl -X PUT http://localhost:9200/blogs -H "Content-Type: application/json" -d '{ "settings": { "number_of_shards": 3, "number_of_replicas": 1 } }'

> Response
{"acknowledged":true,"shards_acknowledged":false,"index":"blogs"}/<code>

啟動多個node實例#

<code>elasticsearch -Epath.data=$ELASTIC_HOME/data/es1 -Epath.logs=$ELASTIC_HOME/logs/es1
elasticsearch -Epath.data=$ELASTIC_HOME/data/es2 -Epath.logs=$ELASTIC_HOME/logs/es2
elasticsearch -Epath.data=$ELASTIC_HOME/data/es3 -Epath.logs=$ELASTIC_HOME/logs/es3/<code>
<code>curl -X GET http://localhost:9200/_cluster/health/?pretty
{
"cluster_name" : "es",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 2,
"number_of_data_nodes" : 2,
"active_primary_shards" : 0,
"active_shards" : 0,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}/<code>

其中status為green

顏色意義green所有分片和複製分片都可用yellow所有主要分片可用,但複製分片不是都可用red不是所有主要分片可用

數據結構#

一個文檔除了信息數據之外還必須包括元數據,Elasticsearch中必須有如下元數據節點:

節點意義_index文檔存儲的地方_type文檔代表對象的類_id文檔的唯一標識

  • _index 索引類似與關係數據庫裡的數據庫, 它是存儲和索引關聯數據的地方
  • _type 在關係型數據庫中,我們將相同類的對象存儲在一個表裡,因為他們有相同的結構,在Elasticsearch中,我們使用相同類型(type)的文檔表示相同的事物,他們的數據結構是相同的
  • _id 是一個字符串,與_index和_type組合時,就可以在Elasticsearch中唯一標識一個文檔,當創建文檔時,你可以自定義_id,也可由系統幫你生成

和關係型數據庫做一個類比 Relational DB ⇒ Databases ⇒ Tables ⇒ Rows ⇒ Columns Elasticsearch ⇒ Indices ⇒ Types ⇒ Documents ⇒ Fields

基本操作#

假設我們的索引為blog,類型為post,

  • 創建索引

使用PUT方法時, 我們可以指定ID,如果希望系統自動生成, 可以使用POST

<code>**手動賦值ID**
curl -X PUT -H "Content-Type: application/json" \\
-d '{ "title": "First blog", "text": "Elastic search", "date": "2018/01/01" }' http://localhost:9200/blog/post

{
"_index" : "blog",

"_type" : "post",
"_id" : "123",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "First blog",
"text" : "Elastic search",
"date" : "2018/01/01"
}
}


**自動生成**

curl -X POST -H "Content-Type: application/json" \\
-d '{ "title": "First blog", "text": "Elastic search", "date": "2018/01/01" }' http://localhost:9200/blog/post

{
"_index" : "blog",
"_type" : "post",
"_id" : "dKg7KWkBMu7m9eCgyDUF",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "First blog",
"text" : "Elastic search",
"date" : "2018/01/01"
}
}/<code>
  • 檢索文檔

請求時追加pretty返回結果會對json進行格式化,如果響應內容中found為true表示文檔存在,如果文檔不存在同樣會返回結果,found為false

<code>curl -X GET http://localhost:9200/blog/post/123?pretty
{
"_index" : "blog",
"_type" : "post",
"_id" : "123",

"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "First blog",
"text" : "Elastic search",
"date" : "2018/01/01"
}
}

curl -X GET http://localhost:9200/blog/post/1234?pretty
{
"_index" : "blog",
"_type" : "post",
"_id" : "1234",
"found" : false
}/<code>
  • 檢索文檔中的部分字段

GET 請求會返回文檔的全部內容, 存儲在_source,如果你只想顯示部分字段,可以使用_source字段過濾

<code>curl -X GET http://localhost:9200/blog/post/123?pretty&_source=title,text

{
"_index" : "blog",
"_type" : "post",
"_id" : "123",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"text" : "Elastic search",
"title" : "First blog"
}
}/<code>
  • 檢查文檔是否存在

存在返回200,不存在返回404

<code>curl -I http://localhost:9200/blog/post/123
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 184/<code>
<code>curl -I  http://localhost:9200/blog/post/1234
HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 59/<code>
  • 更新文檔

更新文檔時,原有文檔會被標記為刪除,不可訪問,新文檔版本號會+1

<code>curl -X PUT -H "Content-Type: application/json" http://localhost:9200/blog/post/123 -d '{ "title": "Update blog" }'

{
"_index" : "blog",
"_type" : "post",
"_id" : "123",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "Update blog"
}
}/<code>

局部更新

使用update API更新文檔時,會接受一個局部文檔參數doc,它會合併到現有文檔中,對象合併在一起,已經存在的字段被覆蓋, 新字段 則追加到原文檔

<code>curl -X POST -H "Content-Type: application/json" http://localhost:9200/blog/post/123/_update -d '{ "doc": { "title": "Update partial blog", "views": 0 }}'

{

"_index" : "blog",
"_type" : "post",
"_id" : "123",
"_version" : 3,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "Update partial blog",
"views" : 0
}
}/<code>

文檔衝突#

由於Elasticsearch是分佈式結構的,每個文檔可能存儲在不同的分片上,當多個進程同時更新文檔時可能會出現寫衝突, 這時可能會出現髒數據。 類比關係型數據庫中的寫衝突解決方法

  • 悲觀鎖:假設任何情況下都會發生衝突,寫數據時先請求鎖,得到鎖之後寫數據,完成後釋放鎖
  • 樂觀鎖:假設不會造成衝突,直接去寫, 寫數據的時候進行版本比較,如果不一致,說明有其他進程修改過,拋出異常 我們可以通過版本號來解決Elasticsearch中的寫衝突問題 在提交更新的時候加入版本號,如果版本號一致則修改,不一致則跳過
<code>curl -X POST -H "Content-Type: application/json" http://localhost:9200/blog/post/123?version=1 -d '{ "title": "Update partial blog", "views": 0 }'/<code>


分享到:


相關文章: