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

SpringBoot集成Elasticsearch系列文章目录

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


文章目录

  • SpringBoot集成Elasticsearch系列文章目录
  • Elasticsearch文档管理
    • 0.抽取ES连接对象的公共方法
    • 1.添加文档
    • 2.更新文档
    • 3.删除文档
    • 4.根据id查询文档
    • 5.批量查询文档
  • 总结


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提供了大量能使我们快速便捷地处理数据的方法。有兴趣的可以继续深入学习。

你可能感兴趣的:(ES,elasticsearch)