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();
                }
            }
        }

你可能感兴趣的:(Java,Elasticsearch,java,elasticsearch,文档获取,文档删除)