解决——》The difference between max_gram and min_gram in NGram Tokenizer must be less than or equal to

推荐链接:
    总结——》【Java】
    总结——》【Mysql】
    总结——》【Redis】
    总结——》【Kafka】
    总结——》【Spring】
    总结——》【SpringBoot】
    总结——》【MyBatis、MyBatis-Plus】
    总结——》【Linux】
    总结——》【MongoDB】
    总结——》【Elasticsearch】

Elasticsearch——》解决:The difference between max_gram and min_gram in NGram Tokenizer must be less than or equal to : [1] but was [2]

1、操作

创建映射时,指定ngram分词器

DELETE my_index
PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "ngram",
          "min_gram": 2,
          "max_gram": 4
        }
      }
    }
  }
}

2、现象

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "The difference between max_gram and min_gram in NGram Tokenizer must be less than or equal to: [1] but was [2]. This limit can be set by changing the [index.max_ngram_diff] index level setting."
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "The difference between max_gram and min_gram in NGram Tokenizer must be less than or equal to: [1] but was [2]. This limit can be set by changing the [index.max_ngram_diff] index level setting."
  },
  "status": 400
}

image.png

3、原因

Elasticsearch 默认情况下设置了一个名为 index.max_ngram_diff 的参数,用于限制 max_grammin_gram 之间的最大差距。
默认max_gram 和 min_gram 参数的差异必须小于或等于 [1],但当前这个差异值为 [2],超出了 Elasticsearch 允许的最大差距,导致无法创建索引。

4、解决

通过更改 index.max_ngram_diff 参数的值来允许更大的 max_gram 和 min_gram 差异值。

情况1:设置所有索引的index.max_ngram_diff

  1. 修改 elasticsearch.yml 文件中的 index.max_ngram_diff 参数的值
  2. 重新启动 Elasticsearch 进程
# 设置所有索引的 index.max_ngram_diff 参数值为 10
index.max_ngram_diff: 10

情况2:创建映射时,设置index.max_ngram_diff

我对应的是情况2,所以我使用了这个方式来解决

# 创建映射时,设置index.max_ngram_diff
PUT /my_index
{
  "settings": {
    "index": {
      "max_ngram_diff": 10
    },
    
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "ngram",
          "min_gram": 2,
          "max_gram": 4
        }
      }
    }
  },
  "mappings": {
    // 映射定义
  }
}

情况3:索引创建后,设置max_ngram_diff参数

# 索引创建后,设置max_ngram_diff参数
PUT /my_index/_settings
{
    "index": {
        "max_ngram_diff": 10
    }
}

5、注意

如果将 index.max_ngram_diff 参数设置得太大,会产生大量的 n-gram,导致索引大小增加并影响性能。因此,需要根据具体情况选择一个合适的值。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值