JestClient 使用教程,教你完成大部分ElasticSearch的操做。

  本篇文章代碼實現很少,主要是教你如何用JestClient去實現ElasticSearch上的操做。html

  授人以魚不如授人以漁。java

1、說明

  一、elasticsearch版本:6.2.4 。linux

    jdk版本:1.8(該升級趕忙升級吧,如今不少技術都是最低要求1.8)。apache

    jest版本:5.3.3。json

 

  二、一些不錯的文章緩存

    一些基本概念的講解:http://www.gaowm.com/categories/Elasticsearch/app

    es配置文件參數介紹:https://www.jianshu.com/p/149a8da90bbcelasticsearch

    中文ik插件安裝:https://blog.csdn.net/zjcjava/article/details/78653753maven

    linux啓動須要更改的一些參數:https://www.cnblogs.com/woxpp/p/6061073.htmlide

2、前提:

       一、最好已經大概看過es的官方文檔,附上文檔地址:

        中文:https://www.elastic.co/guide/cn/elasticsearch/guide/cn/index.html   雖然已經有點老了,不過仍是能夠看看的。(我英文很差看的這個)

        英文:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html   英文好的直接看英文版的,畢竟是最新的。

      二、知道本身須要的es命令:

        好比想用jest進行索引模版的相關操做,須要知道操做模版的命令是「template」 等等。而後能在官方文檔裏查到相關命令的詳細操做。

        簡單說就是如今已經知道怎麼在 es裏進行相關操做了。如今想要用jest進行實現。

3、開始:

  一、pom依賴:

      

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ceshi</groupId>
    <artifactId>ceshi</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>5.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>6.2.4</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- java編譯插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
pom.xml

 

   二、jest初始化,這裏就不說了。直接開始操做了,簡單說幾個命令拋磚引玉:

    索引模版:es命令地址:https://www.elastic.co/guide/cn/elasticsearch/guide/cn/index-templates.html

     文檔中能夠看到命令是「_template」。 

     如今在jest的jar包裏找 「template」相關的類

     

      

其實寫到這 大概應該知道啥意思了。補張圖:

 

 寫的有點亂,之後整理吧。

-------------------------2018-07-17------------------------------------

附上本身的代碼實現(index的一些操做)。數據操做以後整理完,再發。

建立index public void createIndex(String index) { try { JestResult jestResult = jestClient.execute(new CreateIndex.Builder(index).build()); System.out.println("createIndex:{}" + jestResult.isSucceeded()); } catch (IOException e) { e.printStackTrace(); } } 刪除index public void deleteIndex(String index) { try { JestResult jestResult = jestClient.execute(new DeleteIndex.Builder(index).build()); System.out.println("deleteIndex result:{}" + jestResult.isSucceeded()); } catch (IOException e) { e.printStackTrace(); } } 設置index的mapping(設置數據類型和分詞方式) public void createIndexMapping(String index, String type, String mappingString) { //mappingString爲拼接好的json格式的mapping串
    PutMapping.Builder builder = new PutMapping.Builder(index, type, mappingString); try { JestResult jestResult = jestClient.execute(builder.build()); System.out.println("createIndexMapping result:{}" + jestResult.isSucceeded()); if (!jestResult.isSucceeded()) { System.err.println("settingIndexMapping error:{}" + jestResult.getErrorMessage()); } } catch (IOException e) { e.printStackTrace(); } } 獲取index的mapping public String getMapping(String indexName, String typeName) { GetMapping.Builder builder = new GetMapping.Builder(); builder.addIndex(indexName).addType(typeName); try { JestResult result = jestClient.execute(builder.build()); if (result != null && result.isSucceeded()) { return result.getSourceAsObject(JsonObject.class).toString(); } } catch (Exception e) { e.printStackTrace(); } return null; } 獲取索引index設置setting public boolean getIndexSettings(String index) { try { JestResult jestResult = jestClient.execute(new GetSettings.Builder().addIndex(index).build()); System.out.println(jestResult.getJsonString()); if (jestResult != null) { return jestResult.isSucceeded(); } } catch (IOException e) { e.printStackTrace(); } return false; } 更改索引index設置setting public boolean updateIndexSettings(String index) { String source; XContentBuilder mapBuilder = null; try { mapBuilder = XContentFactory.jsonBuilder(); mapBuilder.startObject().startObject("index").field("max_result_window", "1000000").endObject().endObject(); source = mapBuilder.string(); JestResult jestResult = jestClient.execute(new UpdateSettings.Builder(source).build()); System.out.println(jestResult.getJsonString()); if (jestResult != null) { return jestResult.isSucceeded(); } } catch (IOException e) { e.printStackTrace(); } return false; } 獲取索引 別名 public boolean getIndexAliases(String index) { try { JestResult jestResult = jestClient.execute(new GetAliases.Builder().addIndex(index).build()); System.out.println(jestResult.getJsonString()); if (jestResult != null) { return jestResult.isSucceeded(); } } catch (IOException e) { e.printStackTrace(); } return false; } 添加索引別名 public void addAlias(List<String> index, String alias) { try { AddAliasMapping build = new AddAliasMapping.Builder(index, alias).build(); JestResult jestResult = jestClient.execute(new ModifyAliases.Builder(build).build()); System.out.println("result:" + jestResult.getJsonString()); } catch (IOException e) { e.printStackTrace(); } } 獲取索引模版 public void getTemplate(String template) { try { JestResult jestResult = jestClient.execute(new GetTemplate.Builder(template).build()); System.out.println("result:" + jestResult.getJsonString()); } catch (IOException e) { e.printStackTrace(); } } 添加索引模版 public void putreturnreportTemplate() { String source; XContentBuilder mapBuilder = null; try { mapBuilder = XContentFactory.jsonBuilder(); mapBuilder.startObject().field("template", "df_returnreport*").field("order", 1)//                 .startObject("settings").field("number_of_shards", 5)//五個分片
                .startObject("index").field("max_result_window", "1000000")//一次查詢最大一百萬
                .endObject()//                 .endObject()//                 .startObject("mappings")//  .startObject("df_returnreport")//type名
                .startObject("properties")//                 .startObject("id").field("type", "long").endObject()//                 .startObject("username").field("type", "keyword").endObject()//                 .startObject("content").field("type", "text").field("analyzer", "ik_max_word").endObject()//                 .startObject("returntime").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss").endObject()//                 .startObject("gateway").field("type", "integer").endObject()//                 .endObject()//                 .endObject()//  .endObject()//                 .startObject("aliases").startObject("df_returnreport").endObject().endObject()//別名
                .endObject();//         source = mapBuilder.string(); JestResult jestResult = jestClient.execute(new PutTemplate.Builder("my_returnreport", source).build()); System.out.println("result:" + jestResult.getJsonString()); } catch (IOException e) { e.printStackTrace(); } } 索引優化 public void optimizeIndex() { Optimize optimize = new Optimize.Builder().build(); jestClient.executeAsync(optimize, new JestResultHandler<JestResult>() { public void completed(JestResult jestResult) { System.out.println("optimizeIndex result:{}" + jestResult.isSucceeded()); } public void failed(Exception e) { e.printStackTrace(); } }); } 清理緩存 public void clearCache() { try { ClearCache clearCache = new ClearCache.Builder().build(); jestClient.execute(clearCache); } catch (IOException e) { e.printStackTrace(); } }
相關文章
相關標籤/搜索