ElasticSearch学习 (二)Springboot集成elasticsearch和完成一些基本的 *** 作

ElasticSearch学习 (二)Springboot集成elasticsearch和完成一些基本的 *** 作,第1张

ElasticSearch学习 (二)Springboot集成elasticsearch和完成一些基本的 *** 作 引入maven依赖
    
      org.springframework.boot
      spring-boot-starter-data-elasticsearch
    
阿里快速JSON
    
      com.alibaba
      fastjson
      1.2.78
    
配置文件
public class ElasticSearchClientConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http")));
        return client;
    }

}
索引 *** 作
 @Autowired
    private RestHighLevelClient restHighLevelClient;
    // 创建索引
    @Test
    void testCreateIndex() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("w_index");
        CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(response);
    }
    // 查询索引是否存在
    @Test
    void testExistIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("w_index");
        boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }
    // 删除索引
    @Test
    void testDeleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("w_index");
        AcknowledgedResponse delete =  restHighLevelClient.indices().delete(request,RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());
    }

文档 *** 作
 // 添加文档
    @Test
    void testAdddocument() throws IOException {
        // 创建对象
        User user = new User("wang",20);
        // 创建请求
        IndexRequest request = new IndexRequest("w_index");
        // 规则 put/w_index/_doc/1
        request.id("1");
        request.timeout(Timevalue.timevalueSeconds(1));
        request.timeout("1s");
        // 将请求放入json中
        request.source(JSON.toJSONString(user), XContentType.JSON);
        // 客户端发起请求 获取响应的结果
        IndexResponse indexResponse = restHighLevelClient.index(request,RequestOptions.DEFAULT);
        System.out.println(indexResponse.toString());
        System.out.println(indexResponse.status()); // 状态CREATED
    }
    // 测试文档是否存在
    @Test
    void testExistdocument() throws IOException {
        GetRequest getRequest = new GetRequest("w_index","1");
        //不获取source的上下文
        getRequest.fetchSourceContext(new FetchSourceContext(false));
        getRequest.storedFields("_none_");
        boolean exists = restHighLevelClient.exists(getRequest,RequestOptions.DEFAULT);
        System.out.println(exists);

    }
    // 查看文件信息
    @Test
    void testGetdocument() throws IOException {
       GetRequest getRequest = new GetRequest("w_index","1");
       GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(getResponse.getSource());
        System.out.println(getResponse.getFields());
    }
    // 更新文件信息
    @Test
    void testUpdatedocument() throws IOException {
        // 创建请求
        UpdateRequest updateRequest = new UpdateRequest("w_index","1");
        // 设置失效时间
        updateRequest.timeout("1s");
        User user = new User("wan",1);
        updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
        UpdateResponse updateResponse = restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);
        System.out.println(updateResponse);
    }
    // 删除文件信息
    @Test
    void testDeletedocument() throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest("w_index","1");
        deleteRequest.timeout("1s");
        DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest,RequestOptions.DEFAULT);
        System.out.println(deleteResponse.status());
    }

    // 批量插入文件
    @Test
    void testBulkdocument() throws IOException {
        BulkRequest bulkRequest = new BulkRequest("");
        bulkRequest.timeout("10s");
        List userList = new ArrayList<>();
        userList.add(new User("1",1));
        userList.add(new User("2",2));
        userList.add(new User("3",3));
        userList.add(new User("4",4));
        userList.add(new User("5",5));
        for (int i = 0; i 					
										


					

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4684248.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-07
下一篇 2022-11-07

发表评论

登录后才能评论

评论列表(0条)

保存