1. 程式人生 > >elasticsearch官方文件學習筆記----Getting Started

elasticsearch官方文件學習筆記----Getting Started

Getting Started

基本概念

1)準實時:ES搜尋是一個接近實時的搜尋平臺。這意味著從您索引一個文件的時間到它可搜尋的時間,有一個輕微的延遲(通常是一秒)。

2)叢集:ES是一個叢集,一個叢集由一個惟一的名稱標識id,預設情況下是“elasticsearch。

3)節點:節點是一個單獨的伺服器,它是叢集的一部分,儲存您的資料,並參與叢集的索引和搜尋功能,同過叢集id加入一個確切的叢集。

4)index:索引是具有類似特徵的文件集合。類似於資料庫。

5)type : 一種用於索引的邏輯分類/分割槽的型別,允許您在同一個索引中儲存不同型別的文件,為使用者提供的一種型別,另一種用於部落格的型別。類似資料庫表。

6)document:文件是可以被索引的基本資訊單元。例如,您可以為單個客戶提供一個文件,一個單一產品的另一個文件,另一個用於單個訂單。該文件以JSON(JavaScript物件表示法)表示,這是一種無處不在的網路資料交換格式。

7)Shards&Replicas:索引會有分片存放在不同節點上,每個分片都有副本,而且分片可以在建立索引時指定分片,和副本的數量。

安裝

跳過

可以安裝elasticsearch-head通過web檢視ES的資訊。跳過

操作ES

1)檢視叢集狀態:

curl -XGET 'http://mini1:9200/_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
1475247709 17:01:49  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%

status結果可以分為三種:

綠色-一切都很好(叢集功能齊全)

黃色——所有的資料都是可用的,但是有些副本還沒有被分配(叢集是完全功能的)

紅色——有些資料由於某種原因無法使用(叢集部分功能)

2):檢視所有索引

curl -XGET 'http://mini1:9200/_cat/indices?v'

結果:

health status index                pri rep docs.count docs.deleted store.size pri.store.size 
green  open   store                  5   1          3            0     23.4kb         11.7kb 
green  open   .kibana                1   1          2            1     25.2kb         12.6kb 
green  open   gorktest1-2018.09.16   5   1        405            0      908kb          454kb 

3)建立索引:

curl -XPUT 'http://mini1:9200/store'

4)插入資料:

http://localhost:9200/<index>/<type>/[<id>] 其中index、type是必須提供的。 id是可選的,不提供es會自動生成。 index、type將資訊進行分層,利於管理。index可以理解為資料庫;type理解為資料表;id相當於資料庫表中記錄的主鍵,是唯一的。

#向store索引中新增一些書籍

curl -XPUT 'http://mini1:9200/store/books/1' -d '{
  "title": "Elasticsearch: The Definitive Guide",
  "name" : {
    "first" : "Zachary",
    "last" : "Tong"
  },
  "publish_date":"2015-02-06",
  "price":"49.99"
}'

5)查詢:

curl -XGET 'http://mini1:9200/store/books/1?pretty'

返回結果:

{
  "_index" : "store",
  "_type" : "books",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "title" : "Elasticsearch: The Definitive Guide",
    "name" : {
      "first" : "Zachary",
      "last" : "Tong"
    },
    "publish_date" : "2015-02-06",
    "price" : "49.99"
  }
}

6)刪除:

curl -XDELETE 'http://mini1:9200/store?pretty'

7)修改:

curl -XPOST 'http://mini1:9200/store/books/1/_update?pretty' -d '{
  "doc": {
     "price" : 88.88
  }
}'

8)批量操作:

除了能夠索引、更新和刪除單個文件之外,Elasticsearch還可以使用_bulk API批量執行上述任何操作。這個功能非常重要,因為它提供了一種非常有效的機制,可以在儘可能少的網路往返的情況下儘可能快地執行多個操作。

#再新增一本書
curl -XPUT 'http://mini1:9200/store/books/2' -d '{
  "title": "Elasticsearch Blueprints",
  "name" : {
    "first" : "Vineeth",
    "last" : "Mohan"
  },
  "publish_date":"2015-06-06",
  "price":"35.99"
}'

批量更新:

curl -XPOST 'http://mini1:9200/store/books/_bulk' -d '
	{"index":{"_id":"1"}}
	{"price": 10.99 }
	{"index":{"_id":"2"}}
	{"price": "111.22" }
'

ok.....Getting Started 完畢。