学习课程ES增加了按id删除

This commit is contained in:
daihh
2023-01-09 12:09:01 +08:00
parent 4062cb7c8e
commit e33e292fdb
4 changed files with 51 additions and 9 deletions

View File

@@ -31,6 +31,9 @@ import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
@@ -242,9 +245,38 @@ public class CourseStudyElasticsearchImpl implements ICourseStudySearch{
}
@Override
public void updateStatusAndProcessByDocId(String docId, int status, int process) throws Exception {
public void updateStatusAndProcessByDocId(String docId, int status, int progress) throws Exception {
//更新状态和进度
UpdateRequest updateRequest = new UpdateRequest(IndexName, docId);
ObjectMapper mapper=new ObjectMapper();
Map<String,Object> map=new HashMap<String,Object>();
map.put("status",status);
map.put("progress",progress);
String textJson =mapper.writeValueAsString(map);
updateRequest.doc(textJson, XContentType.JSON);
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) {
log.info("更新课程学习状态进度成功!");
} else {
log.error("更新课程学习状态进度失败");
}
}
@Override
public void removeByStudyId(String id) {
//根据学习id删除
try {
DeleteByQueryRequest deleteRequest=new DeleteByQueryRequest(IndexName);
deleteRequest.setQuery(new TermQueryBuilder("id",id));
BulkByScrollResponse response = restHighLevelClient.deleteByQuery(deleteRequest, RequestOptions.DEFAULT);
long n=response.getStatus().getTotal();
if(n==0) {
log.error("按id删除失败",response);
}
}catch(Exception e){
log.error("按id【"+id+"】删除失败",e);
}
}
}