ElasticSearch Java 高級客戶端索引操作~

松哥原創的 Spring Boot 視頻教程已經殺青,感興趣的小夥伴戳這裏-->Spring Boot+Vue+微人事視頻教程


繼續 Es 客戶端~今天我們來看看 high level rest client ~

以下是視頻筆記:

注意,筆記只是視頻內容的一個簡要記錄,因此筆記內容比較簡單,完整的內容可以查看視頻。

28.1 索引管理

28.1.1 創建索引

首先創建一個普通的 Maven 項目,然後引入 high level rest client 依賴:

<dependencies>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.10.0</version>
    </dependency>
</dependencies>

需要注意,依賴的版本和 Es 的版本要對應。

創建一個索引:

public class HighLevelTest {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost"9200"http"),
                new HttpHost("localhost"9201"http"),
                new HttpHost("localhost"9202"http")
        ));
        //刪除已經存在的索引
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog");
        client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        //創建一個索引
        CreateIndexRequest blog1 = new CreateIndexRequest("blog");
        //配置 settings,分片、副本等信息
        blog1.settings(Settings.builder().put("index.number_of_shards"3).put("index.number_of_replicas"2));
        //配置字段類型,字段類型可以通過 JSON 字符串、Map 以及 XContentBuilder 三種方式來構建
        //json 字符串的方式
        blog1.mapping("{\"properties\": {\"title\": {\"type\": \"text\"}}}", XContentType.JSON);
        //執行請求,創建索引
        client.indices().create(blog1, RequestOptions.DEFAULT);
        //關閉 client
        client.close();
    }
}

mapping 的配置,還有另外兩種方式:

第一種,通過 map 構建 mapping:

public class HighLevelTest {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost"9200"http"),
                new HttpHost("localhost"9201"http"),
                new HttpHost("localhost"9202"http")
        ));
        //刪除已經存在的索引
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog");
        client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        //創建一個索引
        CreateIndexRequest blog1 = new CreateIndexRequest("blog");
        //配置 settings,分片、副本等信息
        blog1.settings(Settings.builder().put("index.number_of_shards"3).put("index.number_of_replicas"2));
        //配置字段類型,字段類型可以通過 JSON 字符串、Map 以及 XContentBuilder 三種方式來構建
        //json 字符串的方式
//        blog1.mapping("{\"properties\": {\"title\": {\"type\": \"text\"}}}", XContentType.JSON);
        //map 的方式
        Map<String, String> title = new HashMap<>();
        title.put("type""text");
        Map<String, Object> properties = new HashMap<>();
        properties.put("title", title);
        Map<String, Object> mappings = new HashMap<>();
        mappings.put("properties", properties);
        blog1.mapping(mappings);
        //執行請求,創建索引
        client.indices().create(blog1, RequestOptions.DEFAULT);
        //關閉 client
        client.close();
    }
}

第二種,通過 XContentBuilder 構建 mapping:

public class HighLevelTest {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost"9200"http"),
                new HttpHost("localhost"9201"http"),
                new HttpHost("localhost"9202"http")
        ));
        //刪除已經存在的索引
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog");
        client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        //創建一個索引
        CreateIndexRequest blog1 = new CreateIndexRequest("blog");
        //配置 settings,分片、副本等信息
        blog1.settings(Settings.builder().put("index.number_of_shards"3).put("index.number_of_replicas"2));
        //配置字段類型,字段類型可以通過 JSON 字符串、Map 以及 XContentBuilder 三種方式來構建
        //json 字符串的方式
//        blog1.mapping("{\"properties\": {\"title\": {\"type\": \"text\"}}}", XContentType.JSON);
        //map 的方式
//        Map<String, String> title = new HashMap<>();
//        title.put("type", "text");
//        Map<String, Object> properties = new HashMap<>();
//        properties.put("title", title);
//        Map<String, Object> mappings = new HashMap<>();
//        mappings.put("properties", properties);
//        blog1.mapping(mappings);
        //XContentBuilder 方式
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        builder.startObject("properties");
        builder.startObject("title");
        builder.field("type""text");
        builder.endObject();
        builder.endObject();
        builder.endObject();
        blog1.mapping(builder);
        //執行請求,創建索引
        client.indices().create(blog1, RequestOptions.DEFAULT);
        //關閉 client
        client.close();
    }
}

還可以給索引配置別名:

public class HighLevelTest {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost"9200"http"),
                new HttpHost("localhost"9201"http"),
                new HttpHost("localhost"9202"http")
        ));
        //刪除已經存在的索引
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog");
        client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        //創建一個索引
        CreateIndexRequest blog1 = new CreateIndexRequest("blog");
        //配置 settings,分片、副本等信息
        blog1.settings(Settings.builder().put("index.number_of_shards"3).put("index.number_of_replicas"2));
        //配置字段類型,字段類型可以通過 JSON 字符串、Map 以及 XContentBuilder 三種方式來構建
        //json 字符串的方式
//        blog1.mapping("{\"properties\": {\"title\": {\"type\": \"text\"}}}", XContentType.JSON);
        //map 的方式
//        Map<String, String> title = new HashMap<>();
//        title.put("type", "text");
//        Map<String, Object> properties = new HashMap<>();
//        properties.put("title", title);
//        Map<String, Object> mappings = new HashMap<>();
//        mappings.put("properties", properties);
//        blog1.mapping(mappings);
        //XContentBuilder 方式
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        builder.startObject("properties");
        builder.startObject("title");
        builder.field("type""text");
        builder.endObject();
        builder.endObject();
        builder.endObject();
        blog1.mapping(builder);
        //配置別名
        blog1.alias(new Alias("blog_alias"));
        //執行請求,創建索引
        client.indices().create(blog1, RequestOptions.DEFAULT);
        //關閉 client
        client.close();
    }
}

