Elasticsearch学习(十)————Java获取、删除文档以及判断文档是否存在( Get、Delete API)

Get、Delete API————(获取、删除文档)

1.判断文档是否存在
    	/**
         * @param : client
         * @description : 判断文档是否存在
         */
        private static boolean isExist(RestHighLevelClient client) throws IOException{
    
            GetRequest request = new GetRequest("my_index", "my_type", "1");
            //1.同步判断
            boolean exists = client.exists(request, RequestOptions.DEFAULT);
    
            //2.异步判断
            ActionListener<Boolean> listener = new ActionListener<Boolean>() {
                @Override
                public void onResponse(Boolean exists) {
                   if (exists){
                       System.out.println("文档存在");
                   }else {
                       System.out.println("文档不存在");
                   }
                }
    
                @Override
                public void onFailure(Exception e) {
    
                }
            };
            //client.existsAsync(request, RequestOptions.DEFAULT, listener);
    
            return exists;
        }
2.获取文档
    	/**
         * @param : client
         * @description : 获取文档
         */
        private static void getRequestData(RestHighLevelClient client) throws IOException {
            GetRequest request = new GetRequest("my_index", "my_type", "1");
            /*//includes:包含字段
            String[] includes = new String[]{"first_name", "first_name",};
            //excludes:包含字段
            String[] excludes = Strings.EMPTY_ARRAY;
            FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
            request.fetchSourceContext(fetchSourceContext);*/
    
             //同步获取
            GetResponse response = client.get(request, RequestOptions.DEFAULT);
    
            //监听器
            ActionListener<GetResponse> listener = new ActionListener<GetResponse>() {
                @Override
                public void onResponse(GetResponse getResponse) {
                    System.out.println("获取成功");
                }
    
                @Override
                public void onFailure(Exception e) {
                    System.out.println("获取失败");
                }
            };
    
            //异步获取
            //client.getAsync(request, RequestOptions.DEFAULT, listener);
    
            Map<String, Object> source = response.getSource();
            String sourceAsString = response.getSourceAsString();
            Map<String, Object> sourceAsMap = response.getSourceAsMap();
            byte[] sourceAsBytes = response.getSourceAsBytes();
            BytesReference sourceInternal = response.getSourceInternal();
    
            System.out.println(request);
            System.out.println(response);
    
            System.out.println(source);
            System.out.println(sourceAsString);
            System.out.println(sourceAsMap);
            System.out.println(sourceAsBytes);
            System.out.println(sourceInternal);
        }
3.删除文档
   		 /**
         * @param : client
         * @description : 删除文档
         */
        private static void deleteDocument(RestHighLevelClient client) throws IOException{
    
            DeleteRequest request = new DeleteRequest("my_index", "my_type", "6");
    
            //设置请求超时时间:2分钟
            request.timeout(TimeValue.timeValueMinutes(2));
            //request.timeout("2m");
    
            //同步删除
            DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
    
            //异步删除
            ActionListener<DeleteResponse> listener = new ActionListener<DeleteResponse>() {
                @Override
                public void onResponse(DeleteResponse deleteResponse) {
                    System.out.println("删除后操作");
                }
    
                @Override
                public void onFailure(Exception e) {
                    System.out.println("删除失败");
                }
            };
            //client.deleteAsync(request, RequestOptions.DEFAULT, listener);
    
            //返回信息
            System.out.println(deleteResponse.toString());
            String index = deleteResponse.getIndex();
            String type = deleteResponse.getType();
            String id = deleteResponse.getId();
            long version = deleteResponse.getVersion();
            ReplicationResponse.ShardInfo shardInfo = deleteResponse.getShardInfo();
            if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
    
            }
            if (shardInfo.getFailed() > 0) {
                for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
                    String reason = failure.reason();
                }
            }
        }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值