Elasticsearch权威指南:common_grams Token Filter解析与应用

Elasticsearch权威指南:common_grams Token Filter解析与应用

elasticsearch-definitive-guide The Definitive Guide to Elasticsearch elasticsearch-definitive-guide 项目地址: https://gitcode.com/gh_mirrors/el/elasticsearch-definitive-guide

概述

在Elasticsearch的文本处理流程中,common_grams token filter是一个专门优化包含停用词(Stopwords)的短语查询效率的重要组件。本文将深入解析其工作原理、配置方法以及实际应用场景。

核心概念

什么是common_grams

common_grams token filter通过生成特殊形式的n-gram(特别是bigram)来优化包含常见词(如"the"、"and"等)的短语查询。它与shingles token filter类似,但针对停用词场景做了特殊优化。

工作原理对比

传统shingles处理"The quick and brown fox"会生成:

the_quick
quick_and
and_brown
brown_fox

common_grams会生成更智能的组合:

the, the_quick
quick, quick_and
and, and_brown
brown
fox

配置详解

双模式配置

common_grams需要区分索引时和查询时两种模式:

PUT /my_index
{
  "settings": {
    "analysis": {
      "filter": {
        "index_filter": {
          "type": "common_grams",
          "common_words": "_english_"
        },
        "search_filter": {
          "type": "common_grams",
          "common_words": "_english_",
          "query_mode": true
        }
      },
      "analyzer": {
        "index_grams": {
          "tokenizer": "standard",
          "filter": ["lowercase", "index_filter"]
        },
        "search_grams": {
          "tokenizer": "standard",
          "filter": ["lowercase", "search_filter"]
        }
      }
    }
  }
}

关键参数说明:

  • common_words: 指定常见词列表,支持预设语言(如_english_)或自定义列表
  • query_mode: 设置为true表示查询时分析器

字段映射配置

PUT /my_index/_mapping/my_type
{
  "properties": {
    "text": {
      "type": "string",
      "analyzer": "index_grams",
      "search_analyzer": "standard"
    }
  }
}

工作流程解析

索引时处理

索引阶段,common_grams会:

  1. 输出所有原始unigram(单个词)
  2. 对常见词及其相邻词生成bigram
  3. 保持位置信息一致

例如"The quick and brown fox"会被处理为:

Pos 1: the, the_quick
Pos 2: quick, quick_and
Pos 3: and, and_brown
Pos 4: brown
Pos 5: fox

查询时优化

普通查询

使用标准分析器时,普通match查询不受影响:

GET /my_index/_search
{
  "query": {
    "match": {
      "text": {
        "query": "the quick and brown fox",
        "cutoff_frequency": 0.01
      }
    }
  }
}
短语查询优化

使用search_grams分析器时,会智能转换查询:

GET /my_index/_search
{
  "query": {
    "match_phrase": {
      "text": {
        "query": "The quick and brown fox",
        "analyzer": "search_grams"
      }
    }
  }
}

转换后的查询词项:

Pos 1: the_quick
Pos 2: quick_and
Pos 3: and_brown
Pos 4: brown
Pos 5: fox

优化效果:

  1. 减少常见词单独出现的频率
  2. 大幅降低需要检查的文档数量
  3. 提升文件系统缓存效率

特殊场景优化

双词短语查询

对于常见双词短语如"The quick":

GET /my_index/_search
{
  "query": {
    "match_phrase": {
      "text": {
        "query": "The quick",
        "analyzer": "search_grams"
      }
    }
  }
}

search_grams会将其转换为单个词项the_quick,实现:

  • 将复杂短语查询简化为高效的单词查询
  • 显著提升查询性能

最佳实践建议

  1. 对包含大量常见词的文本字段使用common_grams
  2. 区分索引时和查询时分析器配置
  3. 针对实际查询模式调整常见词列表
  4. 对主要执行短语查询的字段优先考虑此优化
  5. 结合cutoff_frequency参数实现更智能的常见词处理

通过合理使用common_grams token filter,可以显著提升包含常见词的短语查询性能,特别是在处理自然语言文本时效果尤为明显。

elasticsearch-definitive-guide The Definitive Guide to Elasticsearch elasticsearch-definitive-guide 项目地址: https://gitcode.com/gh_mirrors/el/elasticsearch-definitive-guide

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

焦习娜Samantha

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值