如果覺得調 API 太麻煩,也可以直接上 JSON:

public class HighLevelTest2 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost"9200"http"),
                new HttpHost("localhost"9201"http"),
                new HttpHost("localhost"9202"http")
        ));
        //刪除已經存在的索引
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog");
        client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        //創建一個索引
        CreateIndexRequest blog1 = new CreateIndexRequest("blog");
        //直接同構 JSON 配置索引
        blog1.source("{\"settings\": {\"number_of_shards\": 3,\"number_of_replicas\": 2},\"mappings\": {\"properties\": {\"title\": {\"type\": \"keyword\"}}},\"aliases\": {\"blog_alias_javaboy\": {}}}", XContentType.JSON);
        //執行請求,創建索引
        client.indices().create(blog1, RequestOptions.DEFAULT);
        //關閉 client
        client.close();
    }
}

另外還有一些其他的可選配置:

public class HighLevelTest2 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost"9200"http"),
                new HttpHost("localhost"9201"http"),
                new HttpHost("localhost"9202"http")
        ));
        //刪除已經存在的索引
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog");
        client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        //創建一個索引
        CreateIndexRequest blog1 = new CreateIndexRequest("blog");
        //直接同構 JSON 配置索引
        blog1.source("{\"settings\": {\"number_of_shards\": 3,\"number_of_replicas\": 2},\"mappings\": {\"properties\": {\"title\": {\"type\": \"keyword\"}}},\"aliases\": {\"blog_alias_javaboy\": {}}}", XContentType.JSON);
        //請求超時時間,連接所有節點的超時時間
        blog1.setTimeout(TimeValue.timeValueMinutes(2));
        //連接 master 節點的超時時間
        blog1.setMasterTimeout(TimeValue.timeValueMinutes(1));
        //執行請求,創建索引
        client.indices().create(blog1, RequestOptions.DEFAULT);
        //關閉 client
        client.close();
    }
}

前面所有的請求都是同步的,會阻塞的,也可以異步:

public class HighLevelTest2 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost"9200"http"),
                new HttpHost("localhost"9201"http"),
                new HttpHost("localhost"9202"http")
        ));
        //刪除已經存在的索引
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog");
        client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        //創建一個索引
        CreateIndexRequest blog1 = new CreateIndexRequest("blog");
        //直接同構 JSON 配置索引
        blog1.source("{\"settings\": {\"number_of_shards\": 3,\"number_of_replicas\": 2},\"mappings\": {\"properties\": {\"title\": {\"type\": \"keyword\"}}},\"aliases\": {\"blog_alias_javaboy\": {}}}", XContentType.JSON);
        //請求超時時間,連接所有節點的超時時間
        blog1.setTimeout(TimeValue.timeValueMinutes(2));
        //連接 master 節點的超時時間
        blog1.setMasterTimeout(TimeValue.timeValueMinutes(1));
        //執行請求,創建索引
//        client.indices().create(blog1, RequestOptions.DEFAULT);
        //異步創建索引
        client.indices().createAsync(blog1, RequestOptions.DEFAULT, new ActionListener<CreateIndexResponse>() {
            //請求成功
            @Override
            public void onResponse(CreateIndexResponse createIndexResponse) {
                //關閉 client
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //請求失敗
            @Override
            public void onFailure(Exception e) {

            }
        });
        //關閉 client
//        client.close();
    }
}

ElasticSearch 基礎知識:

  1. 打算出一個 ElasticSearch 教程,誰贊成,誰反對?
  2. ElasticSearch 從安裝開始
  3. ElasticSearch 第三彈,核心概念介紹
  4. ElasticSearch 中的中文分詞器該怎麼玩?
  5. ElasticSearch 索引基本操作
  6. ElasticSearch 文檔的添加、獲取以及更新
  7. ElasticSearch 文檔的刪除和批量操作
  8. ElasticSearch 文檔路由,你的數據到底存在哪一個分片上?
  9. ElasticSearch 併發的處理方式:鎖和版本控制
  10. ElasticSearch 中的倒排索引到底是什麼?
  11. ElasticSearch 動態映射與靜態映射
  12. ElasticSearch 四種字段類型詳解
  13. ElasticSearch 中的地理類型和特殊類型
  14. ElasticSearch 23 種映射參數詳解
  15. ElasticSearch 如何配置某個字段的權重?
  16. ElasticSearch 23 種映射參數詳解【3】
  17. ElasticSearch 映射模版
  18. ElasticSearch 搜索入門
  19. ElasticSearch 全文搜索怎麼玩?
  20. ElasticSearch 打錯字還能搜索到?試試 fuzzy query!
  21. ElasticSearch 複合查詢,理解 Es 中的文檔評分策略!
  22. 想搜索附近評分較高的餐廳,ElasticSearch 大顯身手!
  23. ElasticSearch 如何像 MySQL 一樣做多表聯合查詢?
  24. ElasticSearch 地理位置查詢與特殊查詢
  25. ElasticSearch 搜索高亮與排序
  26. ElasticSearch 指標聚合
  27. ElasticSearch 桶聚合
  28. ElasticSearch 管道聚合
  29. Java 操作 ElasticSearch,so easy!



往期推薦
0 1

50+ 需求文檔免費下載!

0 2

Spring Security 教程合集

0 3

接了兩個私活,都是血汗錢

本文分享自微信公衆號 - 江南一點雨(a_javaboy)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章