springboot 整合 Elasticsearch 增删改查代码
时间: 2024-02-29 11:24:18 浏览: 140
好的,以下是一个简单的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);
```
注意:这只是一个简单的示例代码,实际应用中可能需要更复杂的查询和数据处理。
阅读全文