栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

ES集成SpringBoot

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ES集成SpringBoot

引入依赖

  ElasticSearch就一个核心依赖,这里引入阿里的fastjson是因为ES只接受json类型的数据,不接受对象。


    org.springframework.boot
    spring-boot-starter-data-elasticsearch



  com.alibaba
  fastjson
  1.2.33

集成ES并使用 文章使用的连接对象

  ES Client的官方文档有两套,一套低版本,一套高版本。两个版本使用的方式不同。以下使用的高版本

连接ES代码
@Configuration
public class ElasticSearchClientConfig {

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));
        return client;
    }

}
索引测试代码
@SpringBootTest
public class ESIndexTest {

    @Autowired
    @Qualifier("restHighLevelClient")//配置类中的方法名
    private RestHighLevelClient client;

    
    @Test
    public void testCreateIndex() throws IOException {
        // 创建索引请求
        CreateIndexRequest request = new CreateIndexRequest("index_test1");

        // 客户端执行请求
        IndicesClient indices = client.indices();
        CreateIndexResponse response = indices.create(request, RequestOptions.DEFAULT);
        System.out.println(response);
    }

    
    @Test
    public void testExistIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("index_test1");
        IndicesClient indices = client.indices();
        boolean exists = indices.exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    
    @Test
    public void testDeleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("index_test1");
        IndicesClient indices = client.indices();
        AcknowledgedResponse response = indices.delete(request, RequestOptions.DEFAULT);
        System.out.println(response.isAcknowledged());
    }
}
文档测试代码
@SpringBootTest
public class ESdocumentTest {
    @Autowired
//    @Qualifier("restHighLevelClient")  变量名跟方法名一致时即可不指定
    private RestHighLevelClient restHighLevelClient;

    
    @Test
    public void testAdddocument() throws IOException {
        // 构造插入ES的对象
        User user = new User("evader", 21);
        // 创建请求
        IndexRequest request = new IndexRequest("document_test");
        // 设置请求参数
        request.id("1");
        request.timeout(Timevalue.timevalueSeconds(1));
        // 将要插入的对象放入请求
        request.source(JSON.toJSONString(user), XContentType.JSON);
        // 发送请求并获取响应结果
        IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);

        System.out.println(response.toString());
        System.out.println(response.status());
    }

    
    @Test
    public void testExistsDocu() throws IOException {
        GetRequest request = new GetRequest("document_test", "1");
        // 设置不返回_source
        request.fetchSourceContext(new FetchSourceContext(false));
        boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    
    @Test
    public void testGetDocu() throws IOException {
        GetRequest request = new GetRequest("document_test", "1");

        GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
        System.out.println(response);
        System.out.println(response.getVersion());
        System.out.println(response.getSourceAsString());
    }

    
    @Test
    public void testUpdateDocu() throws IOException {
        UpdateRequest request = new UpdateRequest("document_test", "1");
        request.timeout("1s");

        User user = new User("evader1997", 24);
        request.doc(JSON.toJSONString(user), XContentType.JSON);

        UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
        System.out.println(response.status());
    }

    
    @Test
    public void testDeleteDocu() throws IOException {
        DeleteRequest request = new DeleteRequest("document_test", "1");
        request.timeout("1s");

        DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        System.out.println(response.status());
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/531810.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号