package com.uas.search.util; import com.alibaba.fastjson.JSONObject; import com.uas.search.constant.SearchConstants; import com.uas.search.model.*; import org.apache.lucene.document.*; import org.apache.lucene.document.Field.Store; import org.apache.lucene.util.BytesRef; import java.util.Set; /** * 将对象转换为Document的工具类 * * @author sunyj * @since 2016年10月17日 上午11:32:19 */ public class ObjectToDocumentUtils { /** * 空值 */ public static final String NULL_VALUE = "NULLVALUE"; /** * 将对象转为Document * * @param object * 对象,可为Kind、Brand、Component、 * Order、OrderInvoice、Purchase、 * PurchaseInvoice * * @return 转换的Document */ public static Document toDocument(Object object) { if (object == null) { return null; } if (object instanceof Kind) { return toDocument((Kind) object); } else if (object instanceof Brand) { return toDocument((Brand) object); } else if (object instanceof Component) { return toDocument((Component) object); } else if (object instanceof Goods) { return toDocument((Goods) object); } else if (object instanceof PCBGoods) { return toDocument((PCBGoods) object); } else if (object instanceof Order) { return toDocument((Order) object); } else if (object instanceof OrderInvoice) { return toDocument((OrderInvoice) object); } else if (object instanceof Purchase) { return toDocument((Purchase) object); } else if (object instanceof PurchaseInvoice) { return toDocument((PurchaseInvoice) object); } else if (object instanceof V_Products) { return toDocument((V_Products) object); } else { throw new IllegalArgumentException("不支持将以下类型转换为Document:" + object.getClass().getName()); } } /** * Kind对象转为Document * * @param kind * @return */ public static Document toDocument(Kind kind) { if (kind == null || kind.getId() == null || StringUtils.isEmpty(kind.getNameCn()) || kind.getIsLeaf() == null || kind.getLevel() == null) { return null; } Document document = new Document(); // 不能用LongField,否则后续实时更新索引时,方法updateDocument(new Term("", ""), // doc)无法根据id进行更新 document.add(new StringField(SearchConstants.KIND_ID_FIELD, String.valueOf(kind.getId()), Store.YES)); document.add(new TextField(SearchConstants.KIND_NAMECN_FIELD, kind.getNameCn(), Store.YES)); document.add(new StringField(SearchConstants.KIND_NAMECN_UNTOKENIZED_FIELD, kind.getNameCn().toLowerCase(), Store.YES)); document.add(new BinaryDocValuesField(SearchConstants.KIND_NAMECN_UNTOKENIZED_FIELD, new BytesRef(kind.getNameCn()))); document.add(new StringField(SearchConstants.KIND_ISLEAF_FIELD, String.valueOf(kind.getIsLeaf()), Store.YES)); document.add(new StringField(SearchConstants.KIND_LEVEL_FIELD, String.valueOf(kind.getLevel()), Store.YES)); if (kind.getVisitCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.KIND_VISIT_COUNT_FIELD, kind.getVisitCount())); document.add(new LongField(SearchConstants.KIND_VISIT_COUNT_FIELD, kind.getVisitCount(), Store.YES)); } if (kind.getSearchCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.KIND_SEARCH_COUNT_FIELD, kind.getSearchCount())); document.add(new LongField(SearchConstants.KIND_SEARCH_COUNT_FIELD, kind.getSearchCount(), Store.YES)); } return document; } /** * Product对象转为Document * * @param product * @return */ public static Document toDocument(V_Products product) { if (product == null || product.getId() == null || StringUtils.isEmpty(product.getpBrandEn()) || StringUtils.isEmpty(product.getpCmpCode())) { return null; } Document document = new Document(); // 不能用LongField,否则后续实时更新索引时,方法updateDocument(new Term("", ""), // doc)无法根据id进行更新 document.add(new StringField(SearchConstants.PRODUCT_PRIVATE_ID_FIELD, String.valueOf(product.getId()), Store.YES)); document.add(new DoubleDocValuesField(SearchConstants.PRODUCT_PRIVATE_ID_FIELD, product.getId())); if (!StringUtils.isEmpty(product.getTitle())) { document.add(new TextField(SearchConstants.PRODUCT_PRIVATE_TITLE_FIELD, product.getTitle(), Store.YES)); } if (!StringUtils.isEmpty(product.getBrand())) { document.add(new StringField(SearchConstants.PRODUCT_PRIVATE_BRAND_FIELD, product.getBrand(), Store.YES)); } if (!StringUtils.isEmpty(product.getCmpCode())) { document.add(new StringField(SearchConstants.PRODUCT_PRIVATE_CMPCODE_FIELD, product.getCmpCode(), Store.YES)); } if (!StringUtils.isEmpty(product.getKind())) { document.add(new StringField(SearchConstants.PRODUCT_PRIVATE_KIND_FIELD, product.getKind(), Store.YES)); } document.add(new StringField(SearchConstants.PRODUCT_PRIVATE_PBRANDEN_FIELD, product.getpBrandEn(), Store.YES)); document.add(new StringField(SearchConstants.PRODUCT_PRIVATE_PCMPCODE_FIELD, product.getpCmpCode(), Store.YES)); if (product.getStandard() != null) { document.add(new StringField(SearchConstants.PRODUCT_PRIVATE_STANDARD_FIELD, String.valueOf(product.getStandard()), Store.YES)); } else { // 为空默认非标 document.add(new StringField(SearchConstants.PRODUCT_PRIVATE_STANDARD_FIELD, String.valueOf(0), Store.YES)); } if (product.getB2cEnabled() != null) { document.add(new StringField(SearchConstants.PRODUCT_PRIVATE_B2CENABLED_FIELD, String.valueOf(product.getB2cEnabled()), Store.YES)); } else { // 为空默认可用 document.add(new StringField(SearchConstants.PRODUCT_PRIVATE_B2CENABLED_FIELD, String.valueOf(1), Store.YES)); } return document; } /** * Brand对象转为Document * * @param brand * @return */ public static Document toDocument(Brand brand) { if (brand == null || brand.getId() == null || StringUtils.isEmpty(brand.getNameCn()) || StringUtils.isEmpty(brand.getUuid())) { return null; } Document document = new Document(); document.add(new StringField(SearchConstants.BRAND_ID_FIELD, String.valueOf(brand.getId()), Store.YES)); document.add(new TextField(SearchConstants.BRAND_NAMECN_FIELD, brand.getNameCn(), Store.YES)); document.add(new StringField(SearchConstants.BRAND_NAMECN_UNTOKENIZED_FIELD, brand.getNameCn().toLowerCase(), Store.YES)); document.add(new BinaryDocValuesField(SearchConstants.BRAND_NAMECN_UNTOKENIZED_FIELD, new BytesRef(brand.getNameCn()))); document.add(new StringField(SearchConstants.BRAND_UUID_FIELD, brand.getUuid(), Store.YES)); if (brand.getNameEn() != null) { document.add(new TextField(SearchConstants.BRAND_NAMEEN_FIELD, brand.getNameEn(), Store.YES)); document.add(new StringField(SearchConstants.BRAND_NAMEEN_UNTOKENIZED_FIELD, brand.getNameEn().toLowerCase(), Store.YES)); document.add(new BinaryDocValuesField(SearchConstants.BRAND_NAMEEN_UNTOKENIZED_FIELD, new BytesRef(brand.getNameEn()))); } if (brand.getVisitCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.BRAND_VISIT_COUNT_FIELD, brand.getVisitCount())); document.add(new LongField(SearchConstants.BRAND_VISIT_COUNT_FIELD, brand.getVisitCount(), Store.YES)); } if (brand.getSearchCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.BRAND_SEARCH_COUNT_FIELD, brand.getSearchCount())); document.add(new LongField(SearchConstants.BRAND_SEARCH_COUNT_FIELD, brand.getSearchCount(), Store.YES)); } if (brand.getWeight() != null) { document.add(new DoubleDocValuesField(SearchConstants.BRAND_WEIGHT_FIELD, brand.getWeight())); document.add(new DoubleField(SearchConstants.BRAND_WEIGHT_FIELD, brand.getWeight(), Store.YES)); } return document; } /** * Component对象转为Document * * @param component * @return */ public static Document toDocument(Component component) { if (component == null || component.getId() == null || StringUtils.isEmpty(component.getUuid()) || StringUtils.isEmpty(component.getCode()) || component.getKind() == null || component.getKind().getId() == null || StringUtils.isEmpty(component.getKind().getNameCn()) || component.getKind().getLevel() == null || component.getBrand() == null || component.getBrand().getId() == null || StringUtils.isEmpty(component.getBrand().getNameCn()) || StringUtils.isEmpty(component.getBrand().getUuid())) { return null; } Document document = new Document(); document.add(new StringField(SearchConstants.COMPONENT_ID_FIELD, String.valueOf(component.getId()), Store.YES)); document.add(new StringField(SearchConstants.COMPONENT_UUID_FIELD, component.getUuid(), Store.YES)); // 转小写,以避免分词,又不会因大小写影响搜索 document.add(new StringField(SearchConstants.COMPONENT_CODE_FIELD, component.getCode().toLowerCase(), Store.YES)); document.add(new BinaryDocValuesField(SearchConstants.COMPONENT_CODE_FIELD, new BytesRef(component.getCode()))); if (component.getVisitCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.COMPONENT_VISIT_COUNT_FIELD, component.getVisitCount())); document.add(new LongField(SearchConstants.COMPONENT_VISIT_COUNT_FIELD, component.getVisitCount(), Store.YES)); } if (component.getSearchCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.COMPONENT_SEARCH_COUNT_FIELD, component.getSearchCount())); document.add(new LongField(SearchConstants.COMPONENT_SEARCH_COUNT_FIELD, component.getSearchCount(), Store.YES)); } if (component.getWeight() != null) { document.add(new DoubleDocValuesField(SearchConstants.COMPONENT_WEIGHT_FIELD, component.getWeight())); document.add(new DoubleField(SearchConstants.COMPONENT_WEIGHT_FIELD, component.getWeight(), Store.YES)); } Kind kind = component.getKind(); document.add(new StringField(SearchConstants.COMPONENT_KI_ID_FIELD, String.valueOf(kind.getId()), Store.YES)); document.add(new TextField(SearchConstants.COMPONENT_KI_NAME_FIELD, kind.getNameCn(), Store.YES)); document.add(new StringField(SearchConstants.COMPONENT_KI_NAME_UNTOKENIZED_FIELD, kind.getNameCn().toLowerCase(), Store.YES)); document.add(new BinaryDocValuesField(SearchConstants.COMPONENT_KI_NAME_UNTOKENIZED_FIELD, new BytesRef(kind.getNameCn()))); document.add(new StringField(SearchConstants.COMPONENT_KI_LEVEL_FIELD, String.valueOf(kind.getLevel()), Store.YES)); if (kind.getVisitCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.COMPONENT_KI_VISIT_COUNT_FIELD, kind.getVisitCount())); document.add(new LongField(SearchConstants.COMPONENT_KI_VISIT_COUNT_FIELD, kind.getVisitCount(), Store.YES)); } if (kind.getSearchCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.COMPONENT_KI_SEARCH_COUNT_FIELD, kind.getSearchCount())); document.add(new LongField(SearchConstants.COMPONENT_KI_SEARCH_COUNT_FIELD, kind.getSearchCount(), Store.YES)); } Brand brand = component.getBrand(); document.add(new StringField(SearchConstants.COMPONENT_BR_ID_FIELD, String.valueOf(brand.getId()), Store.YES)); document.add(new TextField(SearchConstants.COMPONENT_BR_NAMECN_FIELD, brand.getNameCn(), Store.YES)); document.add(new StringField(SearchConstants.COMPONENT_BR_NAMECN_UNTOKENIZED_FIELD, brand.getNameCn().toLowerCase(), Store.YES)); document.add(new BinaryDocValuesField(SearchConstants.COMPONENT_BR_NAMECN_UNTOKENIZED_FIELD, new BytesRef(brand.getNameCn()))); if(brand.getNameEn() != null){ document.add(new TextField(SearchConstants.COMPONENT_BR_NAMEEN_FIELD, brand.getNameEn(), Store.YES)); document.add(new StringField(SearchConstants.COMPONENT_BR_NAMEEN_UNTOKENIZED_FIELD, brand.getNameEn().toLowerCase(), Store.YES)); document.add(new BinaryDocValuesField(SearchConstants.COMPONENT_BR_NAMEEN_UNTOKENIZED_FIELD, new BytesRef(brand.getNameEn()))); } document.add(new StringField(SearchConstants.COMPONENT_BR_UUID_FIELD, brand.getUuid(), Store.YES)); if (brand.getVisitCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.COMPONENT_BR_VISIT_COUNT_FIELD, brand.getVisitCount())); document.add(new LongField(SearchConstants.COMPONENT_BR_VISIT_COUNT_FIELD, brand.getVisitCount(), Store.YES)); } if (brand.getSearchCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.COMPONENT_BR_SEARCH_COUNT_FIELD, brand.getSearchCount())); document.add(new LongField(SearchConstants.COMPONENT_BR_SEARCH_COUNT_FIELD, brand.getSearchCount(), Store.YES)); } if (brand.getWeight() != null) { document.add(new DoubleDocValuesField(SearchConstants.COMPONENT_BR_WEIGHT_FIELD, brand.getWeight())); document.add(new DoubleField(SearchConstants.COMPONENT_BR_WEIGHT_FIELD, brand.getWeight(), Store.YES)); } if (component.getReserve() != null) { document.add(new DoubleField(SearchConstants.COMPONENT_RESERVE_FIELD, component.getReserve(), Store.YES)); } if (component.getSampleQty() != null) { document.add( new DoubleField(SearchConstants.COMPONENT_SAMPLE_QTY_FIELD, component.getSampleQty(), Store.YES)); } if (component.getOriginalQty() != null) { document.add(new DoubleField(SearchConstants.COMPONENT_ORIGINAL_QTY_FIELD, component.getOriginalQty(), Store.YES)); } if (component.getInactionStockQty() != null) { document.add(new DoubleField(SearchConstants.COMPONENT_INACTION_STOCK_QTY_FIELD, component.getInactionStockQty(), Store.YES)); } // 属性值加入索引,索引中field的键:"pr_"前缀连接属性的id Set propertyValues = component.getProperties(); for (PropertyValue propertyValue : propertyValues) { if (!StringUtils.isEmpty(propertyValue.getValue())) { String fieldKey = SearchConstants.COMPONENT_PROPERTY_PREFIX + propertyValue.getPropertyid(); document.add(new StringField(fieldKey, propertyValue.getValue(), Store.YES)); // 另建一份分词的属性索引,用于属性值联想时不区分大小写 String fieldKeyTokenized = fieldKey + SearchConstants.COMPONENT_PROPERTY_TOKENIZED_SUFFIX; document.add(new TextField(fieldKeyTokenized, propertyValue.getValue(), Store.YES)); } } return document; } /** * Goods对象转为Document * * @param goods * @return */ public static Document toDocument(Goods goods) { if (goods == null || (goods.getTradeGoods() != null && goods.getTradeGoods().getId() == null) || (goods.getComponent() != null && (goods.getComponent().getId() == null || StringUtils.isEmpty(goods.getComponent().getCode()) || goods.getComponent().getKind() == null || goods.getComponent().getBrand() == null))) { return null; } String primaryKey = ""; Document document = new Document(); if(goods.getTradeGoods() != null){ TradeGoods tradeGoods = goods.getTradeGoods(); 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)); } 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())); document.add( new DoubleField(SearchConstants.GOODS_GO_MINPRICERMB_FIELD, tradeGoods.getMinPriceRMB(), Store.YES)); } if (tradeGoods.getMinPriceUSD() != null) { document.add( new DoubleDocValuesField(SearchConstants.GOODS_GO_MINPRICEUSD_FIELD, tradeGoods.getMinPriceUSD())); document.add( new DoubleField(SearchConstants.GOODS_GO_MINPRICEUSD_FIELD, tradeGoods.getMinPriceUSD(), Store.YES)); } if (tradeGoods.getCrName() != null) { document.add(new TextField(SearchConstants.GOODS_CRNAME_FIELD, tradeGoods.getCrName(), Store.YES)); } if (tradeGoods.getVisitCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.GOODS_GO_VISIT_COUNT_FIELD, tradeGoods.getVisitCount())); document.add(new LongField(SearchConstants.GOODS_GO_VISIT_COUNT_FIELD, tradeGoods.getVisitCount(), Store.YES)); } if (tradeGoods.getUpdateDate() != null) { document.add(new DoubleDocValuesField(SearchConstants.GOODS_GO_UPDATE_DATE_FIELD, tradeGoods.getUpdateDate().getTime())); document.add(new LongField(SearchConstants.GOODS_GO_UPDATE_DATE_FIELD, tradeGoods.getUpdateDate().getTime(), Store.YES)); } if (tradeGoods.getMinDelivery() != null) { document.add(new DoubleDocValuesField(SearchConstants.GOODS_GO_MINDELIVERY_FIELD, tradeGoods.getMinDelivery())); document.add(new LongField(SearchConstants.GOODS_GO_MINDELIVERY_FIELD, tradeGoods.getMinDelivery(), Store.YES)); } primaryKey += tradeGoods.getId(); } else { // 批次 id 为 null 时,存默认值,以便于后期搜索时做空值过滤 document.add(new StringField(SearchConstants.GOODS_GO_ID_FIELD, NULL_VALUE, Store.YES)); primaryKey += NULL_VALUE; } if (goods.getStore() != null) { com.uas.search.model.Store store = goods.getStore(); if (store.getUuid() != null) { document.add(new TextField(SearchConstants.GOODS_ST_UUID_FIELD, store.getUuid(), Store.YES)); } if (store.getType() != null) { document.add(new TextField(SearchConstants.GOODS_ST_TYPE_FIELD, store.getType(), Store.YES)); } } if(goods.getComponent() != null){ Component component = goods.getComponent(); document.add(new StringField(SearchConstants.GOODS_CMP_ID_FIELD, String.valueOf(component.getId()), Store.YES)); document.add(new StringField(SearchConstants.GOODS_CMP_CODE_FIELD, component.getCode().toLowerCase(), Store.YES)); document.add(new BinaryDocValuesField(SearchConstants.GOODS_CMP_CODE_FIELD, new BytesRef(component.getCode()))); if (component.getDescription() != null) { document.add(new TextField(SearchConstants.GOODS_CMP_DESCRIPTION_FIELD, component.getDescription(), Store.YES)); } if (component.getVisitCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.GOODS_CMP_VISIT_COUNT_FIELD, component.getVisitCount())); document.add(new LongField(SearchConstants.GOODS_CMP_VISIT_COUNT_FIELD, component.getVisitCount(), Store.YES)); } if (component.getSearchCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.GOODS_CMP_SEARCH_COUNT_FIELD, component.getSearchCount())); document.add(new LongField(SearchConstants.GOODS_CMP_SEARCH_COUNT_FIELD, component.getSearchCount(), Store.YES)); } if (component.getWeight() != null) { document.add(new DoubleDocValuesField(SearchConstants.GOODS_CMP_WEIGHT_FIELD, component.getWeight())); document.add(new DoubleField(SearchConstants.GOODS_CMP_WEIGHT_FIELD, component.getWeight(), Store.YES)); } Kind kind = component.getKind(); if (kind.getId() != null) { document.add(new StringField(SearchConstants.GOODS_KI_ID_FIELD, String.valueOf(kind.getId()), Store.YES)); } if (kind.getNameCn() != null) { document.add(new TextField(SearchConstants.GOODS_KI_NAME_CN_FIELD, kind.getNameCn(), Store.YES)); document.add(new StringField(SearchConstants.GOODS_KI_NAME_CN_UNTOKENIZED_FIELD, kind.getNameCn().toLowerCase(), Store.YES)); document.add(new BinaryDocValuesField(SearchConstants.GOODS_KI_NAME_CN_UNTOKENIZED_FIELD, new BytesRef( kind.getNameCn()))); } if (kind.getLevel() != null) { document.add(new NumericDocValuesField(SearchConstants.GOODS_KI_LEVEL_FIELD, kind.getLevel())); document.add(new LongField(SearchConstants.GOODS_KI_LEVEL_FIELD, kind.getLevel(), Store.YES)); } if (kind.getIsLeaf() != null) { document.add( new StringField(SearchConstants.GOODS_KI_ISLEAF_FIELD, String.valueOf(kind.getIsLeaf()), Store.YES)); } if (kind.getVisitCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.GOODS_KI_VISIT_COUNT_FIELD, kind.getVisitCount())); document.add(new LongField(SearchConstants.GOODS_KI_VISIT_COUNT_FIELD, kind.getVisitCount(), Store.YES)); } if (kind.getSearchCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.GOODS_KI_SEARCH_COUNT_FIELD, kind.getSearchCount())); document.add(new LongField(SearchConstants.GOODS_KI_SEARCH_COUNT_FIELD, kind.getSearchCount(), Store.YES)); } Brand brand = component.getBrand(); if (brand.getId() != null) { document.add(new StringField(SearchConstants.GOODS_BR_ID_FIELD, String.valueOf(brand.getId()), Store.YES)); } if (brand.getNameCn() != null) { document.add(new TextField(SearchConstants.GOODS_BR_NAME_CN_FIELD, brand.getNameCn(), Store.YES)); document.add(new StringField(SearchConstants.GOODS_BR_NAME_CN_UNTOKENIZED_FIELD, brand.getNameCn().toLowerCase(), Store.YES)); document.add(new BinaryDocValuesField(SearchConstants.GOODS_BR_NAME_CN_UNTOKENIZED_FIELD, new BytesRef(brand.getNameCn()))); } if (brand.getNameEn() != null) { document.add(new TextField(SearchConstants.GOODS_BR_NAME_EN_FIELD, brand.getNameEn(), Store.YES)); document.add(new StringField(SearchConstants.GOODS_BR_NAME_EN_UNTOKENIZED_FIELD, brand.getNameEn().toLowerCase(), Store.YES)); document.add(new BinaryDocValuesField(SearchConstants.GOODS_BR_NAME_EN_UNTOKENIZED_FIELD, new BytesRef(brand.getNameEn()))); } if (brand.getUuid() != null) { document.add(new StringField(SearchConstants.GOODS_BR_UUID_FIELD, brand.getUuid(), Store.YES)); } if (brand.getVisitCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.GOODS_BR_VISIT_COUNT_FIELD, brand.getVisitCount())); document.add(new LongField(SearchConstants.GOODS_BR_VISIT_COUNT_FIELD, brand.getVisitCount(), Store.YES)); } if (brand.getSearchCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.GOODS_BR_SEARCH_COUNT_FIELD, brand.getSearchCount())); document.add(new LongField(SearchConstants.GOODS_BR_SEARCH_COUNT_FIELD, brand.getSearchCount(), Store.YES)); } if (brand.getWeight() != null) { document.add(new DoubleDocValuesField(SearchConstants.GOODS_BR_WEIGHT_FIELD, brand.getWeight())); document.add(new DoubleField(SearchConstants.GOODS_BR_WEIGHT_FIELD, brand.getWeight(), Store.YES)); } primaryKey += "_" + component.getId(); } else { // 器件 id 为 null 时,存默认值,以便于后期搜索时做空值过滤 document.add(new StringField(SearchConstants.GOODS_CMP_ID_FIELD, NULL_VALUE, Store.YES)); primaryKey += "_" + NULL_VALUE; } 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()))); } } document.add(new StringField(SearchConstants.GOODS_PRIMARY_KEY_FIELD, primaryKey, Store.YES)); return document; } /** * PCBGoods 对象转为 Document * * @param pcbGoods * @return */ public static Document toDocument(PCBGoods pcbGoods) { if (pcbGoods == null || (pcbGoods.getTradeGoods() != null && pcbGoods.getTradeGoods().getId() == null) || pcbGoods.getPcb() == null || pcbGoods.getPcb().getId() == null || pcbGoods.getPcb().getKind() == null || pcbGoods.getPcb().getBrand() == null || pcbGoods.getPcb().getProducts() == null || pcbGoods.getPcb().getProducts().getId() == null) { return null; } String primaryKey = ""; Document document = new Document(); if(pcbGoods.getTradeGoods() != null){ TradeGoods tradeGoods = pcbGoods.getTradeGoods(); document.add(new StringField(SearchConstants.PCB_GOODS_GO_ID_FIELD, String.valueOf(tradeGoods.getId()), Store.YES)); if (tradeGoods.getReserve() != null) { document.add(new DoubleDocValuesField(SearchConstants.PCB_GOODS_GO_RESERVE_FIELD, tradeGoods.getReserve())); document.add(new DoubleField(SearchConstants.PCB_GOODS_GO_RESERVE_FIELD, tradeGoods.getReserve(), Store.YES)); } if (tradeGoods.getStatus() != null) { document.add(new StringField(SearchConstants.PCB_GOODS_GO_STATUS_FIELD, String.valueOf(tradeGoods.getStatus()), Store.YES)); } if (tradeGoods.getMinPriceRMB() != null) { document.add( new DoubleDocValuesField(SearchConstants.PCB_GOODS_GO_MINPRICERMB_FIELD, tradeGoods.getMinPriceRMB())); document.add( new DoubleField(SearchConstants.PCB_GOODS_GO_MINPRICERMB_FIELD, tradeGoods.getMinPriceRMB(), Store.YES)); } if (tradeGoods.getMinPriceUSD() != null) { document.add( new DoubleDocValuesField(SearchConstants.PCB_GOODS_GO_MINPRICEUSD_FIELD, tradeGoods.getMinPriceUSD())); document.add( new DoubleField(SearchConstants.PCB_GOODS_GO_MINPRICEUSD_FIELD, tradeGoods.getMinPriceUSD(), Store.YES)); } if (tradeGoods.getCrName() != null) { document.add(new TextField(SearchConstants.PCB_GOODS_CRNAME_FIELD, tradeGoods.getCrName(), Store.YES)); } if (tradeGoods.getVisitCount() != null) { document.add(new DoubleDocValuesField(SearchConstants.PCB_GOODS_GO_VISIT_COUNT_FIELD, tradeGoods.getVisitCount())); document.add(new LongField(SearchConstants.PCB_GOODS_GO_VISIT_COUNT_FIELD, tradeGoods.getVisitCount(), Store.YES)); } if (tradeGoods.getUpdateDate() != null) { document.add(new DoubleDocValuesField(SearchConstants.PCB_GOODS_GO_UPDATE_DATE_FIELD, tradeGoods.getUpdateDate().getTime())); document.add(new LongField(SearchConstants.PCB_GOODS_GO_UPDATE_DATE_FIELD, tradeGoods.getUpdateDate().getTime(), Store.YES)); } if (tradeGoods.getMinDelivery() != null) { document.add(new DoubleDocValuesField(SearchConstants.PCB_GOODS_GO_MINDELIVERY_FIELD, tradeGoods.getMinDelivery())); document.add(new LongField(SearchConstants.PCB_GOODS_GO_MINDELIVERY_FIELD, tradeGoods.getMinDelivery(), Store.YES)); } primaryKey += tradeGoods.getId(); } else { // 批次 id 为 null 时,存默认值,以便于后期搜索时做空值过滤 document.add(new StringField(SearchConstants.PCB_GOODS_GO_ID_FIELD, NULL_VALUE, Store.YES)); primaryKey += NULL_VALUE; } PCB pcb = pcbGoods.getPcb(); document.add(new StringField(SearchConstants.PCB_GOODS_PCB_ID_FIELD, String.valueOf(pcb.getId()), Store.YES)); Kind kind = pcb.getKind(); if (kind.getId() != null) { document.add(new StringField(SearchConstants.PCB_GOODS_KI_ID_FIELD, String.valueOf(kind.getId()), Store.YES)); } if (kind.getNameCn() != null) { document.add(new TextField(SearchConstants.PCB_GOODS_KI_NAME_CN_FIELD, kind.getNameCn(), Store.YES)); document.add(new StringField(SearchConstants.PCB_GOODS_KI_NAME_CN_UNTOKENIZED_FIELD, kind.getNameCn().toLowerCase(), Store.YES)); document.add(new BinaryDocValuesField(SearchConstants.PCB_GOODS_KI_NAME_CN_UNTOKENIZED_FIELD, new BytesRef(kind.getNameCn()))); } if (kind.getLevel() != null) { document.add(new NumericDocValuesField(SearchConstants.PCB_GOODS_KI_LEVEL_FIELD, kind.getLevel())); document.add(new LongField(SearchConstants.PCB_GOODS_KI_LEVEL_FIELD, kind.getLevel(), Store.YES)); } if (kind.getIsLeaf() != null) { document.add( new StringField(SearchConstants.PCB_GOODS_KI_ISLEAF_FIELD, String.valueOf(kind.getIsLeaf()), Store.YES)); } Brand brand = pcb.getBrand(); if (brand.getId() != null) { document.add(new StringField(SearchConstants.PCB_GOODS_BR_ID_FIELD, String.valueOf(brand.getId()), Store.YES)); } if (brand.getNameCn() != null) { document.add(new TextField(SearchConstants.PCB_GOODS_BR_NAME_CN_FIELD, brand.getNameCn(), Store.YES)); document.add(new StringField(SearchConstants.PCB_GOODS_BR_NAME_CN_UNTOKENIZED_FIELD, brand.getNameCn().toLowerCase(), Store.YES)); document.add(new BinaryDocValuesField(SearchConstants.PCB_GOODS_BR_NAME_CN_UNTOKENIZED_FIELD, new BytesRef(brand.getNameCn()))); } if (brand.getNameEn() != null) { document.add(new TextField(SearchConstants.PCB_GOODS_BR_NAME_EN_FIELD, brand.getNameEn(), Store.YES)); document.add(new StringField(SearchConstants.PCB_GOODS_BR_NAME_EN_UNTOKENIZED_FIELD, brand.getNameEn().toLowerCase(), Store.YES)); document.add(new BinaryDocValuesField(SearchConstants.PCB_GOODS_BR_NAME_EN_UNTOKENIZED_FIELD, new BytesRef(brand.getNameEn()))); } if (brand.getUuid() != null) { document.add(new StringField(SearchConstants.PCB_GOODS_BR_UUID_FIELD, brand.getUuid(), Store.YES)); } primaryKey += "_" + pcb.getId(); Products products = pcb.getProducts(); document.add(new StringField(SearchConstants.PCB_GOODS_PR_ID_FIELD, String.valueOf(products.getId()), Store.YES)); if (products.getPcmpCode() != null) { document.add(new StringField(SearchConstants.PCB_GOODS_PR_PCMPCODE_FIELD, products.getPcmpCode().toLowerCase(), Store.YES)); document.add(new BinaryDocValuesField(SearchConstants.PCB_GOODS_PR_PCMPCODE_FIELD, new BytesRef(products.getPcmpCode()))); } document.add(new StringField(SearchConstants.PCB_GOODS_PRIMARY_KEY_FIELD, primaryKey, Store.YES)); return document; } /** * Order对象转为Document * * @param order * Order对象 * @return 转换的Document */ public static Document toDocument(Order order) { if (order == null || order.getId() == null || StringUtils.isEmpty(order.getCode()) || order.getBuyeruu() == null || StringUtils.isEmpty(order.getBuyername()) || order.getBuyerEnterprise() == null || order.getBuyerEnterprise().getUu() == null || StringUtils.isEmpty(order.getBuyerEnterprise().getEnName()) || order.getCreatetime() == null || order.getStatus() == null) { return null; } Document document = new Document(); document.add(new StringField(SearchConstants.ORDER_ID_FIELD, String.valueOf(order.getId()), Store.YES)); // 利用正则搜索,code等不会被分词为多个词的字段存为TextField document.add(new TextField(SearchConstants.ORDER_CODE_FIELD, order.getCode(), Store.YES)); document.add( new StringField(SearchConstants.ORDER_BUYERUU_FIELD, String.valueOf(order.getBuyeruu()), Store.YES)); // 利用正则搜索,name等会被分词为多个词的字段存为StringField(不然会有边界问题,影响准确度) document.add(new StringField(SearchConstants.ORDER_BUYERNAME_FIELD, order.getBuyername(), Store.YES)); document.add(new StringField(SearchConstants.ORDER_BUYERENUU_FIELD, String.valueOf(order.getBuyerEnterprise().getUu()), Store.YES)); document.add(new StringField(SearchConstants.ORDER_BUYERENNAME_FIELD, order.getBuyerEnterprise().getEnName(), Store.YES)); if (order.getSellerEnterprise() != null) { if (order.getSellerEnterprise().getUu() != null) { document.add(new StringField(SearchConstants.ORDER_SELLERENUU_FIELD, String.valueOf(order.getSellerEnterprise().getUu()), Store.YES)); } if (order.getSellerEnterprise().getEnName() != null) { document.add(new StringField(SearchConstants.ORDER_SELLERENNAME_FIELD, order.getSellerEnterprise().getEnName(), Store.YES)); } } document.add(new LongField(SearchConstants.CREATETIME_FIELD, order.getCreatetime().getTime(), Store.YES)); document.add(new StringField(SearchConstants.ORDER_STATUS_FIELD, String.valueOf(order.getStatus()), Store.YES)); // 明细以json的格式存储 if (!CollectionUtils.isEmpty(order.getDetails())) { document.add(new TextField(SearchConstants.ORDER_DETAILS_FIELD, JSONObject.toJSONString(order.getDetails()), Store.YES)); } return document; } /** * OrderInvoice对象转为Document * * @param orderInvoice * OrderInvoice对象 * @return 转换的Document */ public static Document toDocument(OrderInvoice orderInvoice) { if (orderInvoice == null || orderInvoice.getId() == null || StringUtils.isEmpty(orderInvoice.getCode()) || orderInvoice.getBuyeruu() == null || StringUtils.isEmpty(orderInvoice.getBuyername()) || orderInvoice.getBuyerEnterprise() == null || orderInvoice.getBuyerEnterprise().getUu() == null || StringUtils.isEmpty(orderInvoice.getBuyerEnterprise().getEnName()) || orderInvoice.getCreatetime() == null || orderInvoice.getStatus() == null) { return null; } Document document = new Document(); document.add(new StringField(SearchConstants.ORDER_INVOICE_ID_FIELD, String.valueOf(orderInvoice.getId()), Store.YES)); document.add(new TextField(SearchConstants.ORDER_INVOICE_CODE_FIELD, orderInvoice.getCode(), Store.YES)); document.add(new StringField(SearchConstants.ORDER_INVOICE_BUYERUU_FIELD, String.valueOf(orderInvoice.getBuyeruu()), Store.YES)); document.add( new StringField(SearchConstants.ORDER_INVOICE_BUYERNAME_FIELD, orderInvoice.getBuyername(), Store.YES)); document.add(new StringField(SearchConstants.ORDER_INVOICE_BUYERENUU_FIELD, String.valueOf(orderInvoice.getBuyerEnterprise().getUu()), Store.YES)); document.add(new StringField(SearchConstants.ORDER_INVOICE_BUYERENNAME_FIELD, orderInvoice.getBuyerEnterprise().getEnName(), Store.YES)); document.add( new LongField(SearchConstants.CREATETIME_FIELD, orderInvoice.getCreatetime().getTime(), Store.YES)); document.add(new StringField(SearchConstants.ORDER_INVOICE_STATUS_FIELD, String.valueOf(orderInvoice.getStatus()), Store.YES)); if (!CollectionUtils.isEmpty(orderInvoice.getDetails())) { document.add(new TextField(SearchConstants.ORDER_INVOICE_DETAILS_FIELD, JSONObject.toJSONString(orderInvoice.getDetails()), Store.YES)); } return document; } /** * Purchase对象转为Document * * @param purchase * Purchase对象 * @return 转换的Document */ public static Document toDocument(Purchase purchase) { if (purchase == null || purchase.getId() == null || StringUtils.isEmpty(purchase.getCode()) || purchase.getSellerenuu() == null || StringUtils.isEmpty(purchase.getSellerenname()) || purchase.getCreatetime() == null || purchase.getStatus() == null) { return null; } Document document = new Document(); document.add(new StringField(SearchConstants.PURCHASE_ID_FIELD, String.valueOf(purchase.getId()), Store.YES)); document.add(new TextField(SearchConstants.PURCHASE_CODE_FIELD, purchase.getCode(), Store.YES)); document.add(new StringField(SearchConstants.PURCHASE_SELLERENUU_FIELD, String.valueOf(purchase.getSellerenuu()), Store.YES)); document.add( new StringField(SearchConstants.PURCHASE_SELLERENNAME_FIELD, purchase.getSellerenname(), Store.YES)); document.add(new LongField(SearchConstants.CREATETIME_FIELD, purchase.getCreatetime().getTime(), Store.YES)); document.add(new StringField(SearchConstants.PURCHASE_STATUS_FIELD, String.valueOf(purchase.getStatus()), Store.YES)); if (!CollectionUtils.isEmpty(purchase.getDetails())) { document.add(new TextField(SearchConstants.PURCHASE_DETAILS_FIELD, JSONObject.toJSONString(purchase.getDetails()), Store.YES)); } return document; } /** * PurchaseInvoice对象转为Document * * @param purchaseInvoice * PurchaseInvoice对象 * @return 转换的Document */ public static Document toDocument(PurchaseInvoice purchaseInvoice) { if (purchaseInvoice == null || purchaseInvoice.getId() == null || StringUtils.isEmpty(purchaseInvoice.getCode()) || purchaseInvoice.getSellerenuu() == null || StringUtils.isEmpty(purchaseInvoice.getSellerenname()) || purchaseInvoice.getCreatetime() == null || purchaseInvoice.getStatus() == null) { return null; } Document document = new Document(); document.add(new StringField(SearchConstants.PURCHASE_INVOICE_ID_FIELD, String.valueOf(purchaseInvoice.getId()), Store.YES)); document.add(new TextField(SearchConstants.PURCHASE_INVOICE_CODE_FIELD, purchaseInvoice.getCode(), Store.YES)); document.add(new StringField(SearchConstants.PURCHASE_INVOICE_SELLERENUU_FIELD, String.valueOf(purchaseInvoice.getSellerenuu()), Store.YES)); document.add(new StringField(SearchConstants.PURCHASE_INVOICE_SELLERENNAME_FIELD, purchaseInvoice.getSellerenname(), Store.YES)); document.add( new LongField(SearchConstants.CREATETIME_FIELD, purchaseInvoice.getCreatetime().getTime(), Store.YES)); document.add(new StringField(SearchConstants.PURCHASE_INVOICE_STATUS_FIELD, String.valueOf(purchaseInvoice.getStatus()), Store.YES)); if (!CollectionUtils.isEmpty(purchaseInvoice.getDetails())) { document.add(new TextField(SearchConstants.PURCHASE_INVOICE_DETAILS_FIELD, JSONObject.toJSONString(purchaseInvoice.getDetails()), Store.YES)); } return document; } }