sunyj 9 лет назад
Родитель
Сommit
401f962940

+ 100 - 0
search-api-b2b/src/main/java/com/uas/search/b2b/model/Sort.java

@@ -0,0 +1,100 @@
+package com.uas.search.b2b.model;
+
+/**
+ * 排序
+ * 
+ * @author sunyj
+ * @since 2016年11月23日 下午1:47:44
+ */
+public class Sort {
+
+	/**
+	 * 所排序的字段的类型
+	 */
+	public static enum Type {
+		/** 字段为字符串 */
+		STRING,
+
+		/** 字段为Integer */
+		INT,
+
+		/** 字段为Float */
+		FLOAT,
+
+		/** 字段为Long */
+		LONG,
+
+		/** 字段为Double */
+		DOUBLE;
+	}
+
+	/**
+	 * 排序的字段名称(在表中的列名)
+	 */
+	private String field;
+
+	/**
+	 * 排序默认为升序,若为true,则为降序
+	 */
+	private boolean reverse = false;
+
+	/**
+	 * 排序的字段的类型,默认为String
+	 */
+	private Type type = Type.STRING;
+
+	public Sort(String field, boolean reverse, Type type) {
+		this.field = field;
+		this.reverse = reverse;
+		this.type = type;
+	}
+
+	public Sort(String field, Type type) {
+		this.field = field;
+		this.type = type;
+	}
+
+	public Sort(String field, boolean reverse) {
+		this.field = field;
+		this.reverse = reverse;
+	}
+
+	public Sort() {
+		super();
+	}
+
+	public Sort(String field) {
+		super();
+		this.field = field;
+	}
+
+	public String getField() {
+		return field;
+	}
+
+	public void setField(String field) {
+		this.field = field;
+	}
+
+	public boolean isReverse() {
+		return reverse;
+	}
+
+	public void setReverse(boolean reverse) {
+		this.reverse = reverse;
+	}
+
+	public Type getType() {
+		return type;
+	}
+
+	public void setType(Type type) {
+		this.type = type;
+	}
+
+	@Override
+	public String toString() {
+		return "Sort [field=" + field + ", reverse=" + reverse + ", type=" + type + "]";
+	}
+
+}

+ 2 - 1
search-api-b2b/src/main/java/com/uas/search/b2b/service/SearchService.java

