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

待我称王封你为后i 2024-03-22 14:47 162阅读 0赞

推荐链接:
总结——》【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分词器

  1. DELETE my_index
  2. PUT my_index
  3. {
  4. "settings": {
  5. "analysis": {
  6. "analyzer": {
  7. "my_analyzer": {
  8. "tokenizer": "my_tokenizer"
  9. }
  10. },
  11. "tokenizer": {
  12. "my_tokenizer": {
  13. "type": "ngram",
  14. "min_gram": 2,
  15. "max_gram": 4
  16. }
  17. }
  18. }
  19. }
  20. }

2、现象

  1. {
  2. "error": {
  3. "root_cause": [
  4. {
  5. "type": "illegal_argument_exception",
  6. "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."
  7. }
  8. ],
  9. "type": "illegal_argument_exception",
  10. "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."
  11. },
  12. "status": 400
  13. }

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,所以我使用了这个方式来解决

  1. # 创建映射时,设置index.max_ngram_diff
  2. PUT /my_index
  3. {
  4. "settings": {
  5. "index": {
  6. "max_ngram_diff": 10
  7. },
  8. "analysis": {
  9. "analyzer": {
  10. "my_analyzer": {
  11. "tokenizer": "my_tokenizer"
  12. }
  13. },
  14. "tokenizer": {
  15. "my_tokenizer": {
  16. "type": "ngram",
  17. "min_gram": 2,
  18. "max_gram": 4
  19. }
  20. }
  21. }
  22. },
  23. "mappings": {
  24. // 映射定义
  25. }
  26. }

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

  1. # 索引创建后,设置max_ngram_diff参数
  2. PUT /my_index/_settings
  3. {
  4. "index": {
  5. "max_ngram_diff": 10
  6. }
  7. }

5、注意

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

发表评论

表情:
评论列表 (有 0 条评论,162人围观)

还没有评论,来说两句吧...

相关阅读