|
|
@@ -226,6 +226,24 @@ public class SearchUtils {
|
|
|
* @return
|
|
|
*/
|
|
|
public static SPage<Document> getDocuments(String tableName, Query query, Integer page, Integer size) {
|
|
|
+ return getDocuments(tableName, query, null, page, size);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据查询条件获取指定表的Document列表
|
|
|
+ *
|
|
|
+ * @param tableName
|
|
|
+ * 表名
|
|
|
+ * @param query
|
|
|
+ * 查询条件
|
|
|
+ * @param sort 排序规则
|
|
|
+ * @param page
|
|
|
+ * 页码
|
|
|
+ * @param size
|
|
|
+ * 页大小
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static SPage<Document> getDocuments(String tableName, Query query, Sort sort, Integer page, Integer size) {
|
|
|
if (query == null) {
|
|
|
throw new SearchException("query不能为null");
|
|
|
}
|
|
|
@@ -247,18 +265,19 @@ public class SearchUtils {
|
|
|
try {
|
|
|
// 如果页码不为1
|
|
|
if (sPage.getPage() > 1) {
|
|
|
- TopDocs previousTopDocs = null;
|
|
|
- previousTopDocs = indexSearcher.search(query, (sPage.getPage() - 1) * sPage.getSize());
|
|
|
+ TopDocs previousTopDocs;
|
|
|
+ previousTopDocs = sort == null ? indexSearcher.search(query, (sPage.getPage() - 1) * sPage.getSize()) :
|
|
|
+ indexSearcher.search(query, (sPage.getPage() - 1) * sPage.getSize(), sort);
|
|
|
int totalHits = previousTopDocs.totalHits;
|
|
|
ScoreDoc[] previousScoreDocs = previousTopDocs.scoreDocs;
|
|
|
if ((sPage.getPage() - 1) * sPage.getSize() >= totalHits) {
|
|
|
throw new SearchException("页码过大:元素总数量为" + totalHits);
|
|
|
}
|
|
|
- topDocs = indexSearcher.searchAfter(previousScoreDocs[previousScoreDocs.length - 1], query,
|
|
|
- sPage.getSize());
|
|
|
+ topDocs = sort == null ? indexSearcher.searchAfter(previousScoreDocs[previousScoreDocs.length - 1], query, sPage.getSize()) :
|
|
|
+ indexSearcher.searchAfter(previousScoreDocs[previousScoreDocs.length - 1], query, sPage.getSize(), sort);
|
|
|
} else {
|
|
|
sPage.setFirst(true);
|
|
|
- topDocs = indexSearcher.search(query, sPage.getSize());
|
|
|
+ topDocs = sort == null ? indexSearcher.search(query, sPage.getSize()) : indexSearcher.search(query, sPage.getSize(), sort);
|
|
|
}
|
|
|
|
|
|
int totalHits = topDocs.totalHits;
|