以下是基于自己的项目中实际使用的java实现,版本为5.5.0
@Autowired
private ElasticsearchOperations esTemplate;
引入pom依赖
<properties>
<elasticsearch.version>5.5.0</elasticsearch.version>
<es.rest.client>5.6.0</es.rest.client>
</properties>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
1.简单查找
传参格式可以根据自己封装的定义,此处以json为例:
JSONObject obj = new JSONObject();
obj.put("id",123);
obj.put("name","你好");
//id为主键,一定要加在里面
主要调用的es方法
AggregatedPage<JSONObject> aggregatedPage= (AggregatedPage) esTemplate.queryForPage(nativeSearchQuery,JSONObject.class);
2.复杂查询
传参格式为嵌套:
//只能传嵌套格式,我项目中定义的filter一定要转义(也可以在底层转义好)
{
"page": {
"pageNo": 1,
"pageSize": "2000",
"orderBy": "id",
"order": "asc"
},
"filter": "{\"OR\":[{\"EQI_id\":\"1001\"},{\"EQI_name\":\"新项目\"}]}",
// 查询id为1001的数据,或者name为新项目的数据
"fun": "searchWithComplexQuery",
"resultFields": "id,name",
"indexName": "XXXX" //es的索引名称,通常定义为别名
}
主要调用的es方法
AggregatedPage<JSONObject> aggregatedPage= (AggregatedPage) esTemplate.queryForPage(nativeSearchQuery,JSONObject.class);
2.更新部分字段
@Override
public void updateIndex(String indexName, JSONObject pojo, boolean needTransSuc) {
// IndexQuery indexQuery = new IndexQueryBuilder()
// .withId(pojo.getString("id"))
// //JSONObject.toJSONString(pojo, FilterUtil.filter)
// .withObject(pojo)
// .withIndexName(indexName)
// .withType(indexName)
// .build();
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.doc(pojo);
UpdateQuery updateQuery=new UpdateQueryBuilder()
.withId(pojo.getString("id"))
.withUpdateRequest(updateRequest)
.withIndexName(indexName)
.withType(indexName)
.withDoUpsert(true)
.build();
if(needTransSuc){
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
// esTemplate.index(indexQuery);
esTemplate.update(updateQuery);
}
});
}else{
esTemplate.update(updateQuery);
}
log.info("updateIndexPartial:[{}]",updateRequest.doc());
}
3.更新全部字段
@Override
public void updateBusinessProjectIndex(Long projectId) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
entityManager.clear();
//构造索引整体数据
ProjectIndex projectIndex = projectService.fillProjectIndex(projectId);
/**
* 最终调用{@link org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository#save(Object)}
*/
projectIndex=projectIndexRepository.save(projectIndex);
log.info("[updateProjectIndex index] [{}]",projectIndex);
}
});
}
4.批量更新
@Override
public void bulkUpdateIndexData(String indexName, List<JSONObject> datas) {
List<UpdateQuery> updateQueryList = datas.stream().map(pojo -> {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.doc(pojo);
// log.info("bulkUpdateIndexPartial:[{}]",updateRequest.doc());
UpdateQuery updateQuery = new UpdateQueryBuilder()
.withId(pojo.getString("id"))
.withUpdateRequest(updateRequest)
.withIndexName(indexName)
.withType(indexName)
.withDoUpsert(true)
.build();
return updateQuery;
}).collect(Collectors.toList());
if(!CollectionUtils.isEmpty(updateQueryList)) {
esTemplate.bulkUpdate(updateQueryList);
esTemplate.refresh(indexName);
}
log.info("bulkUpdateIndexPartial:[{}]",datas);
}
5.插入/新增
同样用updateBusinessProjectIndex接口
6.删除
@Override
public void deleteIndex(Long id,String indexName,boolean needTransSuc){
if(needTransSuc){
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {//dugoud shijierucianjing
esTemplate.delete(indexName,indexName,id.toString());
}
});
}else{
esTemplate.delete(indexName,indexName,id.toString());
}
log.info("[delete index] [index:{}] [id:{}]",indexName,id);
}
7.刷新
esTemplate.refresh();
由于es默认是1s刷新,当update之后需要重新查询使用时,一定会出现查了之后还没有更新完成的现象。此时,调用refresh()后,可以手动刷新。再查询时一定可以查到最新的数据。