一、效果图

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文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.4.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><!-- elasticsearch启动器 (必须)--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency><!--junit单元测试--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope><version>4.12</version></dependency><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.2.3</version><classifier>jdk15</classifier></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

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 {@Idprivate 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;}@Overridepublic 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<Search,Long> {}

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 {@Autowiredprivate SearchReposity searchReposity;@RequestMapping("/search")@ResponseBodypublic String searchByPinYin(HttpServletRequest request, HttpServletResponse response){String content = request.getParameter("keywords");WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders.wildcardQuery("value", content+"*");Iterable<Search> iterable = searchReposity.search(wildcardQueryBuilder);JSONObject jsonObject = JSONObject.fromObject(iterable);return jsonObject.toString();}
}

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

  1. 学习Springboot一之Springboot+Mybatis(注解形式)+Mysql+Web简单Demo

    学习SpringBoot,通过Springboot简单配置连接Mysql数据库,采用Mybatis注解方式实现数据库"增.删.改.查",结合Spring web实现页面呈现后,. ...

  2. ElasticSearch - SpringBoot集成ES

    文章目录 ElasticSearch - SpringBoot集成ES 1.整体设计思路(仿NBA中国官网) 2.项目搭建 3.ES API的基本使用 3.1 新增球员信息 3.2 查看球员信息 3. ...

  3. springboot开篇 (一)简单邮件发送

    上篇终结篇为spring 发送邮件,这次将使用springboot 发送邮件,同时本篇将作为springboot入门篇. 新建一个工程..工程目录结构如下,此次使用idea进行开发.对于一个长期使用e ...

  4. 使用腾讯云实现录音语音转换文字简单DEMO

    使用腾讯云实现录音语音转换文字简单DEMO 感谢 对接腾讯云 新建springboot项目 创建一个接口(VoiceService) 创建controller层(VoiceController) 加入 ...

  5. EPSON机器人建立TCP/IP通讯的简单demo

    以下为我近期研究EPSON机器人通讯的经验总结,主要实现机械手接收相机发送过来的数据,从而达到对应的位置,及其简单demo. 欢迎加入知识星球[3D视觉工坊],进行交流学习.

  6. Solr配置与简单Demo[转]

    Solr配置与简单Demo 简介: solr是基于Lucene Java搜索库的企业级全文搜索引擎,目前是apache的一个项目.它的官方网址在http://lucene.apache.org/sol ...

  7. VC++ 拖放编程简单Demo

    微软的编程类库都带有拖放编程的接口:下面看一个最简单demo:win7, vc6:新建一个对话框工程: 添加一个列表框控件:设置 接受文件 属性: 在 类向导-Class Info 做如下选择: 为W ...

  8. win32汇编定时器简单Demo

    timer.asm: .386.model flat,stdcalloption casemap:noneinclude windows.inc include user32.inc includel ...

  9. win32汇编创建线程简单Demo

    代码如下:thread.asm: .386.model flat,stdcalloption casemap:noneinclude windows.inc include user32.inc in ...

最新文章

  1. 年度编程语言最佳候选人:Kotlin vs. C
  2. 5分绩点转4分_作为一名大学生,如何规划4年大学生活?学姐:建议从这5点做起...
  3. 策略模式和工厂模式的区别_java设计模式之状态模式,策略模式孪生兄弟
  4. IDEA报错:Cannot resolve plugin org.apache.maven.plugins:*
  5. 定位pure virtual method called问题
  6. 怎样删了系统升级服务器,如何优雅的搞垮服务器,再优雅的救活
  7. SQL Server外键中的DELETE CASCADE和UPDATE CASCADE
  8. 农业灌溉泵行业调研报告 - 市场现状分析与发展前景预测
  9. 消息循环,注册窗口,创建窗口【图解】
  10. Android -----paint cap join 理解 ,paint画笔形状设置
  11. QT C++ 百度智能云 人脸图像识别应用实例
  12. 免费中文Python电子书
  13. SpringMVC实现i18n和主题切换
  14. 这是一个基于Threejs的商品VR展示系统的 VR模型展示Demo
  15. 嵌入式Linux驱动笔记(五)------学习platform设备驱动
  16. IPVS -三种IP负载均衡技术与八种调度算法
  17. [最短路-Floyd][并查集]SSL P2344 刻录光盘
  18. 【计算机网络】思科实验(9):动态路由协议RIPv2
  19. 【华为OD机试真题2023 JAVA】几何平均值最大子数组
  20. 隔离放大器HCPL-7840内部工作原理

热门文章

  1. 过来人写给软件工程师的 30 条建议
  2. 农家美女DIY远距离传输设备 3公里外无线上网
  3. 支付宝小程序使用自定义组件(原生)
  4. window.open新开页时页面访问不了问题
  5. Spark3:pyspark注册udf和使用窗口函数
  6. 犬只的放牧,猎捕,性情以及可训性的基因分析
  7. Windows 2008 部署服务之客户端安装
  8. sis防屏蔽程序_手机安全防“四害”
  9. kali重启网卡,kali重启网卡命令
  10. 如何关闭react的端口号_react修改端口号