#Elasticsearch# 文章列表 Elasticsearch 介绍 Elasticsearch 7:快速上手 Elasticsearch 7:关于 Index、Type、Document Elasticsearch 7:安装与启动 Elasticsearch 7:Kibana 的使用 Elasticsearch 7:下载历史版本 Elasticsearch 7:文档唯一性 Elasticsearch 7:默认端口与端口设置 Elasticsearch 7:创建和删除索引 Elasticsearch 7:自定义 mapping 和 settings Elasticsearch 7:设置索引副本数量和分片数量 Elasticsearch 7:查看所有索引 Elasticsearch 7:数据类型 Elasticsearch 7:字符串类型 keyword 、text Elasticsearch 7:数组 Elasticsearch 7:添加和更新文档 Elasticsearch 7:通过 _bulk 批量添加文档 Elasticsearch 7:使用 from 、size 进行分页查询 Elasticsearch 7:查询中使用 sort 进行排序 Elasticsearch 7:查询结果只展示部分字段 Elasticsearch 7:查询结果中展示 _version 字段 Elasticsearch 7:使用 ignore_above 限制字符串长度 Elasticsearch 7:动态映射 Elasticsearch 7:doc_values 属性 Elasticsearch 7:刷新周期 refresh_interval Elasticsearch 7:使用 _refresh 刷新索引 Elasticsearch 7:分片(shard)限制 Elasticsearch 7:使用 _cat thread_pool 查询线程池运行情况 Elasticsearch 7:事务日志 translog Elasticsearch 7:文档 _id 的长度限制 Elasticsearch 7:分片 shard Elasticsearch 7:滚动查询 Elasticsearch 7:聚合查询 Elasticsearch 7:索引模板 Elasticsearch 7:获取文档所属的 shard Elasticsearch 7:获取版本号 Elasticsearch 7:获取指定 shard 中的文档 Elasticsearch 7:获取 shard 统计信息 Elasticsearch 7:搜索实战 Elasticsearch 7:Python 客户端 Elasticsearch 7:Java TransportClient API 客户端 Elasticsearch 7:Java REST Client API 客户端 Elasticsearch:将 SQL 转换为 DSL Elasticsearch 6 快速上手 Elasticsearch 5 快速上手 Elasticsearch 5:禁止自动创建索引 Elasticsearch 5:禁止动态增加字段 Elasticsearch 产品版本支持周期 基于 Elasticsearch 的站内搜索引擎实战

Elasticsearch 7:使用 ignore_above 限制字符串长度


#Elasticsearch#


创建 mapping 时,可以为字符串(专指 keyword) 指定 ignore_above ,用来限定字符长度。超过 ignore_above 的字符会被存储,但不会被索引。

注意,是字符长度,一个英文字母是一个字符,一个汉字也是一个字符。

在动态生成的 mapping 中,keyword类型会被设置ignore_above: 256

ignore_above 可以在创建 mapping 时指定。

验证 ignore_above 效果

PUT my_index
{
  "mappings" : {
    "properties" : {
      "note" : {
        "type" : "keyword",
        "ignore_above": 4
      }
    }
  }
}

使用 _bulk 创建文档

POST _bulk
{ "index" : { "_index" : "my_index", "_id" : "1" } }
{ "note" : "一二三"}
{ "index" : { "_index" : "my_index", "_id" : "2" } }
{ "note" : "一二三四"}
{ "index" : { "_index" : "my_index", "_id" : "3" } }
{ "note" : "一二三四五"}

使用下面的指令可以查询所有数据:

GET my_index/_search

可以看到,上面创建的三个文档都存起来了。

我们用下面的查询验证 ignore_above

# 能查到数据
GET my_index/_search
{
  "query": {
    "match": {
      "note": "一二三"
    }
  }
}

# 能查到数据
GET my_index/_search
{
  "query": {
    "match": {
      "note": "一二三四"
    }
  }
}

# 不能查到数据
GET my_index/_search
{
  "query": {
    "match": {
      "note": "一二三四五"
    }
  }
}

能够修改 ignore_above 吗 ?

可以通过下面的方式改:

PUT my_index/_mappings
{
  "properties" : {
    "note" : {
      "type" : "keyword",
      "ignore_above": 2
    }
  }
}

改大改小都行,但只对新数据有效。

text 类型支持 ignore_above 吗?

不支持。

# 删除索引
DELETE my_index

# 尝试重建索引,note字段为text类型,并指定了 ignore_above,执行时会报错
PUT my_index
{
  "mappings" : {
    "properties" : {
      "note" : {
        "type" : "text",
        "ignore_above": 2
      }
    }
  }
}

# 报错结果如下
{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "Mapping definition for [note] has unsupported parameters:  [ignore_above : 2]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "Failed to parse mapping [_doc]: Mapping definition for [note] has unsupported parameters:  [ignore_above : 2]",
    "caused_by": {
      "type": "mapper_parsing_exception",
      "reason": "Mapping definition for [note] has unsupported parameters:  [ignore_above : 2]"
    }
  },
  "status": 400
}

( 本文完 )