Index API————(添加文档)
官网地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.4/java-rest-high-document-index.html
1.pom文件添加依赖(注意:依赖版本和安装的es主版本相同)
<!--es的rest风格的高级客户端 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.4.0</version>
</dependency>
2.获取客户端
private static RestHighLevelClient getRestHighLevelClient() throws Exception {
//1.指定es的集群 my-application:集群名称(在config目录下的elasticsearch.yml配置文件中查看)
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
//2.创建es的客户端 注意:9300为tcp端口;9200是http端口
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")));
return client;
}
3.添加文档的四种方式
- jsonString
- jsonMap
- XContentBuilder
- Object
3.1:jsonString
/**
* @param : client
* @description : 添加文档————jsonString
*/
private static void addDocumentByJsonString(RestHighLevelClient client) throws Exception {
//1.index请求
IndexRequest indexRequest = new IndexRequest("my_index", "my_type", "1");
//2.构建jsonString参数
Map<String, Object> paramsMap = new HashMap<>(16);
List<String> list = new ArrayList<>();
paramsMap.put("first_name", "cxk");
paramsMap.put("last_name", "cxk");
paramsMap.put("age", 25);
paramsMap.put("about", "道可道,非常道");
list.add("唱");
list.add("跳");
list.add("rap");
list.add("篮球");
paramsMap.put("interests", list);
String jsonString = JSON.toJSONString(paramsMap);
//3.设置请求数据(_source)
indexRequest.source(jsonString, XContentType.JSON);
//添加文档后的响应
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
//index
String index = indexResponse.getIndex();
//type
String type = indexResponse.getType();
//id
String id = indexResponse.getId();
//version:版本
long version = indexResponse.getVersion();
//获取结果:创建 | 更新
if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
//创建后的操作
} else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
//更新后的操作
}
//shardInfo:分片信息
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
}
//失败信息
if (shardInfo.getFailed() > 0) {
for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
String reason = failure.reason();
}
}
}
3.2 jsonMap
/**
* @param : client
* @description : 添加文档————jsonMap
*/
private static void addDocumentByJsonMap(RestHighLevelClient client) throws Exception {
IndexRequest indexRequest = new IndexRequest("my_index", "my_type", "2");
Map<String, Object> paramsMap = new HashMap<>(16);
List<String> list = new ArrayList<>();
paramsMap.put("first_name", "风月");
paramsMap.put("last_name", "wind");
paramsMap.put("age", 20);
paramsMap.put("about", "道可道,非常道");
list.add("书法");
list.add("篮球");
paramsMap.put("interests", list);
indexRequest.source(paramsMap);
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
Result result = indexResponse.getResult();
String s = indexResponse.toString();
System.out.println(result.toString());
System.out.println(s);
}
3.3 XContentBuilder
/**
* @param : client
* @description : 添加文档————XContentBuilder
*/
private static void addDocumentByXContentBuilder(RestHighLevelClient client) throws IOException {
//1.创建文档对象
List<String> list = new ArrayList<>();
list.add("read");
list.add("thinking");
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
builder.field("first_name", "欣儿");
builder.field("last_name", "xin");
builder.field("age", 18);
builder.field("about", "beautiful");
builder.field("interests", list);
builder.endObject();
IndexRequest indexRequest = new IndexRequest("my_index", "my_type", "3");
indexRequest.source(builder);
IndexResponse indexResponse=client.index(indexRequest,RequestOptions.DEFAULT);
Result result = indexResponse.getResult();
String s = indexResponse.toString();
System.out.println(result.toString());
System.out.println(s);
}
3.4 Object
/**
* @param : client
* @description : 添加文档————Object
*/
private static void addDocumentByObject(RestHighLevelClient client) throws IOException {
List<String> list = new ArrayList<>();
list.add("哲学");
list.add("计算机");
IndexRequest request = new IndexRequest("my_index", "my_type", "4")
.source("first_name","didi","last_name","dd","age",18,"about","yuan","interests", list);
IndexResponse indexResponse=client.index(request,RequestOptions.DEFAULT);
Result result = indexResponse.getResult();
String s = indexResponse.toString();
System.out.println(result.toString());
System.out.println(s);
}