ElasticSearch+SpringBoot实现汉语、拼音模糊搜索简单demo 您所在的位置:网站首页 java中文分词数据库 ElasticSearch+SpringBoot实现汉语、拼音模糊搜索简单demo

ElasticSearch+SpringBoot实现汉语、拼音模糊搜索简单demo

2024-06-25 23:35| 来源: 网络整理| 查看: 265

一、效果图 1.1 拼音搜索

1.2 汉字搜索 

现在需要实现输入拼音只匹配 第一个汉字,处于第二位和后面的不匹配,如果有大佬知道请赐教。

二、代码实现  2.1、相关环境搭建

1、安装ES(版本:5.0.0)

2、安装elasticsearch-analysis-ik(注意:版本和ES统一)

3、安装elasticsearch-analysis-pinyin(注意:版本和ES统一)

4、搭建springboot项目(springboot版本:2.0.4.RELEASE)

2.2、具体代码 

1、springboot项目pom文件

4.0.0 org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE com.example demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-starter-data-elasticsearch junit junit test 4.12 net.sf.json-lib json-lib 2.2.3 jdk15 org.springframework.boot spring-boot-maven-plugin

2、 创建实体类Search

package com.example.entity; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.*; @Mapping(mappingPath = "elasticsearch_mapping_search.json")//设置mapping @Setting(settingPath = "elasticsearch_setting_search.json")//设置setting @Document(indexName = "menusearch", type = "menu", shards = 1, replicas = 0) public class Search { @Id private Long id;//id @Field(type = FieldType.Text,analyzer = "pinyin_analyzer",searchAnalyzer = "pinyin_analyzer") private String value;//菜单名称 @Field(type = FieldType.Keyword) private String url;//菜单跳转地址 @Field(type = FieldType.Keyword) private String type;//类型,是不是产品 @Field(type = FieldType.Keyword) private String content;// public Search() { } public Search(Long id, String value, String url, String type, String content) { this.id = id; this.value = value; this.url = url; this.type = type; this.content = content; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public String toString() { return "Search{" + "id=" + id + ", value='" + value + '\'' + ", url='" + url + '\'' + ", type='" + type + '\'' + ", content='" + content + '\'' + '}'; } }

3、创建实体类对应的配置文件elasticsearch_mapping_search.json(放在项目resources目录下)

{ "menu": { "properties": { "content": { "type": "keyword", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "type": { "type": "keyword", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } , "url": { "type": "keyword", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "value": { "type": "text", "analyzer": "pinyin_analyzer", "search_analyzer": "pinyin_analyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } }

4、创建实体类对应的配置文件elasticsearch_setting_search.json(放在项目resources目录下)

{ "index": { "analysis": { "analyzer": { "pinyin_analyzer": { "tokenizer": "my_pinyin" } }, "tokenizer": { "my_pinyin": { "type": "pinyin", //true:支持首字母 "keep_first_letter": true, "first_letter" : "prefix", //false:首字母搜索只有两个首字母相同才能命中,全拼能命中 //true:任何情况全拼,首字母都能命中 "keep_separate_first_letter": true, //true:支持全拼 eg: 刘德华 -> [liu,de,hua] "keep_full_pinyin": true, "keep_original": true, "keep_none_chinese": true, "keep_none_chinese_in_first_letter": true, //设置最大长度 "limit_first_letter_length": 16, "lowercase": true, "trim_whitespace": true, //重复的项将被删除,eg: 德的 -> de "remove_duplicated_term": true } } } } }

5、创建接口

package com.example.demo; import com.example.entity.Search; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; public interface SearchReposity extends ElasticsearchRepository { }

6、业务代码实现

package com.example.demo; import com.example.entity.Search; import net.sf.json.JSONObject; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.WildcardQueryBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Controller public class HelloController { @Autowired private SearchReposity searchReposity; @RequestMapping("/search") @ResponseBody public String searchByPinYin(HttpServletRequest request, HttpServletResponse response){ String content = request.getParameter("keywords"); WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders.wildcardQuery("value", content+"*"); Iterable iterable = searchReposity.search(wildcardQueryBuilder); JSONObject jsonObject = JSONObject.fromObject(iterable); return jsonObject.toString(); } }

 

 

 

 

 

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有