springboot 整合 Elasticsearch 增删改查代码
时间: 2024-02-29 13:24:18 浏览: 148
好的,以下是一个简单的Spring Boot整合Elasticsearch实现增删改查的示例代码:
插入数据:
```
@Autowired
private ElasticsearchTemplate esTemplate;
IndexQuery query = new IndexQueryBuilder()
.withId("1")
.withObject(new User("John", "Doe"))
.build();
String documentId = esTemplate.index(query);
```
查询数据:
```
@Autowired
private ElasticsearchTemplate esTemplate;
GetQuery query = new GetQuery();
query.setId("1");
User user = esTemplate.queryForObject(query, User.class);
```
更新数据:
```
@Autowired
private ElasticsearchTemplate esTemplate;
IndexQuery query = new IndexQueryBuilder()
.withId("1")
.withObject(new User("Jane", "Doe"))
.build();
esTemplate.index(query);
```
删除数据:
```
@Autowired
private ElasticsearchTemplate esTemplate;
DeleteQuery query = new DeleteQuery();
query.setId("1");
esTemplate.delete(query, User.class);
```
注意:这只是一个简单的示例代码,实际应用中可能需要更复杂的查询和数据处理。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)