创建项目添加spring-boot-starter-data-elasticsearch依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-elasticsearchartifactId>
dependency>
版本对应关系
https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.versions
创建配置类
@Configuration
public class ElasticSearchConfig {
// 注册 rest高级客户端
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("ip地址",9200,"http")
)
);
return client;
}
}
创建实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private static final long serialVersionUID = -4925178914240204536L;
private String name;
private Integer age;
}
测试类中编写测试
@SpringBootTest
class SpringbootElasticsearchApplicationTests {
@Autowired
public RestHighLevelClient client;
@Test
void contextLoads() {
System.out.println(client);
}
}
创建索引
/**
* 测试索引创建
* @throws IOException
*/
@Test
public void testCreateIndex() throws IOException {
//创建索引对象, 索引名称
CreateIndexRequest request = new CreateIndexRequest("boot-test");
//根据索引对象创建索引
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
// 查看是否创建成功, true代表创建成功
System.out.println(response.isAcknowledged());
// 查看返回对象
System.out.println(response);
client.close();
}
获取索引,并判断是否存在
/**
* 测试获取索引,并判断是否存在
* @throws IOException
*/
@Test
public void testIndexIsExists() throws IOException {
//创建查询索引对象, 并指定索引名称
GetIndexRequest request = new GetIndexRequest("boot-test");
//根据索引对象查询是否存在
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
// 返回 true 代表存在
System.out.println(exists);
client.close();
}
删除索引,查看是否成功
/**
* 测试删除索引,并判断是否成功
* @throws IOException
*/
@Test
public void testDeleteIndex() throws IOException {
//创建删除索引对象, 并指定索引名称
DeleteIndexRequest request = new DeleteIndexRequest("boot-test");
//根据索引对象删除索引
AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
// 返回 true 代表删除成功
System.out.println(response.isAcknowledged());
client.close();
}
添加文档
/**
* 测试创建文档
* @throws IOException
*/
@Test
public void testAddDocument() throws IOException {
//创建User对象
User user = new User("dada", 18);
//创建文档信息对象,指定索引
IndexRequest request = new IndexRequest("boot-test");
//指定文档id
request.id("1");
//指定文档数据
request.source(JSON.toJSONString(user), XContentType.JSON);
//客户端发送数据,获取响应的结果
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
//获取建立索引的状态信息
System.out.println(response.status());
//获取文档创建的结果---首次创建该文档则返回CREATED,若存在该文档则返回UPDATE
System.out.println(response);
client.close();
}
获取文档
/**
* 测试获取文档
* @throws IOException
*/
@Test
public void testGetDoucument() throws IOException {
//创建文档查询对象,指定索引
GetRequest request = new GetRequest("boot-test", "2");
//客户端发送数据,获取响应的结果
GetResponse response = client.get(request, RequestOptions.DEFAULT);
//若存在该文档则返回该文档的所有数据,若不存在该文档则found为false且没有数据
System.out.println(response.getSourceAsString());
System.out.println(request);
client.close();
}
获取文档,判断是否存在
/**
* 测试获取文档,判断是否存在
* @throws IOException
*/
@Test
public void testGetDoucumentIsExists() throws IOException {
//创建文档查询对象,指定索引
GetRequest request = new GetRequest("boot-test", "1");
//判断文档是否存在
boolean exists = client.exists(request, RequestOptions.DEFAULT);
//存在该文档则返回为true,反之为false
System.out.println(exists);
client.close();
}
更新文档
/**
* 测试更新文档
* @throws IOException
*/
@Test
public void testUpdateDocument() throws IOException {
//创建文档更新对象,指定索引
UpdateRequest request = new UpdateRequest("boot-test", "1");
User user = new User("haha", 66);
request.doc(JSON.toJSONString(user), XContentType.JSON);
//客户端发送数据,获取响应的结果
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println(response.status());
client.close();
}
删除文档
/**
* 测试删除文档
* @throws IOException
*/
@Test
public void testDeleteDocument() throws IOException {
//创建文档删除对象,指定索引
DeleteRequest request = new DeleteRequest("boot-test", "1");
///客户端发送数据,获取响应的结果
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
System.out.println(response.status());
client.close();
}
批量添加文档
/**
* 测试批量添加数据
* @throws IOException
*/
@Test
public void testBulk() throws IOException {
//创建容器并指定索引
BulkRequest request = new BulkRequest("boot-test");
//创建集合封装文档信息
ArrayList<User> userList = new ArrayList<>();
userList.add(new User("a",1));
userList.add(new User("b",2));
userList.add(new User("c",3));
userList.add(new User("d",4));
userList.add(new User("e",5));
userList.add(new User("f",6));
userList.stream().forEach(x -> {
request.add(new IndexRequest().source(JSON.toJSONString(x), XContentType.JSON));
});
//封装好的信息放入该方法中进行批量添加---将会自动生成id
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.status());
}
批量查询文档
/**
* 测试批量查询数据
*/
@Test
public void testSearch() throws IOException {
// 创建查询请求对象
SearchRequest request = new SearchRequest("boot-test");
// 构建搜索条件对象
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 查询条件 使用QueryBuilders工具类创建
// 匹配查询
MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("name", "a");
sourceBuilder.query(matchQuery);
// 设置高亮
sourceBuilder.highlighter(new HighlightBuilder());
// 添加条件到请求
request.source(sourceBuilder);
// 客户端发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查看返回结果
SearchHits hits = response.getHits();
System.out.println(JSON.toJSONString(hits));
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsMap());
}
}