|
|
@@ -16,6 +16,8 @@ import org.apache.lucene.search.BooleanQuery;
|
|
|
import org.apache.lucene.search.IndexSearcher;
|
|
|
import org.apache.lucene.search.NumericRangeQuery;
|
|
|
import org.apache.lucene.search.ScoreDoc;
|
|
|
+import org.apache.lucene.search.Sort;
|
|
|
+import org.apache.lucene.search.SortField;
|
|
|
import org.apache.lucene.search.TopDocs;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
@@ -51,6 +53,7 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
|
|
|
private Logger logger = Logger.getLogger(getClass());
|
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
@Override
|
|
|
public SPage<Long> searchIds(String keyword, Table_name tableName, PageParams pageParams) throws SearchException {
|
|
|
IndexSearcher indexSearcher = SearchUtils.getIndexSearcher();
|
|
|
@@ -59,6 +62,7 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
|
|
|
SPage<Long> sPage = new SPage<>();
|
|
|
BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
+ Sort sort = null;
|
|
|
|
|
|
// 关键词无效,不添加搜索条件(即搜索所有的数据)
|
|
|
if (!SearchUtils.isKeywordInvalid(keyword)) {
|
|
|
@@ -117,6 +121,24 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
fromTime, endTime, true, true), BooleanClause.Occur.MUST);
|
|
|
}
|
|
|
|
|
|
+ // 如果需要排序
|
|
|
+ if (!StringUtils.isEmpty(filters.get(com.uas.search.b2b.util.SearchConstants.SORT_KEY))) {
|
|
|
+ Object sortObject = filters.remove(com.uas.search.b2b.util.SearchConstants.SORT_KEY);
|
|
|
+ List<com.uas.search.b2b.model.Sort> sortList = null;
|
|
|
+ if (sortObject instanceof ArrayList) {
|
|
|
+ sortList = (List<com.uas.search.b2b.model.Sort>) sortObject;
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(sortList)) {
|
|
|
+ SortField[] sortFields = new SortField[sortList.size()];
|
|
|
+ for (int i = 0; i < sortList.size(); i++) {
|
|
|
+ com.uas.search.b2b.model.Sort s = sortList.get(i);
|
|
|
+ sortFields[i] = new SortField(ClassAndTableNameUtils.combineField(tableName, s.getField()),
|
|
|
+ getType(s), s.isReverse());
|
|
|
+ }
|
|
|
+ sort = new Sort(sortFields);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 其他过滤条件,键即为相应的数据库字段名(field名)
|
|
|
Set<Entry<String, Object>> entrySet = filters.entrySet();
|
|
|
for (Entry<String, Object> entry : entrySet) {
|
|
|
@@ -130,7 +152,6 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
* 作为filters中key-value的value传递过来
|
|
|
*/
|
|
|
if (value instanceof ArrayList) {
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
List<Object> list = (List<Object>) value;
|
|
|
for (Object object : list) {
|
|
|
booleanQuery.add(SearchUtils.getBooleanQuery(field, String.valueOf(object)),
|
|
|
@@ -150,17 +171,31 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
TopDocs topDocs;
|
|
|
// 如果页码不为1
|
|
|
if (sPage.getPage() > 1) {
|
|
|
- TopDocs previousTopDocs = indexSearcher.search(booleanQuery, (sPage.getPage() - 1) * sPage.getSize());
|
|
|
+ TopDocs previousTopDocs = null;
|
|
|
+ if (sort != null) {
|
|
|
+ previousTopDocs = indexSearcher.search(booleanQuery, (sPage.getPage() - 1) * sPage.getSize(), sort);
|
|
|
+ } else {
|
|
|
+ previousTopDocs = indexSearcher.search(booleanQuery, (sPage.getPage() - 1) * sPage.getSize());
|
|
|
+ }
|
|
|
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], booleanQuery,
|
|
|
- sPage.getSize());
|
|
|
+ if (sort != null) {
|
|
|
+ topDocs = indexSearcher.searchAfter(previousScoreDocs[previousScoreDocs.length - 1], booleanQuery,
|
|
|
+ sPage.getSize(), sort);
|
|
|
+ } else {
|
|
|
+ topDocs = indexSearcher.searchAfter(previousScoreDocs[previousScoreDocs.length - 1], booleanQuery,
|
|
|
+ sPage.getSize());
|
|
|
+ }
|
|
|
} else {
|
|
|
sPage.setFirst(true);
|
|
|
- topDocs = indexSearcher.search(booleanQuery, sPage.getSize());
|
|
|
+ if (sort != null) {
|
|
|
+ topDocs = indexSearcher.search(booleanQuery, sPage.getSize(), sort);
|
|
|
+ } else {
|
|
|
+ topDocs = indexSearcher.search(booleanQuery, sPage.getSize());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
int totalHits = topDocs.totalHits;
|
|
|
@@ -200,4 +235,30 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取排序字段的类型
|
|
|
+ *
|
|
|
+ * @param s
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private SortField.Type getType(com.uas.search.b2b.model.Sort s) {
|
|
|
+ if (s == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ com.uas.search.b2b.model.Sort.Type type = s.getType();
|
|
|
+ if (type == com.uas.search.b2b.model.Sort.Type.STRING) {
|
|
|
+ return SortField.Type.STRING;
|
|
|
+ } else if (type == com.uas.search.b2b.model.Sort.Type.INT) {
|
|
|
+ return SortField.Type.INT;
|
|
|
+ } else if (type == com.uas.search.b2b.model.Sort.Type.FLOAT) {
|
|
|
+ return SortField.Type.FLOAT;
|
|
|
+ } else if (type == com.uas.search.b2b.model.Sort.Type.LONG) {
|
|
|
+ return SortField.Type.LONG;
|
|
|
+ } else if (type == com.uas.search.b2b.model.Sort.Type.DOUBLE) {
|
|
|
+ return SortField.Type.DOUBLE;
|
|
|
+ } else {
|
|
|
+ throw new SearchException("排序参数错误:" + type);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|