Explorar el Código

optimize filters

sunyj hace 8 años
padre
commit
927965c4d7

+ 11 - 5
mall-search/src/main/java/com/uas/search/controller/SearchController.java

@@ -206,13 +206,17 @@ public class SearchController {
 	@RequestMapping("/goodsIds")
 	@ResponseBody
 	public Map<String, Object> getGoodsIds(String keyword, String params, HttpServletRequest request) throws IOException {
-		PageParams pageParams = params==null?null:JSONObject.parseObject(params, PageParams.class);
-		return searchService.getGoodsIds(keyword, pageParams);
-	}
+        long start = System.currentTimeMillis();
+        PageParams pageParams = params==null?null:JSONObject.parseObject(params, PageParams.class);
+        Map<String, Object> goodsIds = searchService.getGoodsIds(keyword, pageParams);
+        logger.info(String.format("goodsIds\t%s\t%.3fs", keyword, (System.currentTimeMillis()-start)/1000.0));
+        return goodsIds;
+    }
 
 	@RequestMapping("/collectBySearchGoods")
 	@ResponseBody
 	public List<Map<String, Object>> collectBySearchGoods(String keyword, @RequestParam String collectedField, String filters, HttpServletRequest request) throws IOException {
+        long start = System.currentTimeMillis();
         Map<FilterField, Object> filtersMap = new HashMap<>();
         if(!StringUtils.isEmpty(filters)){
             JSONObject json = JSONObject.parseObject(filters);
@@ -221,9 +225,11 @@ public class SearchController {
                 filtersMap.put(FilterField.valueOf(entry.getKey().toUpperCase()), entry.getValue());
             }
         }
-		return searchService.collectBySearchGoods(keyword, CollectField.valueOf(collectedField.toUpperCase()),
+        List<Map<String, Object>> maps = searchService.collectBySearchGoods(keyword, CollectField.valueOf(collectedField.toUpperCase()),
                 filtersMap);
-	}
+        logger.info(String.format("collect\t%s\t%.3fs", keyword, (System.currentTimeMillis()-start)/1000.0));
+        return maps;
+    }
 
 	@RequestMapping("/kind/{id}")
 	@ResponseBody

+ 7 - 10
mall-search/src/main/java/com/uas/search/service/impl/SearchServiceImpl.java

@@ -12,10 +12,7 @@ import com.uas.search.grouping.GoodsGroupCollector;
 import com.uas.search.model.*;
 import com.uas.search.service.SearchService;
 import com.uas.search.sort.StringFieldComparatorSource;
-import com.uas.search.util.CollectionUtils;
-import com.uas.search.util.DocumentToObjectUtils;
-import com.uas.search.util.SearchUtils;
-import com.uas.search.util.StringUtils;
+import com.uas.search.util.*;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.index.Term;
 import org.apache.lucene.search.*;
