SpringBoot集成Elasticsearch(二)——文档管理等

SpringBoot集成Elasticsearch系列文章目录

SpringBoot集成Elasticsearch(一)——索引库创建等
SpringBoot集成Elasticsearch(二)——文档管理等
SpringBoot集成Elasticsearch(三)——ElasticSearchRestTemplate类与ElasticsearchRepository类



Elasticsearch文档管理

0.抽取ES连接对象的公共方法

//原生客户端类,即ESjava客户端。
private RestHighLevelClient client;
public void init(){
   //1.1 创建一个RestHightLevelClient对象,相当于和服务端建立连接。
   client = new RestHighLevelClient(RestClient.builder(
          //没有集群的话  此处可new 一个即可。
          new HttpHost("192.168.53.112",9200)
          new HttpHost("192.168.53.113",9200),
          new HttpHost("192.168.53.114",9200),
   ));
}

1.添加文档

使用RestHightLevelClient对象。
使用client对象的index方法添加文档
创建IndexRequest对象,其中包含了索引库名称、文档id、文档的内容
{“id”:“1”,“title”:“测试文档1”,“content”:“测试文档中的内容”}

public void addDocument() throws Exception{

   String document = "{\"id\":1, \"title\":\"这是测试文章\", \"content\":\"xxxxx\"}";

   //创建IndexRequest对象,其中包含索引库名称,文档id,文档内容
   IndexRequest request = new IndexRequest()
           .index("hello1")
           .id("1")
           .source(document, XContentType.JSON);

   IndexResponse response = client.index(request, RequestOptions.DEFAULT);

   System.out.println(response.toString());

}

2.更新文档

使用client对象的update方法。
需要UpdateRequest参数:
1.更新的索引
2.更新的文档的id
3.更新的文档内容

public void updateDocument() throws Exception{

    String document = "{\"id\":1, \"title\":\"这是测试文章更细的\", \"content\":\"new update\"}";

    //创建IndexRequest对象,其中包含索引库名称,文档id,文档内容
    UpdateRequest request = new UpdateRequest()
            .index("hello1")
            .id("1")
            .doc(document, XContentType.JSON);

    UpdateResponse response = client.update(request, RequestOptions.DEFAULT);

    System.out.println(response.toString());

}

3.删除文档

使用client的delete方法
需要DeleteRequest对象,需要两个参数
1.操作的索引
2.文档的id

public void deleteDocument() throws Exception{

   //创建IndexRequest对象,其中包含索引库名称,文档id,文档内容
    DeleteRequest request = new DeleteRequest("hello1", "1");

    DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);

    System.out.println(response.toString());

}

4.根据id查询文档

使用client对象的get方法。
需要使用GetRequest对象,两个参数:
1.操作的索引
2.文档的id

public void getDocument() throws Exception{

    //创建IndexRequest对象,其中包含索引库名称,文档id,文档内容
    GetRequest request = new GetRequest("hello1", "1");

    GetResponse response = client.get(request, RequestOptions.DEFAULT);

    System.out.println(response.toString());

}

5.批量查询文档

使用client对象的bulk方法。
BulkRequest对象,使用add方法,添加要批量处理的请求。
支持的处理:IndexRequest,DeleteRequest,UpdateRequest

public void bulkDocument() throws Exception{
    //json数据
    String jsonData = "[" +
            "{\"id\":3, \"title\":\"这是测试文章1\", \"content\":\"xxxxx\", \"comment\":\"备注信息\", \"mobile\":\"13344556677\"}\n" +
            "{\"id\":4, \"title\":\"这是一篇文章2\", \"content\":\"xxxxx\", \"comment\":\"备注信息\", \"mobile\":\"13344556677\"}\n" +
            "{\"id\":5, \"title\":\"这是一篇文章3\", \"content\":\"xxxxx\", \"comment\":\"备注信息\", \"mobile\":\"13344556677\"}]";

    //转换成json格式字符串
    JSONArray jsonArray = JSONObject.parseArray(jsonData);

    //创建IndexRequest对象,其中包含索引库名称,文档id,文档内容
    BulkRequest request = new BulkRequest();

    jsonArray.stream()
            .forEach(json -> {
                IndexRequest r = new IndexRequest()
                        .index("hello1")
                        .id(((JSONObject) json).getString("id"))
                        .source(((JSONObject) json).toJSONString(), XContentType.JSON);
                request.add(r);
    });


    BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);

    System.out.println(response.toString());

}

总结

以上就是ESJava客户端对索引库文档的相关操作内容,本文仅仅简单介绍了ES的使用,而ES提供了大量能使我们快速便捷地处理数据的方法。有兴趣的可以继续深入学习。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冉木

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值