设为首页 收藏本站
查看: 1407|回复: 0

[经验分享] ElasticSearch常用操作:索引篇

[复制链接]

尚未签到

发表于 2019-1-29 09:20:45 | 显示全部楼层 |阅读模式
  [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



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-668995-1-1.html 上篇帖子: Elasticsearch analysis & 自定义 analyzers 下篇帖子: Elasticsearch索引创建快照
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表