首先是依赖问题,导入依赖之后会有版本问题:
SpringBoot 2.3.0
支持 7.6.2
版本,但是我不是最新的版本,所以自定义版本:
<properties>
<java.version>1.8java.version>
<elasticsearch>7.6.1elasticsearch>
properties>
我们先创建一个配置类:
@Configuration
public class EsConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
return new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")));
}
}
在测试类中测试:
@Test
void testCreatedIndex() throws IOException {
// 1.创建索引请求
CreateIndexRequest request = new CreateIndexRequest("test3");
// 2.客户端执行请求,获取响应
CreateIndexResponse response = restHighLevelClient.indices()
.create(request, RequestOptions.DEFAULT);
System.out.println(response);
}
打印结果:
org.elasticsearch.client.indices.CreateIndexResponse@6a5dc80
之前已经演示了创建索引了,下面来看一下其他的 API:
测试索引是否存在
/**
* 测试索引是否存在
* @throws IOException exists 会抛出异常
*/
@Test
void testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("test3");
boolean exists = restHighLevelClient.indices()
.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
测试删除索引
/**
* 测试删除索引
* @throws IOException
*/
@Test
void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("test3");
AcknowledgedResponse response = restHighLevelClient.indices()
.delete(request, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());
}
然后再去看发现 test3
已经没了。
首先建一个实体类:
@Data
@Component
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User implements Serializable {
private String name;
private Integer age;
}
创建文档:
/**
* 测试添加文档
*
* @throws IOException 异常
*/
@Test
void testAddDocument() throws IOException {
// 创建对象
User user = new User("WSuo", 18);
// 创建请求
IndexRequest request = new IndexRequest("test3");
// 设置规则 put /test3/_doc/1
request.id("1");
// 将数据放入请求 json
request.source(JSON.toJSONString(user), XContentType.JSON);
// 客户端发送请求
IndexResponse indexResponse = restHighLevelClient
.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.status());
}
结果:
CREATED
/**
* 测试文档是否存在
*/
@Test
void testIsExist() throws IOException {
GetRequest request = new GetRequest("test3", "1");
request.fetchSourceContext(new FetchSourceContext(false)).storedFields("_none_");
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
new FetchSourceContext(false))
:表示不返回 _source
上下文了获取文档的信息 get /test3/_doc/1
/**
* 获取文档的信息 get test3/_doc/1
*/
@Test
void testGetDocument() throws IOException {
GetRequest request = new GetRequest("test3", "1");
GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
}
结果是:
{"age":18,"name":"WSuo"}
更新文档信息 post /test3/_doc/1/_update
/**
* 更新文档信息 post /test3/_doc/1/_update
* @throws IOException 异常
*/
@Test
void testUpdateDocument() throws IOException {
UpdateRequest request = new UpdateRequest("test3", "1");
User user = new User("wSuo", 19);
request.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
System.out.println(response.status());
}
删除文档信息 delete /test3/_doc/2
/**
* 删除文档信息 delete /test3/_doc/2
* @throws IOException 异常
*/
@Test
void testDeleteDocument() throws IOException {
DeleteRequest request = new DeleteRequest("test3", "1");
DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
System.out.println(response.status());
}
批量导入数据
/**
* 批量导入数据
*/
@Test
void testBulkRequest() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("10s");
ArrayList<User> users = new ArrayList<>();
users.add(new User("WSuo1", 18));
users.add(new User("WSuo2", 19));
users.add(new User("WSuo3", 10));
users.add(new User("WSuo4", 20));
users.add(new User("WSuo5", 21));
// 批处理请求
for (User user : users) {
bulkRequest.add(new IndexRequest("test3")
.source(JSON.toJSONString(user), XContentType.JSON));
}
BulkResponse response = restHighLevelClient
.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(response.hasFailures());
}
可以看到 ID
值是随机给的,我们也可以指定 ID
值,在这个位置。
查询
查询出来所有的数据,这里提供的方法和类几乎和原生的 API 一致,如果你对于操作原生的数据库不熟悉,参考:https://blog.csdn.net/weixin_43941364/article/details/106272237
/**
* 查询数据
*/
@Test
void testSearch() throws IOException {
SearchRequest searchRequest = new SearchRequest("test3");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 查询条件,我们可以使用 QueryBuilders 工具类实现
// QueryBuilders.termQuery: 精确匹配
// QueryBuilders.matchAllQuery: 匹配所有
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "WSuo1");
sourceBuilder.query(termQueryBuilder);
// 分页
sourceBuilder.from(0);
sourceBuilder.size(2);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchRequest.source(sourceBuilder);
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(JSON.toJSONString(hits));
}