ElasticSearch索引操作

前面我們在ES的基本概念一文中講到了索引,ES中索引對應的就是關係型數據庫中的.本小節我們來講一下ES的有關索引的基本操作。以後的文章中我會用到了kibana工具,沒有安裝的可以按我的另一篇文章,裏面有介紹怎麼 安裝,安裝完成後瀏覽器訪問 localhost:5601

左側側邊欄找到Dev Tools,如下圖

![img](file:///Users/shengyulong/Material/%E7%AE%80%E4%B9%A6%E6%96%87%E6%A1%A3/materials/kibana.png?lastModify=1553739193)

一:查看所有的索引

GET /_cat/indices?v

es返回

health status index     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana_1 B1GfSkleTF6cj5R4-c41rg   1   0          2            0      9.5kb          9.5kb
yellow open   megacorp  zdXsIWZtRMubzHXsiGq4Cw   5   1          5            0     28.2kb         28.2kb

結果顯示裏面已經有2個索引了分別是.kibana_1, megacorp這個是我之前的測試索引

二:創建索引

語法

PUT /{IndexName}?pretty

創建用戶索引,在kibana的Console中輸入命令

PUT /user?pretty

ES返回

#! Deprecation: the default number of shards will change from [5] to [1] in 7.0.0; if you wish to continue using the default of [5] shards, you must manage this on the create index request or with an index template
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "user"
}

翻譯:棄用:默認的分片數將在7.0.0中從[5]更改爲[1]; 如果您希望繼續使用默認的[5]分片,則必須在創建索引請求或索引模板上進行管理。

我這裏使用的是ES6.6已經是相對較新的版本了,這個問題我們暫時忽略,此時你可以使用GET /_cat/indices?v再次查看索引看看和之前有什麼區別

三:刪除索引

DELETE /{IndexName}?pretty

四:重索引和別名

ES不能重命名索引名稱,但是可以給索引取別名和重索引(reindex)

取別名語法

PUT /{OldIndexName}/_alias/{NewIndexName}

測試

1.往user索引插入文檔

PUT /user/introduce/1?pretty
{
   "name" : "jack",
   "age" : 20,
   "gender" : "male"
}

2.取別名

PUT /user/_alias/users

3.查看索引數據

查users索引:GET /users/_search
查user索引:GET /user/_search

2次返回的結果都是一樣的

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "introduce",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "jack",
          "age" : 20,
          "gender" : "male"
        }
      }
    ]
  }
}

衝索引等於數據遷移,本節就不講解了

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