您当前的位置: 首页 >  Java

java持续实践

暂无认证

  • 3浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

es 7.x JavaAPI 文档 查询 删除

java持续实践 发布时间:2022-01-16 11:56:00 ,浏览量:3

文章目录
      • 根据id 查询文档
      • 根据id文档删除

根据id 查询文档

查询user索引下id为1002文档 . 使用GetRequest

import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

/**
 * @author:
 * 创建时间:2022/1/11 21:05
 */
public class ESTest_Doc_Get {

    public static void main(String[] args) throws Exception {
        RestHighLevelClient esClient = null;
        // 创建ES客户端
        try {
            esClient = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
            );
            GetRequest request = new GetRequest();
            request.index("user").id("1002");

            GetResponse response = esClient.get(request, RequestOptions.DEFAULT);
            System.out.println("查询user索引下id为1002文档的结果: " + response.getSourceAsString());
        } finally {
            if (esClient != null) {
                // 关闭ES客户端
                esClient.close();
            }
        }
    }
}

查询结果如下: 与用http请求直接查询相同. 在这里插入图片描述

根据id文档删除

传入DeleteRequest 请求, 获取DeleteResponse

import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

/**
 * @author:
 * 创建时间:2022/1/11 21:05
 */
public class ESTest_Doc_Delete {

    public static void main(String[] args) throws Exception {
        RestHighLevelClient esClient = null;
        // 创建ES客户端
        try {
            esClient = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("127.0.0.1", 9200, HttpHost.DEFAULT_SCHEME_NAME))
            );
            DeleteRequest request = new DeleteRequest();
            request.index("user").id("1002");

            DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);
            System.out.println("删除user索引下id为1002文档的结果: " + response.toString());
        } finally {
            if (esClient != null) {
                // 关闭ES客户端
                esClient.close();
            }
        }
    }
}

结果打印如下 , 成功进行删除

删除user索引下id为1002文档的结果: DeleteResponse[index=user,type=_doc,id=1002,version=10,result=deleted,shards=ShardInfo{total=2, successful=1, failures=[]}]

发送请求也查询不到数据. 在这里插入图片描述

关注
打赏
1658054974
查看更多评论
立即登录/注册

微信扫码登录

0.2228s