elasticsearch CRUD

1、簡單的集羣管理
2、商品的CRUD操作(document CRUD操作)




----------------------------------------------------------------------------------------------------------------------------


1、簡單的集羣管理


(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、yellow、red?




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狀態。




(2)快速查看集羣中有哪些索引




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




(3)簡單的索引操作




創建索引: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




----------------------------------------------------------------------------------------------------------------------------




2、商品的CRUD操作




(1)新增商品:新增文檔,建立索引




PUT /index/type/id
{
  "json數據"
}




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




{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}




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 /ecommerce/product/1
{
    "name" : "jiaqiangban gaolujie yagao",
    "desc" :  "gaoxiao meibai",
    "price" :  30,
    "producer" :      "gaolujie producer",
    "tags": [ "meibai", "fangzhu" ]
}




{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}




{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": false
}








PUT /ecommerce/product/1
{
    "name" : "jiaqiangban gaolujie yagao"
}




替換方式有一個不好,即使必須帶上所有的field,才能去進行信息的修改




(4)修改商品:更新文檔




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




{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 8,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}




我的風格,其實有選擇的情況下,不太喜歡念ppt,或者照着文檔做,或者直接粘貼寫好的代碼,儘量是純手敲代碼




(5)刪除商品:刪除文檔




DELETE /ecommerce/product/1




{
  "found": true,
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 9,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}




{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "found": false
}


發佈了7 篇原創文章 · 獲贊 41 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章