SpringBoot 整合 Elasticsearch8(最新API——Java API Client for Elasticsearch)—— 2、文档操作

1,575 阅读3分钟

SpringBoot 整合 Elasticsearch8.0(最新API——Java API Client for Elasticsearch)—— 2、文档操作

文档操作

1、添加文档

@SpringBootTest
class ElasticsearchStudyApplicationTests {

    private RestClient restClient;
    private ElasticsearchClient elasticsearchClient;
    private ElasticsearchTransport transport;
    @BeforeEach
    void ElasticsearchClientBuild(){
        // Create the low-level client
        restClient = RestClient.builder(
                new HttpHost("127.0.0.1", 9200)).build();

        // Create the transport with a Jackson mapper
        transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        // And create the API client
        elasticsearchClient = new ElasticsearchClient(transport);
    }

    @Test
    void addOneDocument() throws IOException {
        GoodSpuElasticsearchModel goodSpuElasticsearchModel = new GoodSpuElasticsearchModel(1L, "JavaEE企业级开发", 65, "JavaEE企业级开发.png" , 1, "新华出版社", 30, "书籍");
        IndexResponse indexResponse = elasticsearchClient.index(a ->
                a.index("good_spu").id(String.valueOf(goodSpuElasticsearchModel.getGoodSpuId())).document(goodSpuElasticsearchModel));
        System.out.println("result = " + indexResponse.result().jsonValue());
    }

    @AfterEach
    void ElasticsearchClientDestroy () throws IOException {
        transport.close();
        restClient.close();
    }
}

运行结果:

result = created

使用 Kibana 查看文档

GET /good_spu/_doc/1
{
  "_index" : "good_spu",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "goodSpuId" : 1,
    "goodSpuName" : "JavaEE企业级开发",
    "goodSpuPrice" : 65,
    "goodSpuPicture" : "JavaEE企业级开发.png",
    "goodBrandId" : 1,
    "goodBrandName" : "新华出版社",
    "goodCategoryId" : 30,
    "goodCategoryName" : "书籍"
  }
}

2、查询文档

@SpringBootTest
class ElasticsearchStudyApplicationTests {

    private RestClient restClient;
    private ElasticsearchClient elasticsearchClient;
    private ElasticsearchTransport transport;
    @BeforeEach
    void ElasticsearchClientBuild(){
        // Create the low-level client
        restClient = RestClient.builder(
                new HttpHost("127.0.0.1", 9200)).build();

        // Create the transport with a Jackson mapper
        transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        // And create the API client
        elasticsearchClient = new ElasticsearchClient(transport);
    }

    @Test
    void getDocuments() throws IOException {
        GetResponse getResponse = elasticsearchClient.get(a -> a.index("good_spu").id("1"), GoodSpuElasticsearchModel.class);
        System.out.println("result = " + getResponse.source());
    }

    @AfterEach
    void ElasticsearchClientDestroy () throws IOException {
        transport.close();
        restClient.close();
    }
}

运行结果:

result = GoodSpuElasticsearchModel(goodSpuId=1, goodSpuName=JavaEE企业级开发, goodSpuPrice=65, goodSpuPicture=JavaEE企业级开发.png, goodBrandId=1, goodBrandName=新华出版社, goodCategoryId=30, goodCategoryName=书籍)

3、删除文档

@SpringBootTest
class ElasticsearchStudyApplicationTests {

    private RestClient restClient;
    private ElasticsearchClient elasticsearchClient;
    private ElasticsearchTransport transport;
    @BeforeEach
    void ElasticsearchClientBuild(){
        // Create the low-level client
        restClient = RestClient.builder(
                new HttpHost("127.0.0.1", 9200)).build();

        // Create the transport with a Jackson mapper
        transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        // And create the API client
        elasticsearchClient = new ElasticsearchClient(transport);
    }

    @Test
    void deleteOneDocument() throws IOException {
        DeleteResponse deleteResponse = elasticsearchClient.delete(a ->
                a.index("good_spu").id("1"));
        System.out.println("result = " + deleteResponse.result());
    }
    
    @AfterEach
    void ElasticsearchClientDestroy () throws IOException {
        transport.close();
        restClient.close();
    }
}

运行结果:

result = Deleted

4、批量添加文档

@SpringBootTest
class ElasticsearchStudyApplicationTests {

    private RestClient restClient;
    private ElasticsearchClient elasticsearchClient;
    private ElasticsearchTransport transport;
    @BeforeEach
    void ElasticsearchClientBuild(){
        // Create the low-level client
        restClient = RestClient.builder(
                new HttpHost("127.0.0.1", 9200)).build();

        // Create the transport with a Jackson mapper
        transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        // And create the API client
        elasticsearchClient = new ElasticsearchClient(transport);
    }

