赞
踩
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.8.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.0</version>
</dependency>
void addIndex() throws Exception { /** * GET /user * 作用:可以看到aliases、mappings、settings * 说明:我们下面使用的是直接定死的结构,而SpringBoot中使用的是注解的方式来创建索引结构,最终的原理都是下面的代码 * { * "user": { * "aliases": { * "user.aliases": {} * }, * "mappings": { * "properties": { * "age": { * "type": "integer" * }, * "name": { * "type": "text", * "fields": { * "keyword": { * "type": "keyword" * } * } * }, * "sex": { * "type": "keyword" * } * } * }, * "settings": { * "index": { * "creation_date": "1649243890532", * "number_of_shards": "9", * "number_of_replicas": "2", * "uuid": "EPChtL_vQj2gHJbO5VTHqg", * "version": { * "created": "7060099" * }, * "provided_name": "user" * } * } * } * } */ // 创建ES客户端对象 RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http"))); // 定义索引名称 CreateIndexRequest request = new CreateIndexRequest("user"); // 添加aliases,对比上述结构来理解 String aliaseStr = "{"user.aliases":{}}"; Map aliases = JSONObject.parseObject(aliaseStr, Map.class); // 添加mappings,对比上述结构来理解 String mappingStr = "{"properties":{"name":{"type":"text","fields":{"keyword":{"type":"keyword"}}},"sex":{"type":"keyword"},"age":{"type":"integer"}}}"; Map mappings = JSONObject.parseObject(mappingStr, Map.class); // 添加settings,对比上述结构来理解 String settingStr = "{"index":{"number_of_shards":"9","number_of_replicas":"2"}}"; Map settings = JSONObject.parseObject(settingStr, Map.class); // 添加数据 request.aliases(aliases); request.mapping(mappings); request.settings(settings); // 发送请求到ES CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT); // 处理响应结果 System.out.println("添加索引是否成功:" + response.isAcknowledged()); // 关闭ES客户端对象 client.close(); }
void getIndexInfo() throws Exception {
// 创建ES客户端对象
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
// 定义索引名称
GetIndexRequest request = new GetIndexRequest("user");
// 发送请求到ES
GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
// 处理响应结果
System.out.println("aliases:" + response.getAliases());
System.out.println("mappings:" + response.getMappings());
System.out.println("settings:" + response.getSettings());
// 关闭ES客户端对象
client.close();
}
void deleteIndex() throws Exception {
// 创建ES客户端对象
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
// 定义索引名称
DeleteIndexRequest request = new DeleteIndexRequest("user");
// 发送请求到ES
AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
// 处理响应结果
System.out.println("删除是否成功:" + response.isAcknowledged());
// 关闭ES客户端对象
client.close();
}
void insertDataToIndex() throws Exception { // 1、创建ES客户端对象 RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http"))); // 2、创建请求对象 User user = new User(); user.setName("张三"); user.setSex("男"); user.setAge(22); // 定义请求对象 IndexRequest request = new IndexRequest("user"); // 设置文档id request.id("1000"); // 将json格式字符串放在请求中 request.source(JSONObject.toJSONString(user), XContentType.JSON); // 3、发送请求到ES IndexResponse response = client.index(request, RequestOptions.DEFAULT); // 4、处理响应结果 System.out.println("数据插入结果:" + response.getResult()); // 5、关闭ES客户端对象 client.close(); }
void updateDataFromIndex() throws Exception { // 1、创建ES客户端对象 RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http"))); // 2、定义请求对象 User user = new User(); user.setName("小美"); user.setAge(20); UpdateRequest request = new UpdateRequest(); request.index("user").id("1000"); // 拓展:局部更新也可以这样写:request.doc(XContentType.JSON, "name", "李四", "age", 25);,其中"name"和"age"是User对象中的字段名称,而"小美"和20是对应的字段值 request.doc(JSONObject.toJSONString(user), XContentType.JSON); // 3、发送请求到ES UpdateResponse response = client.update(request, RequestOptions.DEFAULT); // 4、处理响应结果 System.out.println("数据更新结果:" + response.getResult()); // 5、关闭ES客户端对象 client.close(); }
void getDataById() throws Exception {
// 1、创建ES客户端对象
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
// 2、定义请求对象
GetRequest request = new GetRequest("user");
request.id("1000");
// 3、发送请求到ES
GetResponse response = client.get(request, RequestOptions.DEFAULT);
// 4、处理响应结果
System.out.println("查询结果:" + response.getSourceAsString());
// 5、关闭ES客户端对象
client.close();
}
void deleteDataById() throws Exception {
// 1、创建ES客户端对象
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
// 2、定义请求对象
DeleteRequest request = new DeleteRequest("user");
request.id("1000");
// 3、发送请求到ES
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
// 4、处理响应结果
System.out.println("删除是否成功:" + response.getResult());
// 5、关闭ES客户端对象
client.close();
}
void deleteDataByQuery() throws IOException {
// 1、创建ES客户端对象
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
// 2、定义请求对象
DeleteByQueryRequest request = new DeleteByQueryRequest("user");
request.setQuery(QueryBuilders.matchAllQuery());
// 3、发送请求到ES
BulkByScrollResponse response = client.deleteByQuery(request, RequestOptions.DEFAULT);
// 4、处理响应结果
System.out.println("删除失败结果:" + response.getBulkFailures());
// 5、关闭ES客户端对象
client.close();
}
void batchInsertDataToIndex() throws Exception {
// 1、创建ES客户端对象
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
// 2、定义请求对象
// 准备测试数据
List<User> userList = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
User user = new User();
user.setName(i % 2 == 0 ? "张三" + i : "小美" + i);
user.
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。