ES的使用示例

1.安装

ES的安装对springboot的版本配置要求很高,需要根据如下的目录下载对应的版本。
es和springboot的版本匹配
查看自己项目所使用的springboot和spring的版本,对应下载文件。
项目spring版本
下载链接地址:https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-17-3

下载完成解压,根据:https://www.ngui.cc/el/3053538.html?action=onClick
进行安装和配置中文解析词
es安装
访问是否启动成功: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": ""
}

postman插入数据示例

全文查询

POST /schools/_search

{
   "query":{
      "match_all":{}
   }
}

模糊查询

{
   "query":{
      "multi_match" : {
         "query": "SEC",
         "fields": [ "title", "excerpt" ]
      }
   }
}

模糊查询
ES一般用于数据量特别大,检索需要迅速的项目中,比如文库,比如新闻,比如歌词库等项目中。

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值