|
|
@@ -30,6 +30,8 @@ import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
|
|
import co.elastic.clients.elasticsearch._types.ElasticsearchException;
|
|
|
import co.elastic.clients.elasticsearch._types.FieldValue;
|
|
|
import co.elastic.clients.elasticsearch._types.SearchType;
|
|
|
+import co.elastic.clients.elasticsearch._types.SortOptions;
|
|
|
+import co.elastic.clients.elasticsearch._types.SortOrder;
|
|
|
import co.elastic.clients.elasticsearch._types.mapping.Property;
|
|
|
import co.elastic.clients.elasticsearch._types.mapping.TypeMapping;
|
|
|
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
|
|
|
@@ -53,6 +55,7 @@ import co.elastic.clients.json.JsonData;
|
|
|
|
|
|
import ss.AppB;
|
|
|
import ss.log.SsLog;
|
|
|
+import ss.obj.ObjB;
|
|
|
|
|
|
|
|
|
public class Fts implements AutoCloseable {
|
|
|
@@ -99,6 +102,13 @@ public class Fts implements AutoCloseable {
|
|
|
|
|
|
|
|
|
final String indexName = rdIdxName(objectName);
|
|
|
+ // 唯一性排序兜底:按对象主键倒序,保证 ES 的 from/size 分页跨页稳定,并与首页 DB 的 order by id desc 保持一致。Lin
|
|
|
+ final List<SortOptions> sortOpts = new ArrayList<>();
|
|
|
+ if (StringUtils.isNotBlank(objectName)) {
|
|
|
+ ObjB sortObj = AppB.objMap.get(objectName);
|
|
|
+ if (sortObj != null && StringUtils.isNotBlank(sortObj.idName))
|
|
|
+ sortOpts.add(SortOptions.of(s -> s.field(f -> f.field(sortObj.idName).order(SortOrder.Desc))));
|
|
|
+ }
|
|
|
try {
|
|
|
// index = index.toLowerCase(); // 去掉 appName(index),不支持多个应用。Lin
|
|
|
|
|
|
@@ -793,13 +803,18 @@ public class Fts implements AutoCloseable {
|
|
|
.from((pageNumber -1) * rowCountPerPage)
|
|
|
.size(rowCountPerPage), HashMap.class);
|
|
|
} else {
|
|
|
- response = client.search(searchRequestBuilder -> searchRequestBuilder
|
|
|
- .index(indexName)
|
|
|
- .query(queryBuilder -> queryBuilder.bool(boolQueryBuilder.build()))
|
|
|
-// .highlight(hl)
|
|
|
- .searchType(SearchType.QueryThenFetch)
|
|
|
- .from((pageNumber -1) * rowCountPerPage)
|
|
|
- .size(rowCountPerPage), HashMap.class);
|
|
|
+ response = client.search(searchRequestBuilder -> {
|
|
|
+ searchRequestBuilder
|
|
|
+ .index(indexName)
|
|
|
+ .query(queryBuilder -> queryBuilder.bool(boolQueryBuilder.build()))
|
|
|
+// .highlight(hl)
|
|
|
+ .searchType(SearchType.QueryThenFetch)
|
|
|
+ .from((pageNumber -1) * rowCountPerPage)
|
|
|
+ .size(rowCountPerPage);
|
|
|
+ if (!(sortOpts.isEmpty()))
|
|
|
+ searchRequestBuilder.sort(sortOpts);
|
|
|
+ return searchRequestBuilder;
|
|
|
+ }, HashMap.class);
|
|
|
}
|
|
|
|
|
|
/* ES5 > ES8。Lin
|