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

乐天笔记 · · 1158 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

创建 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
}

本文来自:乐天笔记

感谢作者:乐天笔记

查看原文:Elasticsearch 7 : 使用 ignore_above 限制字符串长度

1158 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传