Teng's blog Teng's blog
首页
Java
H5前端
GitHub (opens new window)
首页
Java
H5前端
GitHub (opens new window)
  • 认知

  • 入门

    • 环境准备
    • RESTful&JSON
    • 客户端工具
    • 倒排索引
    • 数据格式
    • RESTful-API

      • 索引-创建
        • 使用PUT请求创建
        • 返回数据解析
        • 后台返回日志
      • 索引-查询
      • 索引-删除
      • 文档-创建
      • 文档-主键查询
      • 文档-全量查询
      • 文档-删除
      • 文档-修改
      • 映射-创建
      • 映射-查询
      • DSL-数据准备
      • DSL-全量查询
      • DSL-匹配查询
      • DSL-关键字查询
      • DSL-指定返回字段
      • DSL-组合查询
      • DSL-范围查询
      • DSL-模糊查询
      • DSL-排序
      • DSL-高亮
      • DSL-分页查询
      • DSL-聚合查询
    • Java-API

  • 环境

  • 进阶

  • 框架集成

  • 优化

  • 面试题

  • Database-Elasticsearch
  • 入门
  • RESTful-API
Shetengteng
2022-01-23

索引-创建

对比关系型数据库,创建索引就等同于创建数据库

在当前ES 7.8版本中,创建索引默认分片是1个,每个分片的副本默认是1个

# 使用PUT请求创建

在 Postman 中,向 ES 服务器发 PUT 请求 :http://127.0.0.1:9200/shopping

PUT http://localhost:9200/shopping
1

示例图

为什么使用PUT请求创建,而不是POST请求?

由于PUT请求具有幂等性,因此在创建一次,下一次再PUT提交时,不会对当前创建的结果有任何影响,如再次PUT创建时,返回错误信息如下






 












{
    "error": {
        "root_cause": [
            {
                "type": "resource_already_exists_exception",
                "reason": "index [shopping/nBExHlw1S16Y5v5Ix6QPfw] already exists",
                "index_uuid": "nBExHlw1S16Y5v5Ix6QPfw",
                "index": "shopping"
            }
        ],
        "type": "resource_already_exists_exception",
        "reason": "index [shopping/nBExHlw1S16Y5v5Ix6QPfw] already exists",
        "index_uuid": "nBExHlw1S16Y5v5Ix6QPfw",
        "index": "shopping"
    },
    "status": 400
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

而POST请求不具有幂等性,每次POST都会有新增的记录;由于索引的特殊性,创建一次后不能创建相同的名称的索引,因此不能使用POST

# 返回数据解析

{
    "acknowledged"【响应结果】: true, # true 操作成功
    "shards_acknowledged"【分片结果】: true, # 分片操作成功
    "index"【索引名称】: "shopping"
}
1
2
3
4
5

# 后台返回日志

[2022-01-23T19:46:00,909][INFO ][o.e.c.m.MetadataCreateIndexService] [LAPTOP-HB9K8SMO] [shopping] creating index, cause [api], templates [], shards [1]/[1], mappings []
1

注意:创建索引库的分片数默认 1 片,在 7.0.0 之前的 Elasticsearch 版本中,默认 5 片

Last Updated: 2022/02/05, 15:58:51
数据格式
索引-查询

← 数据格式 索引-查询→

Theme by Vdoing | Copyright © 2021-2022 Shetengteng | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式