fix: sortWeight应用到es

This commit is contained in:
liu.zixi
2025-12-10 19:56:06 +08:00
parent eba13bb602
commit ad1f02c8ab
2 changed files with 59 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
package com.xboe.module.course.api;
import com.xboe.core.JsonResponse;
import com.xboe.core.api.ApiBaseController;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.WrapperQueryBuilder;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.UpdateByQueryRequest;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.Collections;
@RestController
@RequestMapping("/xboe/m/course/manage")
@Slf4j
public class CourseEsApi extends ApiBaseController {
@Autowired(required = false)
private RestHighLevelClient restHighLevelClient;
@GetMapping("/es/sort")
public JsonResponse updateSortWeight() {
UpdateByQueryRequest request = new UpdateByQueryRequest("new_resource_list");
// 使用 Painless 脚本为文档添加 sortWeight 字段
Script script = new Script(
ScriptType.INLINE,
"painless",
"ctx._source.sortWeight = 9999",
Collections.emptyMap()
);
request.setScript(script);
// 可选:只更新那些还没有 sortWeight 字段的文档(避免覆盖已有值)
String query = "{ \"bool\": { \"must_not\": { \"exists\": { \"field\": \"sortWeight\" } } } }";
request.setQuery(new WrapperQueryBuilder(query));
try {
BulkByScrollResponse response = restHighLevelClient.updateByQuery(request, RequestOptions.DEFAULT);
return success(response.getUpdated());
} catch (IOException e) {
log.error("更新索引失败", e);
return error("更新索引失败");
}
}
}

View File

@@ -27,6 +27,11 @@ public class CourseToCourseFullText {
cft.setDevice(c.getDevice());
cft.setIsTop(c.getIsTop()? 1:0);
if (c.getSortWeight() != null) {
cft.setSortWeight(c.getSortWeight());
} else {
cft.setSortWeight(9999);
}
cft.setKeywords(c.getKeywords());
//DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//Long second = c.getPublishTime().toEpochSecond(ZoneOffset.of("+8"));