mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/per-boe/java-servers.git
synced 2025-12-11 11:56:50 +08:00
fix: sortWeight应用到es
This commit is contained in:
@@ -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("更新索引失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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"));
|
||||
|
||||
Reference in New Issue
Block a user