Browse Source

products search; filter by go_status;

sunyj 8 years ago
parent
commit
d904f5e3e7

+ 3 - 0
src/main/java/com/uas/search/constant/SearchConstants.java

@@ -115,6 +115,7 @@ public class SearchConstants {
 	// 批次索引字段的key
 	public static final String GOODS_GO_ID_FIELD = "go_id";
 	public static final String GOODS_GO_RESERVE_FIELD = "go_reserve";
+	public static final String GOODS_GO_STATUS_FIELD = "go_status";
 	public static final String GOODS_GO_MINPRICERMB_FIELD = "go_minpricermb";
 	public static final String GOODS_GO_MINPRICEUSD_FIELD = "go_minpriceusd";
     public static final String GOODS_GO_VISIT_COUNT_FIELD = "go_visit_count";
@@ -141,6 +142,8 @@ public class SearchConstants {
     public static final String GOODS_BR_VISIT_COUNT_FIELD = "br_visit_count";
     public static final String GOODS_BR_SEARCH_COUNT_FIELD = "br_search_count";
     public static final String GOODS_BR_WEIGHT_FIELD = "br_weight";
+	public static final String GOODS_PR_ID_FIELD = "pr_id";
+	public static final String GOODS_PR_PCMPCODE_FIELD = "pr_pcmpcode";
 
 	// 商城销售订单索引字段的key
 	/**

+ 15 - 7
src/main/java/com/uas/search/dao/GoodsDao.java

@@ -1,9 +1,6 @@
 package com.uas.search.dao;
 
-import com.uas.search.model.Component;
-import com.uas.search.model.Goods;
-import com.uas.search.model.Store;
-import com.uas.search.model.TradeGoods;
+import com.uas.search.model.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Repository;
 import org.springframework.util.CollectionUtils;
@@ -27,6 +24,9 @@ public class GoodsDao {
 	@Autowired
 	private StoreDao storeDao;
 
+	@Autowired
+	private ProductsDao productsDao;
+
 	/**
 	 * 根据批次id或者器件id获取批次
 	 *
@@ -56,10 +56,14 @@ public class GoodsDao {
 		TradeGoods tradeGoods = tradeGoodsDao.findOne(goId);
 		Component component = componentDao.findByUuid(tradeGoods.getCmpUuid());
 		Store store = null;
+		Products products = null;
 		if (tradeGoods.getStoreId() != null) {
 			store = storeDao.findByUuid(tradeGoods.getStoreId());
 		}
-		return new Goods(tradeGoods, store, component);
+		if (tradeGoods.getProductId() != null) {
+			products = productsDao.findOne(tradeGoods.getProductId());
+		}
+		return new Goods(tradeGoods, store, component, products);
 	}
 
 	/**
@@ -91,13 +95,17 @@ public class GoodsDao {
 		if (!CollectionUtils.isEmpty(tradeGoodsesList)) {
 			for (TradeGoods tradeGoods : tradeGoodsesList) {
 				Store store = null;
+				Products products = null;
 				if (tradeGoods.getStoreId() != null) {
 					store = storeDao.findByUuid(tradeGoods.getStoreId());
 				}
-				goodsesList.add(new Goods(tradeGoods, store, component));
+				if (tradeGoods.getProductId() != null) {
+					products = productsDao.findOne(tradeGoods.getProductId());
+				}
+				goodsesList.add(new Goods(tradeGoods, store, component, products));
 			}
 		} else {
-			goodsesList.add(new Goods(null, null, component));
+			goodsesList.add(new Goods(null, null, component, null));
 		}
 		return goodsesList;
 	}

+ 14 - 0
src/main/java/com/uas/search/dao/ProductsDao.java

@@ -0,0 +1,14 @@
+package com.uas.search.dao;
+
+import com.uas.search.model.Products;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.stereotype.Repository;
+
+/**
+ * @author sunyj
+ * @since 2017/11/24 17:02
+ */
+@Repository
+public interface ProductsDao extends JpaSpecificationExecutor<Products>, JpaRepository<Products, Long> {
+}

+ 16 - 1
src/main/java/com/uas/search/model/Goods.java

@@ -27,13 +27,19 @@ public class Goods implements Serializable {
 	 */
 	private Component component;
 
+	/**
+	 * 物料
+	 */
+	private Products products;
+
 	public Goods() {
 	}
 
-	public Goods(TradeGoods tradeGoods, Store store, Component component) {
+	public Goods(TradeGoods tradeGoods, Store store, Component component, Products products) {
 		this.tradeGoods = tradeGoods;
 		this.store = store;
 		this.component = component;
+		this.products = products;
 	}
 
 	public TradeGoods getTradeGoods() {
@@ -60,12 +66,21 @@ public class Goods implements Serializable {
 		this.component = component;
 	}
 
+	public Products getProducts() {
+		return products;
+	}
+
+	public void setProducts(Products products) {
+		this.products = products;
+	}
+
 	@Override
 	public String toString() {
 		return "Goods{" +
 				"tradeGoods=" + tradeGoods +
 				", store=" + store +
 				", component=" + component +
+				", products=" + products +
 				'}';
 	}
 }

+ 57 - 0
src/main/java/com/uas/search/model/Products.java

@@ -0,0 +1,57 @@
+package com.uas.search.model;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import java.io.Serializable;
+
+/**
+ * 物料(用于非标搜索)
+ *
+ * @author sunyj
+ * @since 2017/11/24 16:46
+ */
+@Entity
+@Table(name = "products")
+public class Products implements Serializable {
+
+    /**
+     * 序列号
+     */
+    private static final long serialVersionUID = 1L;
+
+    @Id
+    @Column(name = "pr_id")
+    private Long id;
+
+    /**
+     * 型号
+     */
+    @Column(name = "pr_pcmpcode")
+    private String pcmpCode;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getPcmpCode() {
+        return pcmpCode;
+    }
+
+    public void setPcmpCode(String pcmpCode) {
+        this.pcmpCode = pcmpCode;
+    }
+
+    @Override
+    public String toString() {
+        return "Products{" +
+                "id=" + id +
+                ", pcmpCode='" + pcmpCode + '\'' +
+                '}';
+    }
+}

+ 32 - 0
src/main/java/com/uas/search/model/TradeGoods.java

@@ -18,6 +18,8 @@ public class TradeGoods implements Serializable {
 
 	private static final long serialVersionUID = 1L;
 
+	public static final Long[] VALID_STATUS = {601L, 602L};
+
 	/**
 	 * id
 	 */
@@ -31,6 +33,12 @@ public class TradeGoods implements Serializable {
 	@Column(name = "go_reserve")
 	private Double reserve;
 
+	/**
+	 * 状态
+	 */
+	@Column(name = "go_status")
+	private Long status;
+
 	/**
 	 * 批次的人民币价格
 	 */
@@ -61,6 +69,12 @@ public class TradeGoods implements Serializable {
 	@Column(name = "cmp_uuid")
 	private String cmpUuid;
 
+	/**
+	 * 物料 id
+	 */
+	@Column(name = "go_productid")
+	private Long productId;
+
 	/**
 	 * 访问量
 	 */
@@ -83,6 +97,14 @@ public class TradeGoods implements Serializable {
 		this.reserve = reserve;
 	}
 
+	public Long getStatus() {
+		return status;
+	}
+
+	public void setStatus(Long status) {
+		this.status = status;
+	}
+
 	public Double getMinPriceRMB() {
 		return minPriceRMB;
 	}
@@ -123,6 +145,14 @@ public class TradeGoods implements Serializable {
 		this.cmpUuid = cmpUuid;
 	}
 
+	public Long getProductId() {
+		return productId;
+	}
+
+	public void setProductId(Long productId) {
+		this.productId = productId;
+	}
+
 	public Long getVisitCount() {
 		return visitCount;
 	}
@@ -136,11 +166,13 @@ public class TradeGoods implements Serializable {
 		return "TradeGoods{" +
 				"id=" + id +
 				", reserve=" + reserve +
+				", status=" + status +
 				", minPriceRMB=" + minPriceRMB +
 				", minPriceUSD=" + minPriceUSD +
 				", crName='" + crName + '\'' +
 				", storeId='" + storeId + '\'' +
 				", cmpUuid='" + cmpUuid + '\'' +
+				", productId=" + productId +
 				", visitCount=" + visitCount +
 				'}';
 	}

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

@@ -447,7 +447,7 @@ public class IndexServiceImpl implements IndexService {
                         throw new IllegalArgumentException(line, e);
                     }
                     // 器件作为主体,得到批次
-                    printWriter.println(JSONObject.toJSONString(new Goods(null, null, component)));
+                    printWriter.println(JSONObject.toJSONString(new Goods(null, null, component, null)));
                     size++;
                 }
                 logger.info(goodsFileName + " - Converted..................." + size);

+ 25 - 30
src/main/java/com/uas/search/service/impl/SearchServiceImpl.java

@@ -10,10 +10,7 @@ import com.uas.search.constant.model.SPage;
 import com.uas.search.exception.SearchException;
 import com.uas.search.grouping.DistinctGroupCollector;
 import com.uas.search.grouping.GoodsGroupCollector;
-import com.uas.search.model.Brand;
-import com.uas.search.model.Component;
-import com.uas.search.model.Goods;
-import com.uas.search.model.Kind;
+import com.uas.search.model.*;
 import com.uas.search.service.SearchService;
 import com.uas.search.sort.SimilarValuesFieldComparatorSource;
 import com.uas.search.sort.StringFieldComparatorSource;
@@ -168,7 +165,8 @@ public class SearchServiceImpl implements SearchService {
 	 * @return 品牌排序规则
 	 */
 	private SortField[] sortBrand(String keyword) {
-		// 分数 > 自定义排序 > 权重 > 访问量 > 搜索次数
+		// 自定义排序 > 权重 > 访问量 > 搜索次数 > 分数
+        // 分数排序放在最后,是因为有的中英文名称相同,分数翻倍,但实际匹配度并不高
         return new SortField[]{
 				new SortField(SearchConstants.BRAND_NAMEEN_UNTOKENIZED_FIELD, new StringFieldComparatorSource(keyword)),
 				new SortField(SearchConstants.BRAND_NAMECN_UNTOKENIZED_FIELD, new StringFieldComparatorSource(keyword)),
@@ -1057,9 +1055,10 @@ public class SearchServiceImpl implements SearchService {
      * @return 批次排序规则
      */
     private SortField[] sortGoods(String keyword) {
-        // 批次(访问量) > 器件(自定义排序 > 访问量 > 搜索次数) > 品牌(自定义排序 > 权重 > 访问量 > 搜索次数) > 类目(访问量 > 搜索次数)
+        // 器件、非标型号自定义排序 > 批次(访问量) > 器件(自定义排序 > 访问量 > 搜索次数) > 品牌(自定义排序 > 权重 > 访问量 > 搜索次数) > 类目(访问量 > 搜索次数)
         return new SortField[]{
                 new SortField(SearchConstants.GOODS_CMP_CODE_FIELD, new StringFieldComparatorSource(keyword)),
+                new SortField(SearchConstants.GOODS_PR_PCMPCODE_FIELD, new StringFieldComparatorSource(keyword)),
                 sortField(SearchConstants.GOODS_GO_VISIT_COUNT_FIELD, Type.LONG, true, Long.MIN_VALUE),
                 sortField(SearchConstants.GOODS_CMP_VISIT_COUNT_FIELD, Type.LONG, true, Long.MIN_VALUE),
                 sortField(SearchConstants.GOODS_CMP_SEARCH_COUNT_FIELD, Type.LONG, true, Long.MIN_VALUE),
@@ -1206,45 +1205,40 @@ public class SearchServiceImpl implements SearchService {
 				booleanQuery.add(booleanQuery2, BooleanClause.Occur.MUST);
 			}
 		}
+		// 过滤状态
+		filter(Arrays.asList(TradeGoods.VALID_STATUS), SearchConstants.GOODS_GO_STATUS_FIELD, booleanQuery);
 		return booleanQuery;
 	}
 
 	/**
-	 * 同时搜索器件、类目、品牌,并设置boost
-	 * 
-	 * @param keyword
-	 * @return
+	 * 同时搜索器件、类目、品牌等,并设置boost
 	 */
 	private Query setGoodsBoost(String keyword) {
 		BooleanQuery booleanQuery = new BooleanQuery();
-		BooleanQuery componentCodeQuery = createQuery(SearchConstants.GOODS_CMP_CODE_FIELD, keyword, true);
-		componentCodeQuery.setBoost(100);
-		booleanQuery.add(componentCodeQuery, BooleanClause.Occur.SHOULD);
-		BooleanQuery brandNameCnQuery = createQuery(SearchConstants.GOODS_BR_NAME_CN_UNTOKENIZED_FIELD, keyword, true);
-		brandNameCnQuery.setBoost(10);
-		booleanQuery.add(brandNameCnQuery, BooleanClause.Occur.SHOULD);
-		BooleanQuery brandNameEnQuery = createQuery(SearchConstants.GOODS_BR_NAME_EN_UNTOKENIZED_FIELD, keyword, true);
-		brandNameEnQuery.setBoost(10);
-		booleanQuery.add(brandNameEnQuery, BooleanClause.Occur.SHOULD);
-		BooleanQuery kindNameQuery = createQuery(SearchConstants.GOODS_KI_NAME_CN_FIELD, keyword);
-		kindNameQuery.setBoost(1);
-		booleanQuery.add(kindNameQuery, BooleanClause.Occur.SHOULD);
-		BooleanQuery descriptionQuery = createQuery(SearchConstants.GOODS_CMP_DESCRIPTION_FIELD, keyword);
-		descriptionQuery.setBoost(1);
-		booleanQuery.add(descriptionQuery, BooleanClause.Occur.SHOULD);
+		// 原厂型号
+		booleanQuery.add(createQuery(SearchConstants.GOODS_CMP_CODE_FIELD, keyword, true, 100), BooleanClause.Occur.SHOULD);
+		// 非标
+		booleanQuery.add(createQuery(SearchConstants.GOODS_PR_PCMPCODE_FIELD, keyword, true, 100), Occur.SHOULD);
+		// 品牌
+		booleanQuery.add(createQuery(SearchConstants.GOODS_BR_NAME_CN_UNTOKENIZED_FIELD, keyword, true, 10), BooleanClause.Occur.SHOULD);
+		booleanQuery.add(createQuery(SearchConstants.GOODS_BR_NAME_EN_UNTOKENIZED_FIELD, keyword, true, 10), BooleanClause.Occur.SHOULD);
+		// 类目
+		booleanQuery.add(createQuery(SearchConstants.GOODS_KI_NAME_CN_FIELD, keyword, 1), BooleanClause.Occur.SHOULD);
+		// 属性值
+		booleanQuery.add(createQuery(SearchConstants.GOODS_CMP_DESCRIPTION_FIELD, keyword, 1), BooleanClause.Occur.SHOULD);
 		return booleanQuery;
 	}
 
-    private BooleanQuery createQuery(String field, String keyword){
-       return createQuery(field, keyword, false);
+    private BooleanQuery createQuery(String field, String keyword, float boost){
+       return createQuery(field, keyword, false, boost);
     }
 
 
-    private BooleanQuery createQuery(String field, String keyword, boolean useRegexpQuery){
+    private BooleanQuery createQuery(String field, String keyword, boolean useRegexpQuery, float boost){
+        BooleanQuery booleanQuery = new BooleanQuery();
         if (StringUtils.isEmpty(field) || StringUtils.isEmpty(keyword)) {
-            return null;
+            return booleanQuery;
         }
-        BooleanQuery booleanQuery = new BooleanQuery();
         // 根据空格分隔关键词,分隔的词取或的关系
         String[] array = keyword.split(" ");
         for(String str : array){
@@ -1252,6 +1246,7 @@ public class SearchServiceImpl implements SearchService {
                 booleanQuery.add(SearchUtils.getBooleanQuery(field, str, useRegexpQuery), Occur.SHOULD);
             }
         }
+        booleanQuery.setBoost(boost);
         return booleanQuery;
     }
 

+ 11 - 0
src/main/java/com/uas/search/util/DocumentToObjectUtils.java

@@ -181,6 +181,9 @@ public class DocumentToObjectUtils {
 		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_GO_RESERVE_FIELD))) {
 			tradeGoods.setReserve(Double.valueOf(document.get(SearchConstants.GOODS_GO_RESERVE_FIELD)));
 		}
+		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_GO_STATUS_FIELD))) {
+			tradeGoods.setStatus(Long.valueOf(document.get(SearchConstants.GOODS_GO_STATUS_FIELD)));
+		}
 		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_GO_MINPRICERMB_FIELD))) {
 			tradeGoods.setMinPriceRMB(Double.valueOf(document.get(SearchConstants.GOODS_GO_MINPRICERMB_FIELD)));
 		}
@@ -262,6 +265,14 @@ public class DocumentToObjectUtils {
         }
 		component.setBrand(brand);
 		goods.setComponent(component);
+
+		Products products = new Products();
+		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_PR_ID_FIELD))) {
+			products.setId(Long.valueOf(document.get(SearchConstants.GOODS_PR_ID_FIELD)));
+		}
+		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_PR_PCMPCODE_FIELD))) {
+			products.setPcmpCode(document.get(SearchConstants.GOODS_PR_PCMPCODE_FIELD));
+		}
 		return goods;
 	}
 

