前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >elasticSearch学习(七)

elasticSearch学习(七)

作者头像
崔笑颜
发布2020-08-19 02:14:27
3890
发布2020-08-19 02:14:27
举报
配置文件
代码语言:javascript
复制
package com.cxy.es.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: elasticSearch
 * @description: 配置类
 * @author: cuixy
 * @create: 2020-07-15 17:50
 **/
@Configuration
public class ElasticSearchClientConfig {

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        //ES集群的相关信息,如果有多个就配置多个
                        new HttpHost("192.168.106.129", 9200, "http")
                )
        );
        return client;
    }


}

索引测试类

代码语言:javascript
复制
package com.cxy.es;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

/**
 * @program: elasticSearch
 * @description: 索引测试类
 * @author: cuixy
 * @create: 2020-07-15 17:59
 **/
@SpringBootTest
public class esIndexTest {

    @Autowired
    RestHighLevelClient restHighLevelClient;

    //测试索引的创建
    @Test
    public void createIndex() throws IOException {
        //创建索引请求
        CreateIndexRequest java_index = new CreateIndexRequest("cxy1_index");
        //客户端执行请求创建索引
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(java_index, RequestOptions.DEFAULT);
    }

    //判断索引是否存在
    @Test
    void existIndex() throws IOException {
        GetIndexRequest cxy1_index = new GetIndexRequest("cxy1_index");
        System.out.println(restHighLevelClient.indices().exists(cxy1_index, RequestOptions.DEFAULT));
    }

    //删除索引
    @Test
    void deleteIndex() throws IOException {
        DeleteIndexRequest java_index = new DeleteIndexRequest("cxy1_index");
        AcknowledgedResponse delete = restHighLevelClient.indices().delete(java_index, RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());
    }


}

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-08-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 配置文件
  • 索引测试类
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档