一:pom配置
<dependencies>
<!--es依赖-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.15.2</version>
</dependency>
<!--jsontoString依赖,根据自己写的方式,这个包也不一定需要使用-->
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
</dependency>
</dependencies>
<!--es仓库->
<repositories>
<repository>
<id>es-snapshots</id>
<name>elasticsearch snapshot repo</name>
<url>https://snapshots.elastic.co/maven/</url>
</repository>
</repositories>
二:直接看类
package com.example.demo;
import net.minidev.json.JSONObject;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
//1.创建客户端
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")
)
);
//2.创建索引
CreateIndexRequest request = new CreateIndexRequest("my_first_index");
//3.创建的每个索引都可以有与之关联的特定设置。
// request.settings(Settings.builder()
// .put("index.number_of_shards", 3)
// .put("index.number_of_replicas", 2)
// );
try {
// 4、创建 Mapping
//第一种方式
// request.mapping(
// "{\n" +
// " \"properties\": {\n" +
// " \"message\": {\n" +
// " \"type\": \"text\"\n" +
// " }\n" +
// " }\n" +
// "}",
// XContentType.JSON);
//第二种方式
XContentBuilder mapping = XContentFactory.jsonBuilder().
startObject().field("dynamic", true).
startObject("properties").startObject("my_first_fields")
.field("type","text")
.startObject("fields")
.startObject("raw")
.field("type","keyword")
.endObject()
.endObject()
.endObject().endObject().endObject();
request.mapping(mapping);
// 5、 发送请求
// 5.1 同步方式发送请求
CreateIndexResponse createIndexResponse = client.indices()
.create(request, RequestOptions.DEFAULT);
// 6、处理响应
boolean acknowledged = createIndexResponse.isAcknowledged();//指示是否所有节点都已确认请求
boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();//指示是否在超时之前为索引中的每个分片启动了必需的分片副本数
//7.创建一个文档
IndexRequest indexRequest = new IndexRequest("my_first_index");
//第一种方式
// indexRequest.id("1");
// String jsonString = "{" +
// "\"user\":\"kimchy\"," +
// "\"postDate\":\"2013-01-30\"," +
// "\"message\":\"trying out Elasticsearch\"" +
// "\"my_first_fields\":\"trying out Elasticsearch\"" +
// "}";
// request.source(jsonString, XContentType.JSON);
//第二种方式
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("user", "kimchy");
jsonMap.put("postDate", new Date());
jsonMap.put("message", "trying out Elasticsearch");
jsonMap.put("my_first_fields", "trying out Elasticsearch");
// IndexRequest indexRequest = new IndexRequest("my_first_index")
// .id("1").source(jsonMap);
indexRequest.source(JSONObject.toJSONString(jsonMap), XContentType.JSON);
client.index(indexRequest, RequestOptions.DEFAULT);
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
三:效果:
四:附上官网api:
Index API | Java REST Client [7.15] | Elastic
五:其他
本人只是在main方法里面使用,在正式的项目中可以先配置好RestHighLevelClient ,直接通过注解使用。
更多的功能与方式其实官网api写的很清楚了,英文不需要多好,看关键部分就行了。