Es Java API
2019-02-26 17:04:38 2 举报
AI智能生成
ElasticSearch Java API 脑图
作者其他创作
大纲/内容
索引管理
通过client.admin().indices()(indicesAdminClient)
index是否存在
1 IndicesExistsResponse exReponse = indicesAdminClient.prepareExists("test").get()
2 exReponse.isExists()
type 是否存在
1 indicesAdminClient.prepareTypesExists("test").setTypes("c2b_sub_demand_detail").get()
注意:setTypes 可以写多个
2 existsResponse.isExists()
create index
1 cRepose = indicesAdminClient.prepareCreate("yyb_test").get()
注意:索引名称必须小写
2 cRepose.isAcknowledged()
delete index
1 delReponse = indicesAdminClient.prepareDelete("yyb_test").get()
2 delReponse.isAcknowledged()
close index
1 closeResponse = indicesAdminClient.prepareClose("test").get()
2 closeResponse.isAcknowledged()
open index
1 openResponse = indicesAdminClient.prepareOpen("test").get()
2 openResponse.isAcknowledged()
设置索引别名
1 aliasReponse = indicesAdminClient.prepareAliases().addAlias("test", "xxx").get()
2 aliasReponse.isAcknowledged()
获取索引别名
1 aliasReponse = indicesAdminClient.prepareGetAliases("xxx").get()
2 aliasReponse.getAliases()
刷新 es/index/indexs
indicesAdminClient.prepareRefresh().get();
indicesAdminClient.prepareRefresh("test").get()
indicesAdminClient.prepareRefresh("test", "test01").get()
创建索引并设置settings
1 createReponse = indicesAdminClient.prepareCreate("test_yyb").setSettings(Settings.builder()
.put("index.number_of_shards", 3).put("index.number_of_replicas", 2)).get()
.put("index.number_of_shards", 3).put("index.number_of_replicas", 2)).get()
2 createReponse.isAcknowledged()
更新副本数
1 resReponse = indicesAdminClient.prepareUpdateSettings("test_yyb").setSettings(Settings.builder().
put("index.number_of_replicas", 0)).get()
put("index.number_of_replicas", 0)).get()
2 resReponse.isAcknowledged()
获取settings
1 setReponse = indicesAdminClient.prepareGetSettings("test_yyb").get()
2 for(ObjectObjectCursor<String, Settings> cusor: setReponse.getIndexToSettings()){
String index = cusor.key;
Settings settings = cusor.value;
Integer shards = settings.getAsInt(settings.get("index.number_of_shards"), null);
Integer replaces = settings.getAsInt(settings.get("index.number_of_replicas"), null);
System.out.println("index -> shards:" + shards +",replaces:" + replaces );
}
String index = cusor.key;
Settings settings = cusor.value;
Integer shards = settings.getAsInt(settings.get("index.number_of_shards"), null);
Integer replaces = settings.getAsInt(settings.get("index.number_of_replicas"), null);
System.out.println("index -> shards:" + shards +",replaces:" + replaces );
}
设置 mappings
1 mapResponse = indicesAdminClient.preparePutMapping("test_yyb").setType("test").setSource(
"{\n" +
"\"properties\":{\n" +
"\"name\":{\"type\":\"keyword\"}\n" +
"}\n" +
"}"
).get()
"{\n" +
"\"properties\":{\n" +
"\"name\":{\"type\":\"keyword\"}\n" +
"}\n" +
"}"
).get()
2 mapResponse.isAcknowledged()
获取 mappings
1 getmapResponse = indicesAdminClient.prepareGetMappings("yyb_test").get()
2 MappingMetaData mappings = getmapResponse.getMappings().get("yyb_test").get("test");
System.out.println(mappings.getSourceAsMap())
System.out.println(mappings.getSourceAsMap())
文档管理
新建文档
json转String
使用Map
1 Map<String, Object> doc = new HashMap<>();
doc.put("user", "kichy");
doc.put("postDate", "2013-01-30");
doc.put("message", "trying out Elasticsearch");
IndexResponse resp = cli.client.prepareIndex("test01", "test", "2").setSource(doc).get()
doc.put("user", "kichy");
doc.put("postDate", "2013-01-30");
doc.put("message", "trying out Elasticsearch");
IndexResponse resp = cli.client.prepareIndex("test01", "test", "2").setSource(doc).get()
2 System.out.println(resp.status())
使用Es帮助类
使用Jackson序列化Java Bean
获取文档
1 res = cli.client.prepareGet().setIndex("test01").setType("test").setId("1").get()
isExists()
文档是否存在
getIndex()
文档索引名
getType()
文档类型
getId()
文档id
getVersion()
文档版本信息
getSourceAsBytes()
二进制读取文档内容
getSourceAsMap()
map形式读取文档内容
getSourceAsString()
文本形式读取文档内容
isSourceEmpty()
判断文档内容是否为空
删除文档
res = cli.client.prepareDelete().setIndex("test01").setType("test").setId("1").get()
status
删除是否成功
getType()
文档类型
getId()
文档id
getVersion()
文档版本信息
更新文档
UpdateRequest
UpdateRequest up = new UpdateRequest();
up.index("test01");
up.type("test");
up.id("2");
up.doc(XContentFactory.jsonBuilder().startObject().field("gender", "male").endObject());
UpdateResponse res = cli.client.update(up).get();
System.out.println(res.status());
up.index("test01");
up.type("test");
up.id("2");
up.doc(XContentFactory.jsonBuilder().startObject().field("gender", "male").endObject());
UpdateResponse res = cli.client.update(up).get();
System.out.println(res.status());
内嵌脚本
UpdateRequest up = new UpdateRequest("test01", "test", "2").script(new Script("ctx._source.genner=\"male1\""));
UpdateResponse res = cli.client.update(up).get();
System.out.println(res.status());
UpdateResponse res = cli.client.update(up).get();
System.out.println(res.status());
prepareUpdate
UpdateResponse res = cli.client.prepareUpdate().setIndex("test01").setType("test").setId("3").
setDoc(XContentFactory.jsonBuilder().startObject().field("gener", "male").endObject()).get();
System.out.println(res.status());
setDoc(XContentFactory.jsonBuilder().startObject().field("gener", "male").endObject()).get();
System.out.println(res.status());
0 条评论
下一页