优化项目加入ElasticSerch

Elasticsearch可视化插件、集成 SpringBoot找、具体的 Api 测试!1、创建索引2、判断索引是否存在3、删除索引4、创建文档5、判断文档是否存在6、获取文档信息7、更新文档的信息8、删除文档记录9、特殊的,真的项目一般都会批量插入数据!10、查询优化优化到之前的博客项目中定义EsService来操作ES的增删改ES的搜索功能测试
摘要由CSDN通过智能技术生成

先了解一下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的环境!

下载地址 https://nodejs.org/dist/

  • 解压后在我们的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 = client.i
  • 30
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 44
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 44
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早上真起不来!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值