mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/per-boe/java-servers.git
synced 2025-12-07 18:06:50 +08:00
提交ES的修改
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.xboe.module.course.service;
|
package com.xboe.module.course.service;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.xboe.common.PageList;
|
import com.xboe.common.PageList;
|
||||||
@@ -55,7 +56,10 @@ public interface ICourseFullTextSearch {
|
|||||||
* @param indexName
|
* @param indexName
|
||||||
* @param id
|
* @param id
|
||||||
*/
|
*/
|
||||||
void remove(String indexName,String id) throws Exception;
|
void removeByDocId(String indexName,String id) throws Exception;
|
||||||
|
|
||||||
|
|
||||||
|
boolean deleteById(String indexName, String id) throws IOException;
|
||||||
|
|
||||||
// /**
|
// /**
|
||||||
// * 重新发布到全文检索
|
// * 重新发布到全文检索
|
||||||
|
|||||||
@@ -36,6 +36,9 @@ import org.elasticsearch.common.xcontent.XContentType;
|
|||||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||||
import org.elasticsearch.index.query.QueryBuilders;
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
import org.elasticsearch.index.query.RangeQueryBuilder;
|
import org.elasticsearch.index.query.RangeQueryBuilder;
|
||||||
|
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.SearchHit;
|
||||||
import org.elasticsearch.search.SearchHits;
|
import org.elasticsearch.search.SearchHits;
|
||||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||||
@@ -220,10 +223,11 @@ public class CourseElasticsearchImpl implements ICourseFullTextSearch{
|
|||||||
String textJson =mapper.writeValueAsString(item);
|
String textJson =mapper.writeValueAsString(item);
|
||||||
updateRequest.doc(textJson, XContentType.JSON);
|
updateRequest.doc(textJson, XContentType.JSON);
|
||||||
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
|
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
|
||||||
if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) {
|
int ok=updateResponse.status().getStatus();
|
||||||
|
if (ok==200) {
|
||||||
log.info("更新【"+fullTextId+"】成功!");
|
log.info("更新【"+fullTextId+"】成功!");
|
||||||
} else {
|
} else {
|
||||||
log.error("更新【"+fullTextId+"】失败");
|
log.error("更新【"+fullTextId+"】失败",updateResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
}else {
|
}else {
|
||||||
@@ -377,7 +381,8 @@ public class CourseElasticsearchImpl implements ICourseFullTextSearch{
|
|||||||
BoolQueryBuilder keywordQuery = QueryBuilders.boolQuery();
|
BoolQueryBuilder keywordQuery = QueryBuilders.boolQuery();
|
||||||
keywordQuery.should(QueryBuilders.wildcardQuery("name", "*"+words+"*").boost(9f));
|
keywordQuery.should(QueryBuilders.wildcardQuery("name", "*"+words+"*").boost(9f));
|
||||||
//keywordQuery.should(QueryBuilders.queryStringQuery(words).field("name", 9f));//用此方法无法查询出有转义符的处理
|
//keywordQuery.should(QueryBuilders.queryStringQuery(words).field("name", 9f));//用此方法无法查询出有转义符的处理
|
||||||
keywordQuery.should(QueryBuilders.queryStringQuery(words).field("teacher", 8f));
|
//keywordQuery.should(QueryBuilders.queryStringQuery(words).field("teacher", 8f));
|
||||||
|
keywordQuery.should(QueryBuilders.wildcardQuery("name", "*"+words+"*").boost(9f));
|
||||||
keywordQuery.minimumShouldMatch(1);
|
keywordQuery.minimumShouldMatch(1);
|
||||||
boolQuery.must(keywordQuery);
|
boolQuery.must(keywordQuery);
|
||||||
//boolQuery.must(QueryBuilders.wildcardQuery("name", "*"+params.getKeywords()+"*").boost(9f));
|
//boolQuery.must(QueryBuilders.wildcardQuery("name", "*"+params.getKeywords()+"*").boost(9f));
|
||||||
@@ -682,7 +687,7 @@ public class CourseElasticsearchImpl implements ICourseFullTextSearch{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(String indexName, String id) {
|
public void removeByDocId(String indexName, String id) {
|
||||||
DeleteRequest deleteRequest = new DeleteRequest(indexName);
|
DeleteRequest deleteRequest = new DeleteRequest(indexName);
|
||||||
deleteRequest.id(id);
|
deleteRequest.id(id);
|
||||||
try {
|
try {
|
||||||
@@ -690,9 +695,26 @@ public class CourseElasticsearchImpl implements ICourseFullTextSearch{
|
|||||||
if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
|
if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
|
||||||
log.error("删除失败,未找到索引id是【"+id+"】的文档");
|
log.error("删除失败,未找到索引id是【"+id+"】的文档");
|
||||||
}
|
}
|
||||||
|
//restHighLevelClient.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("删除异常:"+e.getMessage());
|
log.error("删除异常:"+e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean deleteById(String indexName, String id) throws IOException {
|
||||||
|
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);
|
||||||
|
return true;
|
||||||
|
}else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//restHighLevelClient.close();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,9 +79,6 @@ public class CourseFullTextApi extends ApiBaseController{
|
|||||||
for(int i=0;i<crowds.size();i++) {
|
for(int i=0;i<crowds.size();i++) {
|
||||||
crowdIds[i]=crowds.get(i).getGroupId();
|
crowdIds[i]=crowds.get(i).getGroupId();
|
||||||
}
|
}
|
||||||
if(crowdIds.length>1) {
|
|
||||||
System.out.println("课程["+cft.getName()+"]内容:"+crowdIds[0]+","+crowdIds[1]);
|
|
||||||
}
|
|
||||||
//String audienceIds=StringUtils.join(crowdIds,",");
|
//String audienceIds=StringUtils.join(crowdIds,",");
|
||||||
//log.info("受众信息",audienceIds);
|
//log.info("受众信息",audienceIds);
|
||||||
//cft.setAudience(audienceIds);
|
//cft.setAudience(audienceIds);
|
||||||
@@ -107,6 +104,82 @@ public class CourseFullTextApi extends ApiBaseController{
|
|||||||
return success(true);
|
return success(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/delete-by-id")
|
||||||
|
public JsonResponse<Boolean> deleteById(String id) throws Exception{
|
||||||
|
if(fullTextSearch==null){
|
||||||
|
return error("初始化失败:未实现全文检索功能");
|
||||||
|
}
|
||||||
|
if(StringUtils.isBlank(id)) {
|
||||||
|
return error("未指定id");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
boolean ok=fullTextSearch.deleteById(ICourseFullTextSearch.DEFAULT_INDEX_NAME, id);
|
||||||
|
return success(ok);
|
||||||
|
}catch(Exception e) {
|
||||||
|
log.error("根据课程id删除全文检索课程失败",e);
|
||||||
|
return error("删除失败",e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@PostMapping("/republish")
|
||||||
|
public JsonResponse<Boolean> republish(String id) throws Exception{
|
||||||
|
|
||||||
|
if(fullTextSearch==null) {
|
||||||
|
return error("初始化失败:未实现全文检索功能");
|
||||||
|
}
|
||||||
|
if(StringUtils.isBlank(id)) {
|
||||||
|
return error("未指定id");
|
||||||
|
}
|
||||||
|
//提取课程信息,因为现在课程没有太多,所以一次性全部查出来,
|
||||||
|
Course c =courseService.get(id);
|
||||||
|
if(c.getPublished()==null || !c.getPublished()) {
|
||||||
|
return error("此课程还未发布,请发布课程,不用在这里重新发布");
|
||||||
|
}
|
||||||
|
|
||||||
|
CourseFullText cft=CourseToCourseFullText.convert(c);
|
||||||
|
try {
|
||||||
|
//计算课程时长
|
||||||
|
int duration=ccourseService.sumDurationByCourseId(c.getId());
|
||||||
|
cft.setDuration(duration);
|
||||||
|
|
||||||
|
//查询教师
|
||||||
|
CourseTeacherDto teacherDto = courseTeacherService.getTeacherByCourseId(c.getId());
|
||||||
|
if(teacherDto!=null && !teacherDto.getNames().isEmpty()) {
|
||||||
|
cft.setTeacher(StringUtils.join(teacherDto.getNames(),","));
|
||||||
|
}
|
||||||
|
//查询课程受众
|
||||||
|
List<CourseCrowd> crowds = courseService.findCrowdByCourseId(c.getId());
|
||||||
|
if(crowds!=null && !crowds.isEmpty()) {
|
||||||
|
String[] crowdIds=new String[crowds.size()];
|
||||||
|
for(int i=0;i<crowds.size();i++) {
|
||||||
|
crowdIds[i]=crowds.get(i).getGroupId();
|
||||||
|
}
|
||||||
|
// if(crowdIds.length>1) {
|
||||||
|
// log.info("课程["+cft.getName()+"]内容:"+crowdIds[0]+","+crowdIds[1]);
|
||||||
|
// }
|
||||||
|
//String audienceIds=StringUtils.join(crowdIds,",");
|
||||||
|
//log.info("受众信息",audienceIds);
|
||||||
|
//cft.setAudience(audienceIds);
|
||||||
|
cft.setAudiences(crowdIds);
|
||||||
|
cft.setIsSetAudience(1);//有受众
|
||||||
|
}else {
|
||||||
|
cft.setIsSetAudience(0);//无受众
|
||||||
|
}
|
||||||
|
|
||||||
|
String fullTextId=fullTextSearch.republish(ICourseFullTextSearch.DEFAULT_INDEX_NAME, cft,c.getFullTextId());
|
||||||
|
log.info("发布课程全文检索成功:"+fullTextId);
|
||||||
|
c.setFullTextId(fullTextId);
|
||||||
|
if(StringUtils.isNotBlank(fullTextId)) {
|
||||||
|
courseService.updateFieldById(c.getId(),"fullTextId", fullTextId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}catch(Exception e) {
|
||||||
|
log.error("发布课程【"+c.getName()+"("+c.getId()+")】全文检索及更新数据库失败",e);
|
||||||
|
return error("重新发布失败",e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return success(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 全文检索查询
|
* 全文检索查询
|
||||||
|
|||||||
@@ -460,7 +460,7 @@ public class CourseServiceImpl implements ICourseService {
|
|||||||
|
|
||||||
if(StringUtils.isNotBlank(course.getFullTextId())) {
|
if(StringUtils.isNotBlank(course.getFullTextId())) {
|
||||||
try {
|
try {
|
||||||
fullTextSearch.remove(ICourseFullTextSearch.DEFAULT_INDEX_NAME,course.getFullTextId());
|
fullTextSearch.removeByDocId(ICourseFullTextSearch.DEFAULT_INDEX_NAME,course.getFullTextId());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
//log.error("删除课程时删除全文索引错误",e);
|
//log.error("删除课程时删除全文索引错误",e);
|
||||||
}
|
}
|
||||||
@@ -484,7 +484,7 @@ public class CourseServiceImpl implements ICourseService {
|
|||||||
//删除分两种情况,一个是管理员删除,一个是自己删除 ,所以这里的处理移到前端了
|
//删除分两种情况,一个是管理员删除,一个是自己删除 ,所以这里的处理移到前端了
|
||||||
if(c.getFullTextId()!=null) {
|
if(c.getFullTextId()!=null) {
|
||||||
try {
|
try {
|
||||||
fullTextSearch.remove(ICourseFullTextSearch.DEFAULT_INDEX_NAME,c.getFullTextId());
|
fullTextSearch.removeByDocId(ICourseFullTextSearch.DEFAULT_INDEX_NAME,c.getFullTextId());
|
||||||
}catch(Exception e) {
|
}catch(Exception e) {
|
||||||
log.error("删除课程时删除全文索引错误",e);
|
log.error("删除课程时删除全文索引错误",e);
|
||||||
}
|
}
|
||||||
@@ -661,7 +661,7 @@ public class CourseServiceImpl implements ICourseService {
|
|||||||
//更新后需要删除发布
|
//更新后需要删除发布
|
||||||
if(StringUtils.isNotBlank(c.getFullTextId())) {
|
if(StringUtils.isNotBlank(c.getFullTextId())) {
|
||||||
try {
|
try {
|
||||||
fullTextSearch.remove(ICourseFullTextSearch.DEFAULT_INDEX_NAME,c.getFullTextId());
|
fullTextSearch.removeByDocId(ICourseFullTextSearch.DEFAULT_INDEX_NAME,c.getFullTextId());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
//log.error("删除课程时删除全文索引错误",e);
|
//log.error("删除课程时删除全文索引错误",e);
|
||||||
}
|
}
|
||||||
@@ -903,7 +903,7 @@ public class CourseServiceImpl implements ICourseService {
|
|||||||
Object fullId=courseDao.findField("fullTextId", FieldFilters.eq("id", id));
|
Object fullId=courseDao.findField("fullTextId", FieldFilters.eq("id", id));
|
||||||
if(fullId!=null) {
|
if(fullId!=null) {
|
||||||
try {
|
try {
|
||||||
fullTextSearch.remove(ICourseFullTextSearch.DEFAULT_INDEX_NAME, (String)fullId);
|
fullTextSearch.removeByDocId(ICourseFullTextSearch.DEFAULT_INDEX_NAME, (String)fullId);
|
||||||
}catch(Exception e) {
|
}catch(Exception e) {
|
||||||
log.error("更新全文索引字段错误",e);
|
log.error("更新全文索引字段错误",e);
|
||||||
}
|
}
|
||||||
@@ -953,7 +953,7 @@ public class CourseServiceImpl implements ICourseService {
|
|||||||
Object fullId=courseDao.findField("fullTextId", FieldFilters.eq("id", id));
|
Object fullId=courseDao.findField("fullTextId", FieldFilters.eq("id", id));
|
||||||
if(fullId!=null) {
|
if(fullId!=null) {
|
||||||
try {
|
try {
|
||||||
fullTextSearch.remove(ICourseFullTextSearch.DEFAULT_INDEX_NAME, (String)fullId);
|
fullTextSearch.removeByDocId(ICourseFullTextSearch.DEFAULT_INDEX_NAME, (String)fullId);
|
||||||
}catch(Exception e) {
|
}catch(Exception e) {
|
||||||
log.error("停用,从索引中删除课程失败",e);
|
log.error("停用,从索引中删除课程失败",e);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user