1. 程式人生 > >ElasticSearch常用操作:索引篇

ElasticSearch常用操作:索引篇

finally name ali existing 目標 ESS 寫入 ble creat

[TOC]


0 說明

基於es 5.4和5.6,參考兩份資料,《從Lucene到Elasticsearch全文檢索實戰》和官方文檔

https://www.elastic.co/guide/en/elasticsearch/reference/5.4/indices.html(官方文檔相當精彩,不容錯過!)。

1 創建索引

PUT my_index

Note1:索引不能有大寫字母;

Note2:es默認給索引設置5個分片1個副本;

NOte3:索引分片數一經指定後不能再修改,但副本數可以通過命令隨時修改;

可以添加settings配置:

PUT my_index
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  }
}

2 更新索引副本數

PUT my_index/_settings
{
  "number_of_replicas": 2
}

3 讀寫權限設置

權限參數如下:

參數設置 說明
blocks.read_only:true 為true時,設置當前索引只允許讀不允許寫或者更新
blocks.read:true 為true時,禁止對當前索引進行讀操作
blocks.write:true 為true時,禁止對當前索引進行寫操作

比如要禁止用戶進行寫操作:

PUT my_index/_settings
{
  "blocks.write": true
}

再寫入數據時,就會返回403錯誤。

恢復寫操作:

PUT my_index/_settings
{
  "blocks.write": false
}

4 查看索引

GET my_index/_mapping

返回結果:

{
  "my_index": {
    "mappings": {
      "my_type": {
        "properties": {
          "title": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

同時查看多個索引的setting信息:

GET my_index,my_index2/_mapping

查看集群中所有索引的setting信息:

GET _all/_settings

5 刪除索引

DELETE my_index

如果刪除的索引不存在,會報索引未找到異常。

6 索引的打開與關閉

索引關閉以後就幾乎不會占用系統資源。

POST my_index/_close

關閉多個索引:

POST my_index,my_index2/_close

加上ignore_unavailable參數:

POST my_index,my_index2,my_index3/_close?ignore_unavailable=true

my_index3是不存在的,如果不加ignore_unavailable參數,則會拋出索引不存在錯誤。

關閉集群中所有索引:

POST _all/_close

以能配符方式關閉索引,關閉以test開頭的索引:

POST test*/_close

7 復制索引

POST _reindex
{
  "source":{"index":"my_index"},
  "dest":{"index":"my_index3"}
}

Note1:目標索引不會復制源索引中的配置信息,_redinx操作之前需要設置目標索引的分片數、副本數等信息,如果沒有設置,或者說原來就不存在my_index3,那麽會新創建一個索引,並且使用默認配置信息;

Note2:_reindex實際上是用來復制索引文檔的,因此如果my_index中沒有文檔,那麽是不會新創建my_index3的;

可以在source中增加type和query來限制復制的文檔:

POST _reindex
{
  "source":{
    "index":"my_index",
    "type":"my_type",
    "query":{
      "term":{"title":"elasticsearch"}
    }
  },
  "dest":{"index":"my_index3"}
}

8 收縮索引

直接參考官方文檔:https://www.elastic.co/guide/en/elasticsearch/reference/5.4/indices-shrink-index.html,非常詳細。

The shrink index API allows you to shrink an existing index into a new index with fewer primary shards. The requested number of primary shards in the target index must be a factor of the number of shards in the source index. For example an index with 8 primary shards can be shrunk into 4, 2or 1 primary shards or an index with 15 primary shards can be shrunk into 5, 3 or 1. If the number of shards in the index is a prime number it can only be shrunk into a single primary shard. Before shrinking, a (primary or replica) copy of every shard in the index must be present on the same node.

Shrinking works as follows:

  • First, it creates a new target index with the same definition as the source index, but with a smaller number of primary shards.
  • Then it hard-links segments from the source index into the target index. (If the file system doesn’t support hard-linking, then all segments are copied into the new index, which is a much more time consuming process.)
  • Finally, it recovers the target index as though it were a closed index which had just been re-opened.

收縮索引前的準備:

PUT /my_source_index/_settings
{
  "settings": {
    "index.routing.allocation.require._name": "shrink_node_name", 
    "index.blocks.write": true 
  }
}

進行索引的收縮:

POST my_source_index/_shrink/my_target_index

也可以添加其它一些配置信息:

POST my_source_index/_shrink/my_target_index
{
  "settings": {
    "index.number_of_replicas": 1,
    "index.number_of_shards": 1, 
    "index.codec": "best_compression" 
  },
  "aliases": {
    "my_search_indices": {}
  }
}

如果不太理解的話,就一定要好好閱讀上面提供的官方文檔鏈接。

9 索引別名

創建索引別名:

POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "test1",
        "alias": "alias1"
      }
    }
  ]
}

移除索引別名:

POST _aliases
{
  "actions": [
    {
      "remove": {
        "index": "test1",
        "alias": "alias1"
      }
    }
  ]
}

Note1:一個索引可以有多個別名(添加多次就可以了),一個別名也可以對應多個索引(使用多次就可以了);

Note2:在使用別名的時候需要註意,如果別名和索引是一對一的,使用別名索引或者根據ID查詢文檔是可以的,但是如果別名和索引是一對多的,使用別名會發生錯誤,因為Elasticsearch不知道把文檔寫入哪個索引中去或者從哪個索引中讀取文檔;

查看某一個索引的別名:

GET my_index3/_aliases

結果:
{
  "my_index3": {
    "aliases": {
      "alias_test": {},
      "alias_test2": {}
    }
  }
}

查看一個別名所對應的索引:

GET alias_test/_aliases

結果:
{
  "my_index3": {
    "aliases": {
      "alias_test": {},
      "alias_test2": {}
    }
  },
  "my_index2": {
    "aliases": {
      "alias_test": {}
    }
  },
  "my_index": {
    "aliases": {
      "alias_test": {}
    }
  }
}

查看集群上所有的可用別名:

GET _all/_aliases
或
GET _aliases

ElasticSearch常用操作:索引篇