@@ -167,7 +167,8 @@ public interface SearchService {
 	 *            可为空,可能含有翻页信息,filters中可能有过滤信息,包括:1.状态、所属企业uu、其他状态(如已采纳、未采纳等),
 	 *            该部分参数的键为数据库表中相应的字段名称;2.开始时间(Long)、截止时间(Long),
 	 *            这两个参数用于对时间范围进行筛选(包含开始和截止时间),键为com.uas.search.b2b.util.
-	 *            SearchConstants中的常量
+	 *            SearchConstants中的常量;3.排序方式(ArrayList(Sort)),键为com.uas.
+	 *            search.b2b.util.SearchConstants中的常量,值为排序的字段与其升、降序方式等
 	 * @return 单据id
 	 * @throws SearchException
 	 */

+ 5 - 0
search-api-b2b/src/main/java/com/uas/search/b2b/util/SearchConstants.java

@@ -17,4 +17,9 @@ public class SearchConstants {
 	 * 单据搜索时,若限定时间范围,可以键值对的形式传递参数,该键代表截止时间
 	 */
 	public static final String END_DATE_KEY = "endDate";
+
+	/**
+	 * 用来对搜索结果进行排序,该键代表排序的排序方式
+	 */
+	public static final String SORT_KEY = "sort";
 }

+ 14 - 2
search-console-b2b/src/main/java/com/uas/search/console/b2b/controller/SearchController.java

@@ -1,9 +1,11 @@
 package com.uas.search.console.b2b.controller;
 
 import java.util.HashMap;
+import java.util.Map;
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
+import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
@@ -11,8 +13,10 @@ import org.springframework.web.bind.annotation.ResponseBody;
 import com.alibaba.fastjson.JSONObject;
 import com.uas.search.b2b.model.PageParams;
 import com.uas.search.b2b.model.SPage;
+import com.uas.search.b2b.model.Sort;
 import com.uas.search.b2b.service.SearchService;
 import com.uas.search.b2b.service.SearchService.Table_name;
+import com.uas.search.b2b.util.SearchConstants;
 import com.uas.search.console.b2b.core.util.ContextUtils;
 import com.uas.search.console.b2b.service.InnerSearchService;
 import com.uas.search.console.b2b.util.ClassAndTableNameUtils;
@@ -36,7 +40,8 @@ public class SearchController {
 
 	@RequestMapping("")
 	@ResponseBody
-	public SPage<Long> searchIds(String keyword, String tableName, Integer page, Integer size, String filters) {
+	public SPage<Long> searchIds(String keyword, String tableName, Integer page, Integer size, String filters,
+			String sort) {
 		Table_name tbName = null;
 		if (!StringUtils.isEmpty(tableName)) {
 			tbName = Table_name.valueOf(tableName.toUpperCase());
@@ -48,8 +53,15 @@ public class SearchController {
 		if (size != null) {
 			pageParams.setSize(size);
 		}
+		Map<String, Object> filtersMap = new HashMap<>();
 		if (!StringUtils.isEmpty(filters)) {
-			pageParams.setFilters(new HashMap<>(JSONObject.parseObject(filters)));
+			filtersMap.putAll(JSONObject.parseObject(filters));
+		}
+		if (!StringUtils.isEmpty(sort)) {
+			filtersMap.put(SearchConstants.SORT_KEY, JSONObject.parseArray(sort, Sort.class));
+		}
+		if (!CollectionUtils.isEmpty(filtersMap)) {
+			pageParams.setFilters(filtersMap);
 		}
 		return searchService.searchIds(keyword, tbName, pageParams);
 	}

+ 1 - 1
search-console-b2b/src/main/java/com/uas/search/console/b2b/service/impl/IndexServiceImpl.java

@@ -299,7 +299,7 @@ public class IndexServiceImpl implements IndexService {
 					count++;
 				}
 				size += content.size();
-				logger.info(String.format(tableName.value() + "Downloaded...................%.2f%%",
+				logger.info(String.format(tableName.value() + " downloaded...................%.2f%%",
 						size * 100.0 / totalElements));
 
 				pageParams.setPage(pageParams.getPage() + 1);

+ 66 - 5
search-console-b2b/src/main/java/com/uas/search/console/b2b/service/impl/SearchServiceImpl.java

@@ -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);
+		}
+	}
+
 }

+ 11 - 0
search-console-b2b/src/main/java/com/uas/search/console/b2b/util/ObjectToDocumentUtils.java

@@ -4,6 +4,7 @@ import org.apache.commons.collections.CollectionUtils;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.Field.Store;
 import org.apache.lucene.document.LongField;
+import org.apache.lucene.document.NumericDocValuesField;
 import org.apache.lucene.document.StringField;
 import org.apache.lucene.document.TextField;
 import org.springframework.util.StringUtils;
@@ -170,6 +171,16 @@ public class ObjectToDocumentUtils {
 					new TextField(ClassAndTableNameUtils.combineField(tableName, PurchaseOrderSimpleInfo.ITEMS_FIELD),
 							JSONObject.toJSONString(purchaseOrder.getOrderItems()), Store.YES));
 		}
+		// 排序字段
+		document.add(new NumericDocValuesField(
+				ClassAndTableNameUtils.combineField(tableName, PurchaseOrderSimpleInfo.ID_FIELD),
+				purchaseOrder.getId()));
+		document.add(new NumericDocValuesField(
+				ClassAndTableNameUtils.combineField(tableName, PurchaseOrderSimpleInfo.DATE_FIELD),
+				purchaseOrder.getDate().getTime()));
+		document.add(new NumericDocValuesField(
+				ClassAndTableNameUtils.combineField(tableName, PurchaseOrderSimpleInfo.DISPLAY_FIELD),
+				purchaseOrder.getDisplay()));
 		return document;
 	}
 

+ 1 - 1
search-console-b2b/src/main/webapp/WEB-INF/views/console.html

@@ -10,7 +10,7 @@
 		<ol>
 			<strong><li class="title">搜索</li></strong>
 			<ol>
-				<li>search?keyword=PMP150&tableName=PURC$ORDERS&filters={"pu_status":200,"pu_enuu":10041166,"fromDate":1435680000000,"endDate":1479214994000}</li>
+				<li>search?keyword=PMP150&tableName=PURC$ORDERS&filters={"pu_status":200,"pu_enuu":10041166,"fromDate":1435680000000,"endDate":1479214994000}&sort=[{"field":"pu_id","reverse":1,"type":"LONG"},{"field":"pu_date","reverse":1,"type":"LONG"}]</li>
 				<li><a target="_blank">search?keyword=PMP1506000&tableName=PURC$ORDERS</a></li>
 				<li><a target="_blank">search?keyword=MSQ150800&tableName=MAKE$ORDERS</a></li>
 			</ol>