1. 程式人生 > >ElasticSearch從零到精通(一)

ElasticSearch從零到精通(一)

前言

本次記錄僅自己學習所用,只針對性的記錄。

ES版本

6.5

官網

https://www.elastic.co/

基本概念

Near Realtime(NRT)

Elasticsearch(以下簡稱“ES”或“es”)接近實時的搜尋平臺。 這意味著es在發起檢索到結果返回會有一個輕微的延遲(通常1秒。

Cluster(叢集)

略,官網介紹

Node(節點)

略,官網介紹

Index(索引)

Index和mongo中collection有點類似,是document(文件)的集合,例如:你可以將customer歸檔為一個索引,product也歸檔為一個索引,order歸檔為一個索引,在單個叢集中根據自己需要可以定義多個index。

注意: Index只能有小寫字母組成,且該名稱將用於document的index、搜尋、更新、刪除等操作。

Type(型別)

用於索引的邏輯類別/分割槽的型別,允許您在同一索引中儲存不同型別的文件,例如,一個用於user的型別,另一個用於blog的型別。注意:在6.0.0版本後只能建立一個type,檢視 移除原因

Document(文件)

document是可以被indexed的最基本單元,例如:一個customer文件,一個product文件,一個order文件,這些文件資訊均為JSON格式化資訊儲存,在{Index}/{Type}中,你可以儲存多個document,一個文件必須被宣告在一個index的對應type中,即建立document必須要有index和type。

Shards&Replicas

略,官網介紹

安裝

略,官網介紹

索引

檢視索引

GET /_cat/indices?v

結果如下

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

yellow open school CtkMJ7tZQk2XDmfOxVgXqQ 5 1 0 0 460b 460b

green open .kibana_1 w4ymzF7YQtukLFsS33Ri2Q 1 0 4 0 18.5kb 18.5kb

yellow open immiker kWTcZHE-QS6rjR1BavOVhw 5 1 10 0 67kb 67kb

在以上結果中有一欄index,可以清晰檢視到當前school、.kibana_1、immiker 3個index,其中.kibana_1為系統建立好的,關於其他欄(health、uuid等)詳細介紹見官網介紹

建立索引

PUT /{Index}?pretty

{Index}為指定索引名稱,引數pretty是將請求結果格式化json輸出,非必填。例如:我們需要建立一個order索引,執行語句如下:

PUT /order?pretty

我們用 GET /_cat/indices?v 可以檢視一下索引,得到結果如下:

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

green open .kibana_1 w4ymzF7YQtukLFsS33Ri2Q 1 0 4 0 18.5kb 18.5kb

yellow open order qoLOxVTyRsmCxJRI3K4zYg 5 1 0 0 1.1kb 1.1kb

yellow open immiker kWTcZHE-QS6rjR1BavOVhw 5 1 10 0 67kb 67kb

yellow open school CtkMJ7tZQk2XDmfOxVgXqQ 5 1 0 0 1.2kb 1.2kb

刪除索引

DELETE /{Index}

例如刪除order索引,DELETE /order即可刪除訂單索引,執行結果如下:

{
  "acknowledged" : true
}

Document

建立/替換文件

PUT /{Index}/{Type}/{Id}

建立文件需要指定Index、Type、Id(可選)及具體內容,如在school索引下建立一個type為student的Id為20110001的學生資訊,如果Index或Type不存在,將自動建立。

PUT /school/student/20110001

{
  "name": "郭亦云",
  "studentNo": 20110001,
  "age": 19,
  "from": "北京",
  "gender": 1,
  "profissional": "電腦科學與技術",
  "grade": 2011,
  "class": "計科(1)班"
}

返回結果如下:

{
  "_index" : "school",// 所在索引名稱
  "_type" : "student",// 所在type
  "_id" : "20110001",// 指定id
  "_version" : 1,// 版本號
  "result" : "created",// 建立/更新結果,第一次建立為created
  "_shards" : {
    "total" : 2,
    "successful" : 1,// 成功數
    "failed" : 0// 失敗數
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

注意:如果再次執行PUT /school/student/20110001原有資訊(所有欄位會被更新為最新documwnt)將被覆蓋更新,其返回結果:

{
  "_index" : "school",
  "_type" : "student",
  "_id" : "20110001",
  "_version" : 2,// 版本號+1
  "result" : "updated",// 由原來的created變成updated
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

如果我們不指定Id,僅僅將資料儲存到Index/Type下會結果會怎樣?我們向/school/student中儲存一個學生資訊,即POST /school/student,學生資訊如下:

{
  "name": "城一嘉",
  "studentNo": 20110002,
  "age": 19,
  "from": "上海",
  "gender": 1,
  "profissional": "電腦科學與技術",
  "grade": 2011,
  "class": "計科(1)班"
}

其會自動生成一個id(自然id),返回結果為:

{
  "_index" : "school",
  "_type" : "student",
  "_id" : "f3QWl2cBCkI2QzPFaO5N",// 自然id
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

總結:執行PUT /{Index}/{Type}/{Id} 如果document不存在,則建立,否則替換原來document,如果不指定document的id的話,在進行儲存時,會自動產生一個自然id

檢視指定id的document

GET /{Index}/{Type}/{Id}

通過GET /{Index}/{Type}/{Id}獲取指定內容,切記,無需請求體(GET還能帶請求體?自行百度),其結果如下:

{
  "_index" : "school",
  "_type" : "student",
  "_id" : "20110001",
  "_version" : 3,
  "found" : true,// 找到與否
  "_source" : {
    "name" : "郭亦云",
    "studentNo" : 20110001,
    "age" : 19,
    "from" : "北京",
    "gender" : 1,
    "profissional" : "電腦科學與技術",
    "grade" : 2011,
    "class" : "計科(1)班"
  }
}

更新document

POST {Index}/{Type}/{Id}/_update

{
	"doc"{
		...// 待更新欄位資訊
	}
}

例如,將id為20110001的學生姓名更改為 景清霽,年齡更改為20

POST /school/student/20110001/_update

{
  "doc": {
    "name":"景清霽",
    "age":20
  }
}

執行結果如下:

{
  "_index" : "school",
  "_type" : "student",
  "_id" : "20110001",
  "_version" : 4,
  "result" : "updated",// 已更新
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

另外還有一種更新方式,如:將年齡更新為25

{
  "script": "ctx._source.age=25"
}
條件更新document

通常我們會像sql一樣來更新指定一類資料中的資訊,例如將學號為20110001的學生年齡更改為20,或者將年級為2011級的學生均修改為2012級,那麼該如果操作了呢?

POST {Index}/{Type}/_update_by_query

例如將年級為2011級的學生均修改為2012級,年齡修改為20:

{
  "query":{// 條件匹配
    "match":{
      "grade":2011
    }
  },
  "script":"ctx._source.grade=2012;ctx._source.age=20"// 資訊修改,多個欄位以英文;隔開
}
刪除document

DELETE {Index}/{Type}/{Id}

例如刪除id為20110001的document

DELETE /school/student/20110001

執行結果:

{
  "_index" : "school",
  "_type" : "student",
  "_id" : "20110001",
  "_version" : 8,
  "result" : "deleted",// 刪除結果
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 7,
  "_primary_term" : 1
}

條件刪除document

POST {Index}/{Type}/_delete_by_query

例如,刪除學號為20110001的學生資訊

{
  "query": {
    "match": {
      "studentNo": 20110001
    }
  }
}

執行結果:

{
  "took" : 7,
  "timed_out" : false,
  "total" : 1,
  "deleted" : 1,
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

總結

以上只是對index、type、document作一個快速介紹,如document的基本操作增、刪、改、查,對於更復雜的操作,我們將在下一篇學習!感謝您的耐心閱讀~