分析器参数(Analyzer)

Analyzer 参数 #

analyzer 映射参数用于定义在索引和搜索期间应用于文本字段的文本分析过程。

相关指南(先读这些) #

代码样例 #

以下示例配置定义了一个名为 my_custom_analyzer 的自定义分词器:

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "my_stop_filter",
            "my_stemmer"
          ]
        }
      },
      "filter": {
        "my_stop_filter": {
          "type": "stop",
          "stopwords": ["the", "a", "and", "or"]
        },
        "my_stemmer": {
          "type": "stemmer",
          "language": "english"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "my_text_field": {
        "type": "text",
        "analyzer": "my_custom_analyzer",
        "search_analyzer": "standard",
        "search_quote_analyzer": "my_custom_analyzer"
      }
    }
  }
}

在此示例中,my_custom_analyzer 使用标准分词器,将所有标记转换为小写,应用自定义停用词过滤器,并应用英语词干提取器。

您可以映射一个文本字段,使其在索引和搜索操作中都使用此自定义分词器:

"mappings": {
  "properties": {
    "my_text_field": {
      "type": "text",
      "analyzer": "my_custom_analyzer"
    }
  }
}

三种 Analyzer 参数 #

Easysearch 支持在映射中指定三种不同的分析器,作用于不同阶段:

参数作用阶段说明
analyzer索引时 + 搜索时同时用于索引和搜索的默认分析器。
search_analyzer仅搜索时覆盖搜索时使用的分析器。适合索引和搜索使用不同分析策略的场景。
search_quote_analyzer短语搜索时用于 match_phrase 等短语查询。适合短语搜索不做词干提取。

示例:索引和搜索使用不同分析器 #

一个常见场景是索引时使用同义词扩展,但搜索时使用标准分析器:

PUT products
{
  "settings": {
    "analysis": {
      "analyzer": {
        "synonym_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": ["lowercase", "my_synonyms"]
        }
      },
      "filter": {
        "my_synonyms": {
          "type": "synonym",
          "synonyms": ["手机,手持电话,移动电话"]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "product_name": {
        "type": "text",
        "analyzer": "synonym_analyzer",
        "search_analyzer": "standard"
      }
    }
  }
}

常用内置分析器 #

分析器名称描述适用场景
standard按 Unicode 文本分割规则分词,转小写,去停用词(可选)大多数西语文本
simple按非字母字符分割,转小写简单文本
whitespace按空格分割,不做其他处理结构化文本
keyword不分词,整个字段值作为一个词项精确值
ik_smartIK 智能分词(中文)中文搜索(推荐)
ik_max_wordIK 最大化分词(中文)中文索引(高召回)

提示:对于中文内容,推荐使用 IK 分词器。可以使用 _analyze API 测试分析效果:

POST _analyze
{
  "analyzer": "ik_smart",
  "text": "中国人民银行"
}