ES详解-索引管理详解

索引管理的引入

我们在前文中增加文档时,如下的语句会动态创建一个customerindex

bash 复制代码
PUT /customer/_doc/1
{
  "name": "John Doe"
}

然后执行下面操作可以获得索引的mapping结果,如下:

GET customer/_mapping

而这个index实际上已经自动创建了它里面的字段(name)的类型。下面是它自动创建的mapping:

json 复制代码
{
    "customer": {
        "mappings": {
                "properties": { // 表示属性
                    "name": {// 某个属性值
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    }
}

如果我们需要对这个建立索引的过程做更多的控制:比如想要确保这个索引有数量适中的主分片,并且在我们索引任何数据之前,分析器和映射已经被建立好。

那么就会引入两点:第一个禁止自动创建索引第二个是手动创建索引

  • 禁止自动创建索引

可以通过在 config/elasticsearch.yml 的每个节点下添加下面的配置:

action.auto_create_index: false

手动创建索引就是接下来文章的内容。

索引的格式

在请求体里面传入设置或类型映射,如下所示:

arduino 复制代码
PUT /my_index
{
    "settings": { ... any settings ... },
    "mappings": {
        "properties": { ... any properties ... }
    }
}
  • settings: 用来设置分片,副本等配置信息
  • mappings: 字段映射,类型等
  • properties: 由于type在后续版本中会被Deprecated, 所以无需被type嵌套

索引管理操作

创建索引

我们创建一个user 索引test-index-users,其中包含三个属性:name,age, remarks; 存储在一个分片一个副本上。

bash 复制代码
PUT /test-index-users

{
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 1
    },
    "mappings": {
        "properties": {
            "name": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "age": {
                "type": "long"
            },
            "remarks": {
                "type": "text"
            }
        }
    }
}

执行结果为

json 复制代码
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "test-index-users"
}
  • 插入测试数据 执行下面操作
bash 复制代码
POST /test-index-users/_doc

{
    "name": "my name is rock",
    "age": 28,
    "remarks": "备注信息"
}

执行后结果如下:

json 复制代码
{
    "_index": "test-index-users",
    "_type": "_doc",
    "_id": "QDTSjZABMLTXIV0XzaYI",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

返回结果的含义在上面一个章节中已经讲过,这里就不重复多了

  • 查询刚才插入数据
bash 复制代码
GET test-index-users/_search

{
    "query": {
        "bool": {
            "must": [
                {
                    "match_all": {}
                }
            ]
        }
    },
    "from": 0,
    "size": 1
}
  • 我们再测试下不匹配的数据类型(age):
bash 复制代码
POST /test-index-users/_doc

{
    "name": "my name is rock",
    "age": "28y",
    "remarks": "备注信息"
}

你可以看到无法类型不匹配的错误, 结果如下:

swift 复制代码
{
    "error": {
        "root_cause": [
            {
                "type": "mapper_parsing_exception",
                "reason": "failed to parse field [age] of type [long] in document with id 'QzTnjZABMLTXIV0X2qY2'. Preview of field's value: '28y'"
            }
        ],
        "type": "mapper_parsing_exception",
        "reason": "failed to parse field [age] of type [long] in document with id 'QzTnjZABMLTXIV0X2qY2'. Preview of field's value: '28y'",
        "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "For input string: \"28y\""
        }
    },
    "status": 400
}

修改索引

查看刚才的索引,GET _cat/indices 可以看到索引状态结果如下:

json 复制代码
{
    "health": "yellow",
    "status": "open",
    "index": "test-index-users",
    "uuid": "cBgCFh3ZR4GlRXwKHMStKA",
    "pri": "1",
    "rep": "1",
    "docs.count": "3",
    "docs.deleted": "0",
    "store.size": "8.7kb",
    "pri.store.size": "8.7kb"
}

我们注意到刚创建的索引的状态是yellow 的,因为我测试的环境是单点环境,无法创建副本,但是在上述number_of_replicas配置中设置了副本数是1; 所以在这个时候我们需要修改索引的配置。

修改副本数量为0

bash 复制代码
PUT /test-index-users/_settings

{
    "settings": {
        "number_of_replicas": 0
    }
}

返回结果如下内容:

json 复制代码
{
    "acknowledged": true
}

再次查看索引结果

json 复制代码
{
    "health": "green", // 状态已经变成绿色
    "status": "open",
    "index": "test-index-users",
    "uuid": "cBgCFh3ZR4GlRXwKHMStKA",
    "pri": "1",
    "rep": "0",
    "docs.count": "3",
    "docs.deleted": "0",
    "store.size": "8.7kb",
    "pri.store.size": "8.7kb"
}

打开/关闭索引

  • 关闭索引 一旦索引被关闭,那么这个索引只能显示元数据信息,不能够进行读写操作。
json 复制代码
POST test-index-users/_close

// 返回结果
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "indices": {
        "test-index-users": {
            "closed": true
        }
    }
}

当关闭以后,再插入数据时, 会出现异常。

  • 打开索引
bash 复制代码
POST test-index-users/_open

打开索引后,打开后又可以重新写数据了。

删除索引

最后我们将创建的test-index-users删除。

perl 复制代码
DELETE test-index-users

查看索引

接下来我们看下之前bank的索引的信息

  • mapping

查看字段映射配置关系

bash 复制代码
GET /bank/_mapping
  • settings

查看字段的设置

bash 复制代码
GET /bank/_settings
相关推荐
蜗牛沐雨1 小时前
Rust 中的 `PartialEq` 和 `Eq`:深入解析与应用
开发语言·后端·rust
Python私教1 小时前
Rust快速入门:从零到实战指南
开发语言·后端·rust
秋野酱2 小时前
基于javaweb的SpringBoot爱游旅行平台设计和实现(源码+文档+部署讲解)
java·spring boot·后端
小明.杨2 小时前
Django 中时区的理解
后端·python·django
有梦想的攻城狮2 小时前
spring中的@Async注解详解
java·后端·spring·异步·async注解
qq_12498707533 小时前
原生小程序+springboot+vue医院医患纠纷管理系统的设计与开发(程序+论文+讲解+安装+售后)
java·数据库·spring boot·后端·小程序·毕业设计
lybugproducer3 小时前
浅谈 Redis 数据类型
java·数据库·redis·后端·链表·缓存
焚 城3 小时前
.NET8关于ORM的一次思考
后端·.net
撸猫7915 小时前
HttpSession 的运行原理
前端·后端·cookie·httpsession
嘵奇6 小时前
Spring Boot中HTTP连接池的配置与优化实践
spring boot·后端·http