如何手动设计自定义 Analyzer 以支持中英文混合短语查询

解读

国内业务场景(如电商商品标题、UGC 内容、客服聊天记录)普遍出现“iPhone15 手机壳 防摔”“Nike 运动鞋 男款 42 码”这类中英数字混排短语。默认的 standard analyzer 会把中文逐字切分、把英文切为词根,导致查询时“手机壳”被拆成“手”“机”“壳”,而“iPhone15”被拆成“iPhone”“15”,最终短语查询无法命中或召回过宽。面试官希望候选人能现场拆解需求、手写 DSL,并解释每一步对中文、英文、数字、符号的处理策略,体现对 倒排索引、token filter 顺序、position 增量 的深度理解。

知识点

  1. character filter:先脱敏、去 HTML、统一全半角、大小写。
  2. tokenizer:
    • ik_max_word 或 jieba 做中文细粒度切词,保证“手机壳”整体产出;
    • 对英文/数字部分需保留原词,避免被 ik 误拆。
  3. token filter:
    • lowercase 统一大小写;
    • stop 可关或自定义停用词,防止“的”“the”干扰短语;
    • synonym_graph 支持同义词“手机壳⇔保护套”;
    • trim 去首尾空格;
    • unique 去重;
    • shingle 或 match_phrase_prefix 参数 slop 控制短语窗口。
  4. position 增量:确保中英文 token 在链式 filter 后 position 连续,match_phrase 才不会被截断。
  5. multi-field 策略:同一字段用 “mixed_ik”(细粒度最大化召回)与 “mixed_ik_smart”(粗粒度保证精度)双 analyzer,查询时用 dis_max 组合。
  6. 索引与查询分离:索引阶段用最大化切词保证召回,查询阶段用 search_analyzer 指定 ik_smart 降低无意义组合。
  7. 数字与型号保护:在 ik 的 ext_dict 里加入“iPhone15”“RTX4090”等热词,防止被拆成“iPhone”“15”。
  8. 测试工具:_analyze API 必须现场演示,验证“iPhone15 手机壳”输出 token 顺序、position、start_offset 是否满足 phrase query。

答案

  1. 创建自定义 analyzer 的完整 DSL(7.x+ 集群,单节点演示):
PUT /mixed_index
{
  "settings": {
    "analysis": {
      "char_filter": {
        "ts_char": {
          "type": "mapping",
          "mappings": [
            "0 => 0", "1 => 1", "( => (", ") => )",
            "Iphone => iPhone", "IPHONE => iPhone"
          ]
        }
      },
      "tokenizer": {
        "ik_max_custom": {
          "type": "ik_max_word",
          "enable_lowercase": false,
          "enable_stop": false,
          "user_dict": "analysis/ik/custom/mydict.dic"
        }
      },
      "filter": {
        "my_stop": {
          "type": "stop",
          "stopwords": ["的", "了", "the", "a"]
        },
        "my_synonym": {
          "type": "synonym_graph",
          "synonyms": ["手机壳,保护套", "运动鞋,球鞋"]
        },
        "my_shingle": {
          "type": "shingle",
          "max_shingle_size": 3,
          "min_shingle_size": 2,
          "output_unigrams": true
        }
      },
      "analyzer": {
        "mixed_ik": {
          "type": "custom",
          "char_filter": ["html_strip", "ts_char"],
          "tokenizer": "ik_max_custom",
          "filter": ["my_stop", "lowercase", "my_synonym", "trim", "unique"]
        },
        "mixed_ik_smart": {
          "type": "custom",
          "char_filter": ["html_strip", "ts_char"],
          "tokenizer": "ik_smart",
          "filter": ["my_stop", "lowercase", "trim"]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "mixed_ik",
        "search_analyzer": "mixed_ik_smart",
        "fields": {
          "phrase": {
            "type": "text",
            "analyzer": "mixed_ik",
            "index_options": "offsets"
          }
        }
      }
    }
  }
}
  1. 验证 analyzer:
GET /mixed_index/_analyze
{
  "analyzer": "mixed_ik",
  "text": "iPhone15 手机壳 防摔 透明"
}

期望返回 token 链:
iPhone15(position 0)
手机壳(position 1)
防摔(position 2)
透明(position 3)
且 positionIncrementGap=0,保证 phrase query “iPhone15 手机壳”能连续命中。

  1. 查询示例:
GET /mixed_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "title": {
              "query": "iPhone15 手机壳",
              "slop": 0
            }
          }
        },
        {
          "match": {
            "title.phrase": {
              "query": "iPhone15 手机壳",
              "boost": 2
            }
          }
        }
      ]
    }
  }
}
  1. 线上调优要点
  • 热词每周从搜索日志导出 TOP 1 万未召回词,自动写回 mydict.dic 并 reload analyzer。
  • 对型号类数字串,加 pattern_replace filter:正则 ([A-Za-z]+)(\d+) => $1$2,确保“iPhone15”整体 token。
  • 高并发场景开启 eager_global_ordinals 减少聚合延迟。
  • 若出现 synonym_graph 导致的 phrase 不匹配,查询层改用 match_phrase_prefix 并限制 slop=1。

拓展思考

  1. 如果业务还要求 拼音搜索“shoujike”能召回“手机壳”,你如何在同一个 analyzer 链里无损叠加 pinyin filter?请考虑 token 类型标签(<ALPHANUM> vs <CN>) 与 position 重叠 问题。
  2. 当索引增量达到 千亿级文档,热词词典动态更新会造成 segment 重建开销,你会采用 search-time synonym 方案还是 percolate 机制?权衡点在哪里?
  3. 面对 Elasticsearch 8.x 默认开启 security 且 IK 插件需要离线安装 的私有化部署场景,你如何编写 ansible playbook 实现无外网一键安装并校验 analyzer?
  4. 如果查询语句里出现 中英文 wildcard“iPhone手机”,在 match_phrase 无法支持通配符的情况下,你会用 query_string 还是 wildcard field,抑或 ngram 前置索引?请给出性能压测思路与 集群 CPU 飙高 时的降级预案。