Elasticsearch 学习笔记

Elasticsearch 可以用于快速地储存、搜索和分析海量数据。这里会将其简称为 ES。大部分DSL在 Kibana Dev Tools 中执行。

👉文章列表
Elasticsearch 7 学习笔记 ⏬ 快速上手 Index、Type、Document 安装与启动 Kibana 的使用 下载历史版本 文档唯一性 默认端口与端口设置 创建和删除索引 自定义 mapping 和 settings 设置索引副本数量和分片数量 查看所有索引 数据类型 字符串类型 keyword 、text 数组 添加和更新文档 通过 _bulk 批量添加文档 使用 from 、size 进行分页查询 查询中使用 sort 进行排序 查询结果只展示部分字段 查询结果中展示 _version 字段 使用 ignore_above 限制字符串长度 动态映射 精确搜索/全文搜索示例
客户端 ⏬ Python 客户端 Java TransportClient API 客户端 Java REST Client API 客户端
其他 ⏬ ES 6 快速上手 ES 5 快速上手

Elasticsearch 7 : 文档 _id 的长度限制


结论

不能超过 512 字节。

测试

建立索引

PUT student
{
  "mappings" : {
    "properties" : {
      "name" : {
        "type" : "keyword"
      }
    }
  },
  "settings" : {
    "index" : {
      "number_of_shards" : 1
    }
  }
}

插入简单数据测试

POST student/_doc/1
{
  "name": "1111"
}

成功插入。

POST student/_doc/0123456789
{
  "name": "2222"
}

成功插入。

插入 _id 长度为 230 字节的数据

POST student/_doc/0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789
{
  "name": "3333"
}

成功。

插入 _id 长度为 648 字节的数据(会失败)

POST student/_doc/0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789
{
  "name": "4444"
}

报错如下:

{
  "error": {
    "root_cause": [
      {
        "type": "action_request_validation_exception",
        "reason": "Validation Failed: 1: id is too long, must be no longer than 512 bytes but was: 648;"
      }
    ],
    "type": "action_request_validation_exception",
    "reason": "Validation Failed: 1: id is too long, must be no longer than 512 bytes but was: 648;"
  },
  "status": 400
}

插入 _id 是 270 个中文字符的数据

大部分中文字符,在 UTF-8 中用3个字节存储。270个中文,510 字节。

POST student/_doc/零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九
{
  "name": "5555"
}

成功。

插入 _id 是 271 个中文字符的数据(会失败)

271 个中文,对应 513 个字节。

POST student/_doc/零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零
{
  "name": "5555"
}

报错信息如下:

{
  "error": {
    "root_cause": [
      {
        "type": "action_request_validation_exception",
        "reason": "Validation Failed: 1: id is too long, must be no longer than 512 bytes but was: 513;"
      }
    ],
    "type": "action_request_validation_exception",
    "reason": "Validation Failed: 1: id is too long, must be no longer than 512 bytes but was: 513;"
  },
  "status": 400
}

( 本文完 )

文章目录