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

語言: CN / TW / HK

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

文件操作

1、新增文件

```java @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

json { "_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、查詢文件

```java @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、刪除文件

```java @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、批量新增文件

```java @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、批量刪除文件

```java @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、索引操作