JestClient 对ElasticSearch的操作
一、说明
1、elasticsearch版本:6.2.4 。
jdk版本:1.8(该升级赶紧升级吧,现在很多技术都是最低要求1.8)。
jest版本:5.3.3。
2、一些不错的文章
一些基本概念的讲解:http://www.gaowm.com/categories/Elasticsearch/
es配置文件参数介绍:https://www.jianshu.com/p/149a8da90bbc
中文ik插件安装:https://blog.csdn.net/zjcjava/article/details/78653753
linux启动需要更改的一些参数:https://www.cnblogs.com/woxpp/p/6061073.html
二、前提:
1、最好已经大概看过es的官方文档,附上文档地址:
中文:https://www.elastic.co/guide/cn/elasticsearch/guide/cn/index.html 虽然已经有点老了,不过还是可以看看的。(我英文不好看的这个)
英文:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html 英文好的直接看英文版的,毕竟是最新的。
2、知道自己需要的es命令:
比如想用jest进行索引模版的相关操作,需要知道操作模版的命令是“template” 等等。然后能在官方文档里查到相关命令的详细操作。
简单说就是现在已经知道怎么在 es里进行相关操作了。现在想要用jest进行实现。
三、开始:
1、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>
2、jest初始化,这里就不说了。直接开始操作了,简单说几个命令抛砖引玉:
索引模版:es命令地址:https://www.elastic.co/guide/cn/elasticsearch/guide/cn/index-templates.html
文档中可以看到命令是“_template”。
现在在jest的jar包里找 “template”相关的类
其实写到这 大概应该知道啥意思了。补张图:
写的有点乱,以后整理吧。
-------------------------2018-07-17------------------------------------
附上自己的代码实现(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(); } }