ES 關於文檔的API操做

添加FastJSON依賴java

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.60</version>
        </dependency>

關於文檔的操做spring

package com.dance.danceesapi.test;

import com.alibaba.fastjson.JSON;
import com.dance.danceesapi.pojo.User;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

/**
 * 關於文檔的API的操做
 */
@SpringBootTest
public class TestDocument {

    @Autowired
    @Qualifier("restHighLevelClient")
    RestHighLevelClient restHighLevelClient;

    /**
     * 測試添加文檔
     * @throws IOException
     */
    @Test
    void addDocument() throws IOException {

        // 建立文檔對象
        User user = new User("彼岸舞111", 18);

        // 指定索引庫
        IndexRequest flower = new IndexRequest("flower");

        // 設置參數 id 超時時間 和數據源
        flower.id("4").timeout(TimeValue.timeValueSeconds(5)).source(JSON.toJSONString(user), XContentType.JSON);

        // 執行請求
        IndexResponse index = restHighLevelClient.index(flower, RequestOptions.DEFAULT);

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

        System.out.println(index.status());

    }

    /**
     * 測試文檔是否存在
     * @throws IOException
     */
    @Test
    void existDocument() throws IOException {

        GetRequest flower = new GetRequest("flower", "1");

        boolean exists = restHighLevelClient.exists(flower, RequestOptions.DEFAULT);

        System.out.println(exists);
    }

    /**
     * 獲取文檔信息
     * @throws IOException
     */
    @Test
    void getDocument() throws IOException {

        GetRequest flower = new GetRequest("flower", "1");

        GetResponse documentFields = restHighLevelClient.get(flower, RequestOptions.DEFAULT);

        System.out.println(documentFields.getSourceAsString());
    }

    /**
     * 測試文檔的更新
     * @throws IOException
     */
    @Test
    void updateDocument() throws IOException {

        User user = new User("彼岸草小姐姐",19);

        UpdateRequest flower = new UpdateRequest("flower","1");

        flower.timeout(TimeValue.timeValueSeconds(5));

        flower.doc(JSON.toJSONString(user),XContentType.JSON);

        UpdateResponse update = restHighLevelClient.update(flower, RequestOptions.DEFAULT);

        System.out.println(update);

        System.out.println(update.status());

    }

    /**
     * 測試文檔的刪除
     * @throws IOException
     */
    @Test
    void deleteDocument() throws IOException {

        DeleteRequest flower = new DeleteRequest("flower", "4");

        DeleteResponse delete = restHighLevelClient.delete(flower, RequestOptions.DEFAULT);

        System.out.println(delete);

        System.out.println(delete.status());

    }

    /**
     * 測試批量 增刪改查均可以 只須要更換不一樣的Request就能夠了
     * @throws IOException
     */
    @Test
    void batchDocument() throws IOException {

        BulkRequest flower = new BulkRequest();

        for (int i = 0; i < 100; i++) {
            int y = i + 4;
            User user = new User("測試" + y, y);
            flower.add(
                    new IndexRequest("flower")
                            .id(y+"")
                            .source(JSON.toJSONString(user),XContentType.JSON)
            );
        }

        BulkResponse bulk = restHighLevelClient.bulk(flower, RequestOptions.DEFAULT);

        System.out.println(bulk);

        System.out.println(bulk.status());

        // 返回false 表明沒有失敗
        System.out.println(bulk.hasFailures());

    }

    @Test
    void query() throws IOException {

        SearchRequest flower = new SearchRequest("flower");

        // 包含的字段
        String[] includes = new String[]{"name"};

        // 排除的字段
        String[] excludes = new String[]{"age"};

        // 構造搜索條件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        // match匹配 字段
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "彼岸");

        // 加入條件
        sourceBuilder.query(matchQueryBuilder);

        sourceBuilder.fetchSource(new FetchSourceContext(true,includes,excludes));

        // 分頁
        sourceBuilder.from(0);
        
        sourceBuilder.size(2);
        
        flower.source(sourceBuilder);

        SearchResponse search = restHighLevelClient.search(flower, RequestOptions.DEFAULT);

        System.out.println(search);

        System.out.println(search.getHits());

        for (SearchHit hit : search.getHits().getHits()) {
            System.out.println(hit.getSourceAsString());
        }

    }

}

做者:彼岸舞json

時間:2020\09\11api

內容關於:ElasticSearch網絡

本文來源於網絡,只作技術分享,一律不負任何責任elasticsearch

相關文章
相關標籤/搜索