Elasticsearch 簡介及簡單CRUD操作

 

1.簡單的集羣管理

  • 快速檢查集羣的健康狀況(es提供了一套api,叫做cat api,可以查看es中各種各樣的數據)
GET /_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
1488006741 15:12:21  elasticsearch yellow          1         1      1   1    0    0        1             0                  -                 50.0%

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1488007113 15:18:33  elasticsearch green           2         2      2   1    0    0        0             0                  -                100.0%

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1488007216 15:20:16  elasticsearch yellow          1         1      1   1    0    0        1             0                  -                 50.0%
  • 集羣狀態說明
green:每個索引的primary shard和replica shard都是active狀態的
yellow:每個索引的primary shard都是active狀態的,但是部分replica shard不是active狀態,處於不可用的狀態
red:不是所有索引的primary shard都是active狀態的,部分索引有數據丟失了

yellow狀態說明:
           一臺電腦,就啓動了一個es進程,相當於就只有一個node。現在es中有一個index,就是kibana自己內置建立的index。由於默認的配置是給每個index分配5個primary shard和5個replica shard,而且primary shard和replica shard不能在同一臺機器上(爲了容錯)。現在kibana自己建立的index是1個primary shard和1個replica shard。當前就一個node,所以只有1個primary shard被分配了和啓動了,但是一個replica shard沒有第二臺機器去啓動。如果此時只要啓動第二個es進程,就會在es集羣中有2個node,然後那1個replica shard就會自動分配過去,然後cluster status就會變成green狀態

  • 查看集羣中索引
GET /_cat/indices?v

health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   .kibana rUm9n9wMRQCCrRDEhqneBg   1   1          1            0      3.1kb          3.1kb
  • 操作索引
① 創建索引:PUT /test_index?pretty

health status index      uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   test_index XmS9DTAtSkSZSwWhhGEKkQ   5   1          0            0       650b           650b
yellow open   .kibana    rUm9n9wMRQCCrRDEhqneBg   1   1          1            0      3.1kb          3.1kb

② 刪除索引:DELETE /test_index?pretty

health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   .kibana rUm9n9wMRQCCrRDEhqneBg   1   1          1            0      3.1kb          3.1kb
注意:刪除索引可以通過指定索引名稱刪除,也可以使用_all或者* 刪除全部索引,爲了防止誤刪,在es中
elasticsearch.yml 中 action.destructive_require_name 爲true,禁止使用通配符刪除,必須指定列或者別名刪除

③ 獲取索引:
GET /test_index?pretty

④ 設置索引

創建副本的時候可以通過number_of_shards 和 number_of_replicas 參數的數量來修改分片和副本的數量。默認情況下分片的數量是5,副本的數量是1 個
PUT /test_index
{
    "setting":{
        "index":{"number_of_shards":3,"number_of_replicas":2}
    }
}
或者可以簡寫爲
{
    "setting":{"number_of_shards":3,"number_of_replicas":2}
}

⑤ 打開或者關閉索引
POST /test_index/_close
POST /test_index/_open

案例:

   1. 新增商品

語法: PUT /index/type/id
PUT /ecommerce/product/1
{
    "name" : "gaolujie yagao",
    "desc" :  "gaoxiao meibai",
    "price" :  30,
    "producer" :      "gaolujie producer",
    "tags": [ "meibai", "fangzhu" ]
}

PUT /ecommerce/product/2
{
    "name" : "jiajieshi yagao",
    "desc" :  "youxiao fangzhu",
    "price" :  25,
    "producer" :      "jiajieshi producer",
    "tags": [ "fangzhu" ]
}

PUT /ecommerce/product/3
{
    "name" : "zhonghua yagao",
    "desc" :  "caoben zhiwu",
    "price" :  40,
    "producer" :      "zhonghua producer",
    "tags": [ "qingxin" ]
}

es會自動建立index和type,不需要提前創建,而且es默認會對document每個field都建立倒排索引,讓其可以被搜索

2. 查詢商品信息:檢索文檔

GET /index/type/id
GET /ecommerce/product/1
{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "name": "gaolujie yagao",
    "desc": "gaoxiao meibai",
    "price": 30,
    "producer": "gaolujie producer",
    "tags": [
      "meibai",
      "fangzhu"
    ]
  }
}

3. 修改商品:替換文檔

使用PUT 替換,這種方式更新文檔,必須帶上文檔中所有字段信息,否則,其他字段信息會丟失

PUT /ecommerce/product/1
{
    "name" : "jiaqiangban gaolujie yagao",
    "desc" :  "gaoxiao meibai",
    "price" :  30,
    "producer" :      "gaolujie producer",
    "tags": [ "meibai", "fangzhu" ]
}

修改商品:更新文檔

可以使用POST 更新文檔

POST /ecommerce/product/1/_update
{
  "doc": {
    "name": "jiaqiangban gaolujie yagao"
  }
}

4. 刪除商品:刪除文檔

DELETE /ecommerce/product/1

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章