    @Test
    void addDocumentList() throws IOException {
        List<GoodSpuElasticsearchModel> list = new ArrayList<>();
        list.add(new GoodSpuElasticsearchModel(1L, "Java企业及开发", 50, "Java企业及开发.png" , 1, "新华出版社", 30, "书籍"));

        list.add(new GoodSpuElasticsearchModel(2L, "Java编程", 55, "Java编程.png" , 1, "新华出版社", 30, "书籍"));
        list.add(new GoodSpuElasticsearchModel(3L, "Java入门到精通", 70, "Java入门到精通.png" , 1, "新华出版社", 30, "书籍"));
        list.add(new GoodSpuElasticsearchModel(4L, "Hadoop权威指南", 110, "Hadoop权威指南" , 1, "新华出版社", 30, "书籍"));
        list.add(new GoodSpuElasticsearchModel(5L, "编译原理", 75, "编译原理" , 1, "新华出版社", 30, "书籍"));
        list.add(new GoodSpuElasticsearchModel(6L, "操作原理", 60, "操作原理" , 1, "新华出版社", 30, "书籍"));

        List<BulkOperation> bulkOperations = new ArrayList<>();
        list.forEach(a ->
                bulkOperations.add(BulkOperation.of(b ->
                        b.index(c ->
                                c.id(String.valueOf(a.getGoodSpuId())).document(a)
                        )
                ))
        );
        BulkResponse bulkResponse = elasticsearchClient.bulk(x -> x.index("good_spu").operations(bulkOperations));
        bulkResponse.items().forEach(i -> System.out.println("i = " + i.result()));
        System.out.println("bulkResponse.errors() = " + bulkResponse.errors());
    }

    @AfterEach
    void ElasticsearchClientDestroy () throws IOException {
        transport.close();
        restClient.close();
    }
}

运行结果:

i = created
i = created
i = created
i = created
i = created
i = created
bulkResponse.errors() = false

使用 Kibana 查看文档

GET good_spu/_search
{
  "query": {
    "match_all": {}
  }
}
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 6,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "good_spu",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "goodSpuId" : 1,
          "goodSpuName" : "JavaEE企业级开发",
          "goodSpuPrice" : 65,
          "goodSpuPicture" : "JavaEE企业级开发.png",
          "goodBrandId" : 1,
          "goodBrandName" : "新华出版社",
          "goodCategoryId" : 30,
          "goodCategoryName" : "书籍"
        }
      },
      {
        "_index" : "good_spu",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "goodSpuId" : 2,
          "goodSpuName" : "Java编程",
          "goodSpuPrice" : 55,
          "goodSpuPicture" : "Java编程.png",
          "goodBrandId" : 1,
          "goodBrandName" : "新华出版社",
          "goodCategoryId" : 30,
          "goodCategoryName" : "书籍"
        }
      },
      {
        "_index" : "good_spu",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "goodSpuId" : 3,
          "goodSpuName" : "Java入门到精通",
          "goodSpuPrice" : 70,
          "goodSpuPicture" : "Java入门到精通.png",
          "goodBrandId" : 1,
          "goodBrandName" : "新华出版社",
          "goodCategoryId" : 30,
          "goodCategoryName" : "书籍"
        }
      },
      {
        "_index" : "good_spu",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "goodSpuId" : 4,
          "goodSpuName" : "Hadoop权威指南",
          "goodSpuPrice" : 110,
          "goodSpuPicture" : "Hadoop权威指南",
          "goodBrandId" : 1,
          "goodBrandName" : "新华出版社",
          "goodCategoryId" : 30,
          "goodCategoryName" : "书籍"
        }
      },
      {
        "_index" : "good_spu",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "goodSpuId" : 5,
          "goodSpuName" : "编译原理",
          "goodSpuPrice" : 75,
          "goodSpuPicture" : "编译原理",
          "goodBrandId" : 1,
          "goodBrandName" : "新华出版社",
          "goodCategoryId" : 30,
          "goodCategoryName" : "书籍"
        }
      },
      {
        "_index" : "good_spu",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "goodSpuId" : 6,
          "goodSpuName" : "操作原理",
          "goodSpuPrice" : 60,
          "goodSpuPicture" : "操作原理",
          "goodBrandId" : 1,
          "goodBrandName" : "新华出版社",
          "goodCategoryId" : 30,
          "goodCategoryName" : "书籍"
        }
      }
    ]
  }
}

批量添加文档成功

4、批量删除文档

@SpringBootTest
class ElasticsearchStudyApplicationTests {

    private RestClient restClient;
    private ElasticsearchClient elasticsearchClient;
    private ElasticsearchTransport transport;
    @BeforeEach
    void ElasticsearchClientBuild(){
        // Create the low-level client
        restClient = RestClient.builder(
                new HttpHost("127.0.0.1", 9200)).build();

        // Create the transport with a Jackson mapper
        transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        // And create the API client
        elasticsearchClient = new ElasticsearchClient(transport);
    }

    @Test
    void deleteDocumentList() throws IOException {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        List<BulkOperation> bulkOperations = new ArrayList<>();
        list.forEach(a ->
                bulkOperations.add(BulkOperation.of(b ->
                        b.delete(c -> c.id(a))
                ))
        );
        BulkResponse bulkResponse = elasticsearchClient.bulk(a -> a.index("good_spu").operations(bulkOperations));
        bulkResponse.items().forEach(a -> System.out.println("result = " + a.result()));
        System.out.println("bulkResponse.errors() = " + bulkResponse.errors());
    }

    @AfterEach
    void ElasticsearchClientDestroy () throws IOException {
        transport.close();
        restClient.close();
    }
}

运行结果:

result = deleted
result = deleted
result = deleted
result = deleted
result = deleted
result = deleted
bulkResponse.errors() = false

使用 Kibana 查看文档

GET good_spu/_search
{
  "query": {
    "match_all": {}
  }
}
{
  "took" : 433,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

批量删除文档成功

上一篇:SpringBoot 整合 Elasticsearch8(最新API——Java API Client for Elasticsearch)—— 1、索引操作