ES的使用示例

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一般用于数据量特别大,检索需要迅速的项目中,比如文库,比如新闻,比如歌词库等项目中。

相关推荐
keke.shengfengpolang12 分钟前
2026大专大数据技术专业男生就业岗位全攻略:从入门到进阶
大数据
heimeiyingwang33 分钟前
企业内部知识管理:AI 驱动的文档检索与知识问答
大数据
yohalaser2 小时前
硬核智测赋能 武汉曜华激光加速钙钛矿产线产业化进程
大数据·运维·人工智能
Elasticsearch3 小时前
在 Kubernetes 上的依赖管理
elasticsearch
Dr.AE4 小时前
AI+政务 行业分析报告
大数据·人工智能·政务
Howie Zphile4 小时前
# 组织增熵与全面预算管理的持续优化
java·大数据·数据库
Dr.AE5 小时前
AI+金融 行业分析报告
大数据·人工智能·金融·产品经理
Elastic 中国社区官方博客5 小时前
使用 Elasticsearch 和神经模型为复杂语言提供更好的文本分析
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索
m0_488913015 小时前
新手小白也能学会的Dify本地部署教程(超详细)
人工智能·搜索引擎·云原生·eureka·开源·大模型·产品经理
AI开发架构师5 小时前
大数据领域Eureka的服务注册中心搭建
大数据·ai·云原生·eureka