1.安装
ES的安装对springboot的版本配置要求很高,需要根据如下的目录下载对应的版本。
查看自己项目所使用的springboot和spring的版本,对应下载文件。
下载链接地址:https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-17-3
下载完成解压,根据:https://www.ngui.cc/el/3053538.html?action=onClick
进行安装和配置中文解析词
访问是否启动成功:http://localhost:9200/
2.增加中文解析包
配置中文解析词
下载链接:https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.17.3
重启ES。
如果增加新词,需进行配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict">custom.dic</entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords"></entry>
<!--用户可以在这里配置远程扩展字典 -->
<entry key="remote_ext_dict">words_location</entry>
<!--用户可以在这里配置远程扩展停止词字典-->
<entry key="remote_ext_stopwords">words_location</entry>
</properties>
3.项目使用
1.引入依赖
<!-- 引入ES依赖 --><dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.17.3</version></dependency>
2.d.读数据 BoolQueryBuilder的should/must,对应mysql的or/and
创建索引(也就是sql里对应的表)
{"settings":{"number_of_shards":3,"number_of_replicas":2},"mappings":{"properties":{"id":{"type":"long"},"projectId":{"type":"text"},"title":{"type":"text","analyzer":"ik_smart"},"excerpt":{"type":"text","analyzer":"ik_max_word"},"creatTime": {"type": "date","store": true,"format": "yyyy-MM-dd HH:mm:ss"},"icon": {"type": "keyword","index": false},"tags": {"type": "keyword","index": false},"module": {"type": "keyword","index": false}}}}
填充内容
{"id": 6430,"projectId": "flash_qg005ewi","title": "Twitter (X) 获得添加加密货币支付所需的许可","excerpt": "PANews 8月29日消息,据Cryptotimes报道.","icon": "https://uscloudmedia.s3.us-west-2.amazonaws.com/dhunter/icon/pannews.png", "createTime": "2023-08-30 23:30:00","module": 1,"tags": ""
}
全文查询
POST /schools/_search
{"query":{"match_all":{}}
}
模糊查询
{"query":{"multi_match" : {"query": "SEC","fields": [ "title", "excerpt" ]}}
}
ES一般用于数据量特别大,检索需要迅速的项目中,比如文库,比如新闻,比如歌词库等项目中。