中文、英文、拼音混合搜索

发布时间 2024-01-10 09:34:31作者: 粒子先生

作为系统的使用者,希望系统提供支持中文搜索、英文搜索、中英混搜、全拼搜索、首字母搜索、中文+全拼、中文+首字母混搜等多种方式混合的高级搜索功能,提高易用性及搜索的全面度及准确度。

实现方案

  • 组合ik中文分词器与pinyin分词器,自定义支持中文、英文、拼音混合搜索的自定义分析器。
  • 调整索引结构,指定查询字段的分析器为自定义分析器。

自定义分析器DSL示例

PUT /xxxx
{
  "settings": {
    "analysis": {
      "analyzer": {
        "text_anlyzer": {      // 拼音
          "tokenizer": "ik_max_word",
          "filter": "py"
        },
        "completion_analyzer": {  // 自动补全
          "tokenizer": "keyword",
          "filter": "py"
        }
      },
      "filter": {
        "py": {
          "type": "pinyin",
          "keep_full_pinyin": false,
          "keep_joined_full_pinyin": true,
          "keep_original": true,
          "limit_first_letter_length": 16,
          "remove_duplicated_term": true,
          "none_chinese_pinyin_tokenize": false
        }
      }
    }
  },
  "mappings": {
    "properties": {
       // 自动补全字段分析器配置
      "suggestion":{
          "type": "completion",
          "analyzer": "completion_analyzer"
      }
    }
  }
}