成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Elasticsearch入門學(xué)習(xí)(四):使用javaAPI學(xué)習(xí)ES

kun_jian / 762人閱讀

摘要:一依賴剛開(kāi)始少這個(gè)包創(chuàng)建索引失敗官方文檔并沒(méi)有給這個(gè)提示二開(kāi)始之前的準(zhǔn)備官方文檔連接操作所用到的實(shí)體類三關(guān)于索引的操作官方文檔新增索引索引名稱分片副本內(nèi)容查詢指定索引索引名稱刪除索引四關(guān)于文檔的操作官方文檔創(chuàng)建文檔索引名稱前

一、Maven依賴
    
    
        org.elasticsearch
        elasticsearch
        7.1.0
    
    
        org.elasticsearch.client
        elasticsearch-rest-high-level-client
        7.1.0
    
二、開(kāi)始之前的準(zhǔn)備

官方文檔

    /**
     * 連接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;
    }
 /**
 * 操作所用到的實(shí)體類
 */
@Data
class Article{
private long id;
private String title;
    public Article(long id, String title) {
        this.id = id;
        this.title = title;
    }
}
三、關(guān)于索引的操作

官方文檔

新增索引

  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));
        //內(nèi)容
        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();
    }
}
四、關(guān)于文檔的操作

官方文檔

創(chuàng)建文檔

    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);
            //可以設(shè)置文章ID
            indexRequest.id("5");
            indexRequest.source(json, XContentType.JSON);
            IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

根據(jù)文檔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();
        }
    }

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/75357.html

相關(guān)文章

  • 慕課網(wǎng)_《ElasticSearch入門學(xué)習(xí)總結(jié)

    摘要:時(shí)間年月日星期四說(shuō)明本文部分內(nèi)容均來(lái)自慕課網(wǎng)。那么里面的數(shù)據(jù)就可以分為各種各樣的索引,比如汽車索引圖書索引家具索引等等。圖書索引又可以細(xì)分為各種類型,比如科普類小說(shuō)類技術(shù)類等等。具體到每一本書籍,就是文檔,就是整個(gè)圖書里面最小的存儲(chǔ)單位。 時(shí)間:2017年09月14日星期四說(shuō)明:本文部分內(nèi)容均來(lái)自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)源碼:無(wú)學(xué)習(xí)源碼:https...

    notebin 評(píng)論0 收藏0
  • 渣渣為什么要看 ElasticSearch 源碼?

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

    Cciradih 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<