解决——》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、操作
  • 2、现象
  • 3、原因
  • 4、解决
    • 情况1:设置所有索引的index.max_ngram_diff
    • 情况2:创建映射时,设置index.max_ngram_diff
    • 情况3:索引创建后,设置max_ngram_diff参数
  • 5、注意

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
}

解决——》The difference between max_gram and min_gram in NGram Tokenizer must be less than or equal to_第1张图片

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,导致索引大小增加并影响性能。因此,需要根据具体情况选择一个合适的值。

你可能感兴趣的:(Elasticsearch,elasticsearch,ngram,tokenizer,index,max_ngram_diff)