RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
ElasticSearch常用操作:索引篇

[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常用操作:索引篇
网址分享:http://scyingshan.cn/article/gjdgcs.html