Elasticsearch学习笔记4:Springboot整合ES

一、编写ES配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//Spring两步骤:
//1、找对象
//2、放到spring中待用
@Configuration
public class ElasticSearchClientConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(
                new HttpHost(hostname: "127.0.0.1", port:9200, scheme: "http"))) ;
        return client;
    }
}

二、关于索引的操作

1.创建索引

//面向对象来操作
@Autowired
@Qualifier ("restHighLevelClient")
private RestHighLevelClient client;
//测试索引的创建    Request PUT testdb
@Test
void testCreateIndex () throws IOException {
    //1、创建索引请求
    CreateIndexRequest request =new CreateIndexRequest("testdb") ;
    //2、客户端执行请求 IndicesCLient,请求后获得响应
    CreateIndexResponse createIndexResponse =
            client.indices().create (request,RequestOptions.DEFAULT);
    System.out.println (createIndexResponse) ;
}

2.获取索引

// 测试获取索引,判断其是否存在
@Test
void testExistIndex() throws IOException{
    GetIndexRequest request = new GetIndexRequest (indices: "testdb") ;
    boolean exists = client.indices(

你可能感兴趣的:(Elasticsearch,springboot,Elasticsearch,索引,文档)