+ 15 - 1
src/main/java/com/uas/search/util/ObjectToDocumentUtils.java

@@ -238,6 +238,9 @@ public class ObjectToDocumentUtils {
 				document.add(new DoubleDocValuesField(SearchConstants.GOODS_GO_RESERVE_FIELD, tradeGoods.getReserve()));
 				document.add(new DoubleField(SearchConstants.GOODS_GO_RESERVE_FIELD, tradeGoods.getReserve(), Store.YES));
 			}
+			if (tradeGoods.getStatus() != null) {
+				document.add(new StringField(SearchConstants.GOODS_GO_STATUS_FIELD, String.valueOf(tradeGoods.getStatus()), Store.YES));
+			}
 			if (tradeGoods.getMinPriceRMB() != null) {
 				document.add(
 						new DoubleDocValuesField(SearchConstants.GOODS_GO_MINPRICERMB_FIELD, tradeGoods.getMinPriceRMB()));
@@ -339,7 +342,18 @@ public class ObjectToDocumentUtils {
 			document.add(new DoubleField(SearchConstants.GOODS_BR_WEIGHT_FIELD, brand.getWeight(), Store.YES));
 		}
 
-		return document;
+        if(goods.getProducts() != null){
+            Products products = goods.getProducts();
+            if (products.getId() != null) {
+                document.add(new StringField(SearchConstants.GOODS_PR_ID_FIELD, String.valueOf(products.getId()), Store.YES));
+            }
+            if(products.getPcmpCode() != null){
+                document.add(new StringField(SearchConstants.GOODS_PR_PCMPCODE_FIELD, products.getPcmpCode().toLowerCase(), Store.YES));
+                document.add(new BinaryDocValuesField(SearchConstants.GOODS_PR_PCMPCODE_FIELD, new BytesRef(products.getPcmpCode())));
+            }
+        }
+
+        return document;
 	}
 
 	/**