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(); } }
相关文章
相关标签/搜索