淘东电商项目(47) -商品搜索服务功能的实现(集成拼音分词器)

引言

本文代码已提交至Github,有兴趣的同学可以下载来看看:https://github.com/ylw-github/taodong-shop

在上一篇博客《淘东电商项目(46) -商品搜索服务功能的实现》,实现了使用ES的技术实现商品搜索的功能,效果如下图:
在这里插入图片描述
但是只支持中文的搜索方式,在大型的购物网站,一般都支持拼音查询的,其实实现方式很简单,ES只要集成拼音分词器就可以了,本文就来讲解下。

本文目录结构:
l____引言
l____ 1. 拼音分词器的安装
l____ 2. 重新定义文档类型映射
l____ 3. 测试
l____ 4. 总结

1. 拼音分词器的下载与安装

目前使用的ES版本是5.6.12,所以拼音分词器elasticsearch-analysis-pinyin的版本必须和es一样也为5.6.12

首先下载拼音分词器,地址:https://github.com/medcl/elasticsearch-analysis-pinyin/releases
在这里插入图片描述
我的下载版本地址是:https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v5.6.12/elasticsearch-analysis-pinyin-5.6.12.zip

下在完成后,使用SSH工具上传文件到Linux服务器的user/local目录:
在这里插入图片描述
拷贝到es容器的plugins目录:

docker cp elasticsearch-analysis-pinyin-5.6.12.zip es容器id或es容器名:/usr/share/elasticsearch/plugins

进入ES容器的plugins目录:

docker exec -it es容器id或es容器名 /bin/bash

解压并更改名字:

unzip elasticsearch-analysis-pinyin-5.6.12.zip 
mv elasticsearch ik-pinyin

在这里插入图片描述
删除压缩包,退出并重启ES:

rm elasticsearch-analysis-pinyin-5.6.12.zip 
exit
docker restart es容器id或es容器名

2. 重新定义文档类型映射

如果要使ES支持拼音分词和中文分词,那么需要重新定义文档类型:

1.首先自定义分词器 ,支持拼音和中文分词:

DELETE  /product
PUT /product
{
   "settings": {
        "analysis": {
            "analyzer": {
                "ik_smart_pinyin": {
                    "type": "custom",
                    "tokenizer": "ik_smart",
                    "filter": ["my_pinyin", "word_delimiter"]
                },
                "ik_max_word_pinyin": {
                    "type": "custom",
                    "tokenizer": "ik_max_word",
                    "filter": ["my_pinyin", "word_delimiter"]
                }
            },
            "filter": {
                "my_pinyin": {
                    "type" : "pinyin",
                    "keep_separate_first_letter" : true,
                    "keep_full_pinyin" : true,
                    "keep_original" : true,
                    "limit_first_letter_length" : 16,
                    "lowercase" : true,
                    "remove_duplicated_term" : true 
                }
            }
        }
  }
  
}

2.重新指定文档类型映射拼音分词类型

POST /product/_mapping/product
{
   
  
      "product": {
        "properties": {
          "@timestamp": {
            "type": "date"
          },
          "@version": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "attribute_list": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "category_id": {
            "type": "long"
          },
          "created_time": {
            "type": "date"
          },
          "detail": {
            "type": "text",
             "analyzer":"ik_smart_pinyin",
            "search_analyzer":"ik_smart_pinyin"

          },
          "id": {
            "type": "long"
          },
          "main_image": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "name": {
            "type": "text",
            "analyzer":"ik_smart_pinyin",
            "search_analyzer":"ik_smart_pinyin"

          },
          "revision": {
            "type": "long"
          },
          "status": {
            "type": "long"
          },
          "sub_images": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "subtitle": {
            "type": "text",
          "analyzer":"ik_smart",
         "search_analyzer":"ik_smart"

          },
          "updated_time": {
            "type": "date"
          }
        }
      }
}

3.重新使用logstash导入数据:
在这里插入图片描述

3. 测试

1.启动项目Eureka,商品搜索服务
在这里插入图片描述

2.浏览器使用拼音搜索输入:http://localhost:8500/search?name=pg,可以看到能使用pg来实现搜索苹果商品:
在这里插入图片描述
本文完!

原创文章 2756 获赞 5261 访问量 70万+

猜你喜欢

转载自blog.csdn.net/qq_20042935/article/details/105729469
今日推荐