This commit is contained in:
daihh
2022-12-30 17:22:01 +08:00
parent ccb0948516
commit 06831f18af
3 changed files with 28 additions and 1 deletions

View File

@@ -40,6 +40,9 @@ public interface ICourseFullTextSearch {
*/
boolean checkHas(String indexName,String id);
boolean reCreateIndex(String indexName) throws Exception;
/**
* 更新一个字段
* @param indexName

View File

@@ -74,6 +74,18 @@ public class CourseElasticsearchImpl implements ICourseFullTextSearch{
* @param indexName
* @return
*/
@Override
public boolean reCreateIndex(String indexName) throws Exception{
boolean isExists = esUtil.isIndexExists(indexName);
if(!isExists) {
boolean ok =createIndex(indexName);
if(!ok) {
log.error("创建索引【"+indexName+"】失败");
return false;
}
}
return true;
}
/**
* 创建索引
@@ -99,7 +111,7 @@ public class CourseElasticsearchImpl implements ICourseFullTextSearch{
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
builder.field("properties").startObject();
builder.field("id").startObject().field("index", "true").field("type", "text").endObject();
builder.field("id").startObject().field("index", "true").field("type", "keyword").endObject();
//builder.field("name").startObject().field("type", "text").field("analyzer", "ik_max_word").field("search_analyzer","ik_smart").endObject();
builder.field("name").startObject().field("type", "keyword").endObject();
builder.field("keywords").startObject().field("type", "text").field("analyzer", "ik_max_word").field("search_analyzer","ik_smart").endObject();
@@ -208,6 +220,15 @@ public class CourseElasticsearchImpl implements ICourseFullTextSearch{
@Override
public String republish(String indexName, CourseFullText item, String fullTextId) throws Exception{
boolean isExists = esUtil.isIndexExists(indexName);
if(!isExists) {
boolean ok =createIndex(indexName);
if(!ok) {
log.error("创建索引【"+indexName+"】失败");
return null;
}
}
String fullId=fullTextId;
boolean exists=false;

View File

@@ -55,6 +55,9 @@ public class CourseFullTextApi extends ApiBaseController{
if(fullTextSearch==null) {
return error("初始化失败:未实现全文检索功能");
}
//重新创建索引,如果已经存在,不会重新建
fullTextSearch.reCreateIndex(ICourseFullTextSearch.DEFAULT_INDEX_NAME);
//提取课程信息,因为现在课程没有太多,所以一次性全部查出来,
CourseQueryDto dto =new CourseQueryDto();
dto.setPublish(true);//必须是已发布的