@@ -885,9 +882,9 @@ public class SearchServiceImpl implements SearchService {
 			for (ScoreDoc scoreDoc : scoreDocs) {
 				Document document = indexSearcher.doc(scoreDoc.doc, fieldsToLoad);
                 String cmpId = document.get(SearchConstants.GOODS_CMP_ID_FIELD);
-				cmpIds.add(StringUtils.isEmpty(cmpId) ? null : Long.valueOf(cmpId));
+				cmpIds.add(StringUtils.isEmpty(cmpId) || cmpId.equals(ObjectToDocumentUtils.NULL_VALUE) ? null : Long.valueOf(cmpId));
 				String goId = document.get(SearchConstants.GOODS_GO_ID_FIELD);
-				goIds.add(StringUtils.isEmpty(goId) ? null : Long.valueOf(goId));
+				goIds.add(StringUtils.isEmpty(goId) || goId.equals(ObjectToDocumentUtils.NULL_VALUE) ? null : Long.valueOf(goId));
 			}
 			map.put("componentIds", cmpIds);
 			map.put("goodsIds", goIds);
@@ -944,14 +941,14 @@ public class SearchServiceImpl implements SearchService {
             // 如果未明确指定状态,则使用默认状态分情况进行过滤(结果中包括没有批次的器件)
             status = Arrays.asList(TradeGoods.VALID_STATUS);
             // 批次 id 不为空时,对状态过滤
-            Query goIdNotNullQuery = SearchUtils.getNotNullQuery(SearchConstants.GOODS_GO_ID_FIELD);
+            Query goNullQuery = SearchUtils.getNullQuery(SearchConstants.GOODS_GO_ID_FIELD);
             BooleanQuery q1 = new BooleanQuery();
-            q1.add(goIdNotNullQuery, Occur.MUST);
+            q1.add(goNullQuery, Occur.MUST_NOT);
             filter(status, SearchConstants.GOODS_GO_STATUS_FIELD, q1);
             // 或者批次 id 为空(此时是器件)
             BooleanQuery q2 = new BooleanQuery();
-            q2.add(SearchUtils.getNotNullQuery(SearchConstants.GOODS_CMP_ID_FIELD), Occur.MUST);
-            q2.add(goIdNotNullQuery, Occur.MUST_NOT);
+            q2.add(SearchUtils.getNullQuery(SearchConstants.GOODS_CMP_ID_FIELD), Occur.MUST_NOT);
+            q2.add(goNullQuery, Occur.MUST);
 
             BooleanQuery booleanQuery = new BooleanQuery();
             booleanQuery.add(q1, Occur.SHOULD);

+ 6 - 4
mall-search/src/main/java/com/uas/search/util/DocumentToObjectUtils.java

@@ -175,8 +175,9 @@ public class DocumentToObjectUtils {
 		}
 		Goods goods = new Goods();
 		TradeGoods tradeGoods = new TradeGoods();
-		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_GO_ID_FIELD))) {
-			tradeGoods.setId(Long.valueOf(document.get(SearchConstants.GOODS_GO_ID_FIELD)));
+        String goId = document.get(SearchConstants.GOODS_GO_ID_FIELD);
+        if (!StringUtils.isEmpty(goId) && !goId.equals(ObjectToDocumentUtils.NULL_VALUE)) {
+			tradeGoods.setId(Long.valueOf(goId));
 		}
 		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_GO_RESERVE_FIELD))) {
 			tradeGoods.setReserve(Double.valueOf(document.get(SearchConstants.GOODS_GO_RESERVE_FIELD)));
@@ -214,8 +215,9 @@ public class DocumentToObjectUtils {
 		goods.setStore(store);
 
 		Component component = new Component();
-        if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_CMP_ID_FIELD))) {
-            component.setId(Long.valueOf(document.get(SearchConstants.GOODS_CMP_ID_FIELD)));
+        String cmpId = document.get(SearchConstants.GOODS_CMP_ID_FIELD);
+        if (!StringUtils.isEmpty(cmpId) && !cmpId.equals(ObjectToDocumentUtils.NULL_VALUE)) {
+            component.setId(Long.valueOf(cmpId));
         }
         if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_CMP_CODE_FIELD))) {
             component.setCode(document.get(SearchConstants.GOODS_CMP_CODE_FIELD));

+ 14 - 5
mall-search/src/main/java/com/uas/search/util/ObjectToDocumentUtils.java

@@ -18,6 +18,11 @@ import java.util.Set;
  */
 public class ObjectToDocumentUtils {
 
+    /**
+     * 空值
+     */
+    public static final String NULL_VALUE = "NULL_VALUE";
+
 	/**
 	 * 将对象转为Document
 	 * 
@@ -240,9 +245,7 @@ public class ObjectToDocumentUtils {
 		Document document = new Document();
 		if(goods.getTradeGoods() != null){
 			TradeGoods tradeGoods = goods.getTradeGoods();
-			if (tradeGoods.getId() != null) {
-				document.add(new StringField(SearchConstants.GOODS_GO_ID_FIELD, String.valueOf(tradeGoods.getId()), Store.YES));
-			}
+            document.add(new StringField(SearchConstants.GOODS_GO_ID_FIELD, String.valueOf(tradeGoods.getId()), Store.YES));
 			if (tradeGoods.getReserve() != null) {
 				document.add(new DoubleDocValuesField(SearchConstants.GOODS_GO_RESERVE_FIELD, tradeGoods.getReserve()));
 				document.add(new DoubleField(SearchConstants.GOODS_GO_RESERVE_FIELD, tradeGoods.getReserve(), Store.YES));
@@ -277,7 +280,10 @@ public class ObjectToDocumentUtils {
                 document.add(new DoubleDocValuesField(SearchConstants.GOODS_GO_MINDELIVERY_FIELD, tradeGoods.getMinDelivery()));
                 document.add(new LongField(SearchConstants.GOODS_GO_MINDELIVERY_FIELD, tradeGoods.getMinDelivery(), Store.YES));
             }
-		}
+		} else {
+            // 批次 id 为 null 时,存默认值,以便于后期搜索时做空值过滤
+            document.add(new StringField(SearchConstants.GOODS_GO_ID_FIELD, NULL_VALUE, Store.YES));
+        }
 
 		if (goods.getStore() != null) {
 			com.uas.search.model.Store store = goods.getStore();
@@ -361,7 +367,10 @@ public class ObjectToDocumentUtils {
 				document.add(new DoubleDocValuesField(SearchConstants.GOODS_BR_WEIGHT_FIELD, brand.getWeight()));
 				document.add(new DoubleField(SearchConstants.GOODS_BR_WEIGHT_FIELD, brand.getWeight(), Store.YES));
 			}
-		}
+		} else {
+            // 器件 id 为 null 时,存默认值,以便于后期搜索时做空值过滤
+            document.add(new StringField(SearchConstants.GOODS_CMP_ID_FIELD, NULL_VALUE, Store.YES));
+        }
 
         if(goods.getProducts() != null){
             Products products = goods.getProducts();

+ 4 - 12
mall-search/src/main/java/com/uas/search/util/SearchUtils.java

@@ -11,8 +11,6 @@ import org.apache.lucene.analysis.TokenStream;
 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.index.Term;
-import org.apache.lucene.queryparser.classic.ParseException;
-import org.apache.lucene.queryparser.classic.QueryParser;
 import org.apache.lucene.search.BooleanClause.Occur;
 import org.apache.lucene.search.*;
 import org.slf4j.Logger;
@@ -172,22 +170,16 @@ public class SearchUtils {
 	}
 
     /**
-     * 查询值为空
+     * 查询值为空
      *
-     * @param field
-     *            搜索的域名
+     * @param field 搜索的域名
      * @return 构造的查询
      */
-	public static Query getNotNullQuery(String field){
+    public static Query getNullQuery(String field) {
         if (StringUtils.isEmpty(field)) {
             return null;
         }
-        String query = field + ":[* TO *]";
-        try {
-            return new QueryParser(null, null).parse(query);
-        } catch (ParseException e) {
-            throw new IllegalStateException("查询语法错误:" + query);
-        }
+        return new TermQuery(new Term(field, ObjectToDocumentUtils.NULL_VALUE));
     }
 
 	/**