今天看啥  ›  专栏  ›  缘来是你ylh

ElasticSearch索引操作

缘来是你ylh  · 简书  ·  · 2019-03-31 04:06

前面我们在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"
        }
      }
    ]
  }
}

冲索引等于数据迁移,本节就不讲解了




原文地址:访问原文地址
快照地址: 访问文章快照