修改拼音分词器源码实现汉字/拼音/简拼混合搜索时同音字不匹配

拼音插件下载(es版本6.5.4)

https://files.cnblogs.com/files/lcy2020/elasticsearch-analysis-pinyin-6.5.4.zip

配置参考

1)配置映射

PUT /association_test/
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "pinyin_analyzer": {
            "tokenizer": "my_pinyin"
          }
        },
        "tokenizer": {
          "my_pinyin": {
            "type": "pinyin",
            "keep_first_letter": true,
            "keep_separate_first_letter": true,
            "keep_full_pinyin": true,
            "keep_original": false,
            "limit_first_letter_length": 16,
            "keep_separate_chinese":true,
            "keep_none_chinese_in_first_letter":true,
            "keep_joined_full_pinyin":true,
            "lowercase": true
          }
        }
      }
    }
  },
  "mappings": {
    "route": {
      "properties": {
        "words": {
          "type": "completion",
          "fields": {
            "w-pinyin": {
              "type": "completion",
              "analyzer": "my_pinyin",
              "search_analyzer":"standard",
              "contexts": [
                { 
                  "name": "filter",
                  "type": "category",
                  "path": "filter"
                }
              ]
            }
          },
          "contexts": [
                { 
                  "name": "filter",
                  "type": "category",
                  "path": "filter"
                }
              ]
        }
      }
    }
  }
}

2) 添加数据

PUT /association_test/route/1
{
  "words": {
    "input":"蛋黄酥Eg",
    "weight": 5
  },
  "filter": "杭州市CN"
}

PUT /association_test/route/2
{
  "words": {
    "input":"淡黄色的Egg",
    "weight": 12
  },
  "filter": "杭州市CN"
}

PUT /association_test/route/3
{
  "words": {
    "input":"刘德华",
    "weight": 3
  },
  "filter": "杭州市CN"
}

3)搜索

1、简拼

2、中文拼音混合

参考文章:
https://www.cnblogs.com/danvid/p/10691547.html
https://github.com/medcl/elasticsearch-analysis-pinyin

posted @ 2021-12-23 11:10  厉小辉  阅读(210)  评论(0)    收藏  举报