国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

Elasticsearch入門學習(四):使用javaAPI學習ES

kun_jian / 755人閱讀

摘要:一依賴剛開始少這個包創建索引失敗官方文檔并沒有給這個提示二開始之前的準備官方文檔連接操作所用到的實體類三關于索引的操作官方文檔新增索引索引名稱分片副本內容查詢指定索引索引名稱刪除索引四關于文檔的操作官方文檔創建文檔索引名稱前

一、Maven依賴
    
    
        org.elasticsearch
        elasticsearch
        7.1.0
    
    
        org.elasticsearch.client
        elasticsearch-rest-high-level-client
        7.1.0
    
二、開始之前的準備

官方文檔

    /**
     * 連接ES
     * @return
     */
    public RestHighLevelClient start() {
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.100.151", 9201, "http"),
                        new HttpHost("192.168.100.151", 9202, "http"),
                        new HttpHost("192.168.100.151", 9203, "http")));
        return restHighLevelClient;
    }
 /**
 * 操作所用到的實體類
 */
@Data
class Article{
private long id;
private String title;
    public Article(long id, String title) {
        this.id = id;
        this.title = title;
    }
}
三、關于索引的操作

官方文檔

新增索引

  public void createIndex(RestHighLevelClient client) {
        //索引名稱
        CreateIndexRequest request = new CreateIndexRequest("hello");
        //分片副本
        request.settings(Settings.builder().put("index.number_of_shards", 5)
                .put("index.number_of_replicas", 1));
        //內容
        Map  id = new HashMap <>();
        id.put("type","text");
        id.put("store",true);
        Map  title = new HashMap <>();
        title.put("type","text");
        title.put("store",true);
        title.put("index",true);
        title.put("analyzer", "standard");
        Map  properties = new HashMap <>();
        properties.put("id",id);
        properties.put("title",title);
        Map  mapping = new HashMap <>();
        mapping.put("properties",properties);
        request.mapping(mapping);
        try {
            CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
            System.out.println(response.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

查詢指定索引

public void getIndex(RestHighLevelClient client) throws IOException {
    //索引名稱
    GetIndexRequest request = new GetIndexRequest("hello");
    try {
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

刪除索引

public void delIndex(RestHighLevelClient client){
    DeleteIndexRequest request = new DeleteIndexRequest("hello");
    try {
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println(delete.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
四、關于文檔的操作

官方文檔

創建文檔

    public void createDocument(RestHighLevelClient client){
        //索引名稱
        IndexRequest indexRequest = new IndexRequest("hello");
        ObjectMapper mapper = new ObjectMapper();
        Article article = new Article(3L, "web前端");
        byte[] json = new byte[0];
        try {
            json = mapper.writeValueAsBytes(article);
            //可以設置文章ID
            indexRequest.id("5");
            indexRequest.source(json, XContentType.JSON);
            IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

根據文檔ID查詢文檔

public void getDocument(RestHighLevelClient client){
    GetRequest getRequest = new GetRequest("hello", "1");
    GetResponse documentFields = null;
    try {
        documentFields = client.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(documentFields.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

更新文檔

    public void updateDocument(RestHighLevelClient client){
        UpdateRequest  updateRequest = new UpdateRequest("hello", "1");
        Article article = new Article(2L, "java入門到放棄");
        ObjectMapper mapper = new ObjectMapper();
        byte[] json = new byte[0];
        try {
            json = mapper.writeValueAsBytes(article);
            IndexRequest indexRequest = new IndexRequest("hello");
            indexRequest.source(json, XContentType.JSON);
            updateRequest.doc(indexRequest);
            UpdateResponse updateResponse = client.update(
                    updateRequest, RequestOptions.DEFAULT);
            System.out.println(updateResponse);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

刪除文檔

    public void delDocument(RestHighLevelClient client){
        DeleteRequest request = new DeleteRequest("hello", "1");
        try {
            DeleteResponse deleteResponse = client.delete(
                    request, RequestOptions.DEFAULT);
            System.out.println(deleteResponse);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

查詢文檔

    public void searchDocument(RestHighLevelClient client){
        SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = null;
        try {
            searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            System.out.println(searchResponse.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/75357.html

相關文章

  • 慕課網_《ElasticSearch入門學習總結

    摘要:時間年月日星期四說明本文部分內容均來自慕課網。那么里面的數據就可以分為各種各樣的索引,比如汽車索引圖書索引家具索引等等。圖書索引又可以細分為各種類型,比如科普類小說類技術類等等。具體到每一本書籍,就是文檔,就是整個圖書里面最小的存儲單位。 時間:2017年09月14日星期四說明:本文部分內容均來自慕課網。@慕課網:http://www.imooc.com教學源碼:無學習源碼:https...

    notebin 評論0 收藏0
  • 渣渣為什么要看 ElasticSearch 源碼?

    摘要:當時自己在本地測試搭建集群后,給分配了另外一個任務就是去了解中的自帶分詞英文分詞中文分詞的相同與差異以及自己建立分詞需要注意的點。還有就是官網的文檔了,非常非常詳細,還有,版本的是有中文的官方文檔,可以湊合著看。 前提 人工智能、大數據快速發展的今天,對于 TB 甚至 PB 級大數據的快速檢索已然成為剛需,大型企業早已淹沒在系統生成的浩瀚數據流當中。大數據技術業已集中在如何存儲和處理這...

    Cciradih 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<