文章目录
先了解一下ElasticSerch
ElasticSerch简介
什么是ElasticSerch
- ElasticSearch,简称es,es是一个开源的高扩展的分布式全文检索引擎,它可以近乎实施的存储、检索数据;本身扩展性很好,可以扩展到上百台服务器,处理PB级别(大数据时代)的数据。es也使用java开发并使用Lucene 作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的RESTful API来隐藏Lucene的复杂性,从而让全文搜索变得简单。
Elasticsearch安装
JDK1.8,最低要求!Elasticsearch 客户端,界面工具!
下载地址 https://www.elastic.co/cn/downloads/elasticsearch
- 下载windows版本,解压压缩包,打开,看到如下目录:
-
config配置文件:elasticsearch.yml
http.port: 9200
- bin目录下的elasticsearch.bat启动
安装可视化插件
elasticsearch-head的插件
下载地址 https://github.com/mobz/elasticsearch-head
这里我们还需要node.js的环境!
- 解压后在我们的elasticsearch-head-master目录下执行(命令行,管理员)
cnpm install #下载好后 在我们的node_modules文件下
npm run start # 启动
- 再去访问9200,可以看到有跨域问题!(端口不一致)
- 由于ES进程和客户端进程端口号不同,存在跨域问题,所以需要在ES的配置文件中配置下解决跨域问题:elasticsearch.yml
#配置跨域问题
http.cors.enabled: true #开启跨域的支持
http.cors.allow-origin: "*" #允许所有访问
重启动es,使用head工具进行连接测试:解决跨域问题!
- 初学就把索引当作数据库
集成 SpringBoot
找官方文档!
文档连接 https://www.elastic.co/guide/index.html
1、找到原生的依赖
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.12.1</version>
</dependency>
这里的版本要和es版本一致
具体的 Api 测试!
- RestHighLevelClient的配置
/**
* @author ljy
* @version 1.0.0
* @Description TODO
* @createTime 2021年12月07日 20:19:00
*/
@Configuration
public class ElasticSearchClientConfig {
// ES配置
@Bean
@Qualifier("highLevelClient")
public RestHighLevelClient restHighLevelClient() {
// RestHighLevelClient highLevelClient = new RestHighLevelClient(
// RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));
// 由于插入大量数据会导致es失效问题
RestHighLevelClient highLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
// 该方法接收一个RequestConfig.Builder对象,对该对象进行修改后然后返回。
@Override
public RequestConfig.Builder customizeRequestConfig(
RequestConfig.Builder requestConfigBuilder) {
return requestConfigBuilder.setConnectTimeout(5000 * 1000) // 连接超时(默认为1 秒)
.setSocketTimeout(6000 * 1000);// 套接字超时(默认为30秒)//更改客户端的超 时限制默认30秒现在改为100*1000分钟
}
}));// 调整最大重试超时时间(默认为30秒).setMaxRetryTimeoutMillis(60000);
return highLevelClient;
}
}
- 注入即可
@Autowired
@Qualifier("highLevelClient")
private RestHighLevelClient client;
1、创建索引
// 测试索引的创建 Request PUT liu_index
@Test
void testCreateIndex() throws IOException {
// 1、创建索引请求
CreateIndexRequest request = new CreateIndexRequest("liu_index");
// 2、客户端执行请求 IndicesClient,请求后获得响应
CreateIndexResponse createIndexResponse =
client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse); //org.elasticsearch.client.indices.CreateIndexResponse@fd9e6b0a
}
2、判断索引是否存在
//测试获取索引
@Test
void testExistIndex() throws IOException{
GetIndexRequest request = new GetIndexRequest("liu_index");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);//true
}
3、删除索引
// 测试删除索引
@Test
void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("liu_index");
//删除
AcknowledgedResponse delete = c