ObjectToDocumentUtils.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. package com.uas.search.util;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.uas.search.constant.SearchConstants;
  4. import com.uas.search.model.*;
  5. import net.sf.ehcache.search.SearchException;
  6. import org.apache.lucene.document.*;
  7. import org.apache.lucene.document.Field.Store;
  8. import org.apache.lucene.util.BytesRef;
  9. import java.util.Set;
  10. /**
  11. * 将对象转换为Document的工具类
  12. *
  13. * @author sunyj
  14. * @since 2016年10月17日 上午11:32:19
  15. */
  16. public class ObjectToDocumentUtils {
  17. /**
  18. * 空值
  19. */
  20. public static final String NULL_VALUE = "NULLVALUE";
  21. /**
  22. * 将对象转为Document
  23. *
  24. * @param object
  25. * 对象,可为Kind、Brand、Component、
  26. * Order、OrderInvoice、Purchase、
  27. * PurchaseInvoice
  28. *
  29. * @return 转换的Document
  30. */
  31. public static Document toDocument(Object object) {
  32. if (object == null) {
  33. return null;
  34. }
  35. if (object instanceof Kind) {
  36. return toDocument((Kind) object);
  37. } else if (object instanceof Brand) {
  38. return toDocument((Brand) object);
  39. } else if (object instanceof Component) {
  40. return toDocument((Component) object);
  41. } else if (object instanceof Goods) {
  42. return toDocument((Goods) object);
  43. } else if (object instanceof Order) {
  44. return toDocument((Order) object);
  45. } else if (object instanceof OrderInvoice) {
  46. return toDocument((OrderInvoice) object);
  47. } else if (object instanceof Purchase) {
  48. return toDocument((Purchase) object);
  49. } else if (object instanceof PurchaseInvoice) {
  50. return toDocument((PurchaseInvoice) object);
  51. } else {
  52. throw new SearchException("不支持将以下类型转换为Document:" + object.getClass().getName());
  53. }
  54. }
  55. /**
  56. * Kind对象转为Document
  57. *
  58. * @param kind
  59. * @return
  60. */
  61. public static Document toDocument(Kind kind) {
  62. if (kind == null || kind.getId() == null || StringUtils.isEmpty(kind.getNameCn()) || kind.getIsLeaf() == null
  63. || kind.getLevel() == null) {
  64. return null;
  65. }
  66. Document document = new Document();
  67. // 不能用LongField,否则后续实时更新索引时,方法updateDocument(new Term("", ""),
  68. // doc)无法根据id进行更新
  69. document.add(new StringField(SearchConstants.KIND_ID_FIELD, String.valueOf(kind.getId()), Store.YES));
  70. document.add(new TextField(SearchConstants.KIND_NAMECN_FIELD, kind.getNameCn(), Store.YES));
  71. document.add(new StringField(SearchConstants.KIND_NAMECN_UNTOKENIZED_FIELD, kind.getNameCn().toLowerCase(), Store.YES));
  72. document.add(new BinaryDocValuesField(SearchConstants.KIND_NAMECN_UNTOKENIZED_FIELD, new BytesRef(kind.getNameCn())));
  73. document.add(new StringField(SearchConstants.KIND_ISLEAF_FIELD, String.valueOf(kind.getIsLeaf()), Store.YES));
  74. document.add(new StringField(SearchConstants.KIND_LEVEL_FIELD, String.valueOf(kind.getLevel()), Store.YES));
  75. if (kind.getVisitCount() != null) {
  76. document.add(new DoubleDocValuesField(SearchConstants.KIND_VISIT_COUNT_FIELD, kind.getVisitCount()));
  77. document.add(new LongField(SearchConstants.KIND_VISIT_COUNT_FIELD, kind.getVisitCount(), Store.YES));
  78. }
  79. if (kind.getSearchCount() != null) {
  80. document.add(new DoubleDocValuesField(SearchConstants.KIND_SEARCH_COUNT_FIELD, kind.getSearchCount()));
  81. document.add(new LongField(SearchConstants.KIND_SEARCH_COUNT_FIELD, kind.getSearchCount(), Store.YES));
  82. }
  83. return document;
  84. }
  85. /**
  86. * Brand对象转为Document
  87. *
  88. * @param brand
  89. * @return
  90. */
  91. public static Document toDocument(Brand brand) {
  92. if (brand == null || brand.getId() == null || StringUtils.isEmpty(brand.getNameCn())
  93. || StringUtils.isEmpty(brand.getUuid())) {
  94. return null;
  95. }
  96. Document document = new Document();
  97. document.add(new StringField(SearchConstants.BRAND_ID_FIELD, String.valueOf(brand.getId()), Store.YES));
  98. document.add(new TextField(SearchConstants.BRAND_NAMECN_FIELD, brand.getNameCn(), Store.YES));
  99. document.add(new StringField(SearchConstants.BRAND_NAMECN_UNTOKENIZED_FIELD, brand.getNameCn().toLowerCase(), Store.YES));
  100. document.add(new BinaryDocValuesField(SearchConstants.BRAND_NAMECN_UNTOKENIZED_FIELD, new BytesRef(brand.getNameCn())));
  101. document.add(new StringField(SearchConstants.BRAND_UUID_FIELD, brand.getUuid(), Store.YES));
  102. if (brand.getNameEn() != null) {
  103. document.add(new TextField(SearchConstants.BRAND_NAMEEN_FIELD, brand.getNameEn(), Store.YES));
  104. document.add(new StringField(SearchConstants.BRAND_NAMEEN_UNTOKENIZED_FIELD, brand.getNameEn().toLowerCase(), Store.YES));
  105. document.add(new BinaryDocValuesField(SearchConstants.BRAND_NAMEEN_UNTOKENIZED_FIELD, new BytesRef(brand.getNameEn())));
  106. }
  107. if (brand.getVisitCount() != null) {
  108. document.add(new DoubleDocValuesField(SearchConstants.BRAND_VISIT_COUNT_FIELD, brand.getVisitCount()));
  109. document.add(new LongField(SearchConstants.BRAND_VISIT_COUNT_FIELD, brand.getVisitCount(), Store.YES));
  110. }
  111. if (brand.getSearchCount() != null) {
  112. document.add(new DoubleDocValuesField(SearchConstants.BRAND_SEARCH_COUNT_FIELD, brand.getSearchCount()));
  113. document.add(new LongField(SearchConstants.BRAND_SEARCH_COUNT_FIELD, brand.getSearchCount(), Store.YES));
  114. }
  115. if (brand.getWeight() != null) {
  116. document.add(new DoubleDocValuesField(SearchConstants.BRAND_WEIGHT_FIELD, brand.getWeight()));
  117. document.add(new DoubleField(SearchConstants.BRAND_WEIGHT_FIELD, brand.getWeight(), Store.YES));
  118. }
  119. return document;
  120. }
  121. /**
  122. * Component对象转为Document
  123. *
  124. * @param component
  125. * @return
  126. */
  127. public static Document toDocument(Component component) {
  128. if (component == null || component.getId() == null || StringUtils.isEmpty(component.getUuid())
  129. || StringUtils.isEmpty(component.getCode()) || component.getKind() == null
  130. || component.getKind().getId() == null || StringUtils.isEmpty(component.getKind().getNameCn())
  131. || component.getKind().getLevel() == null || component.getBrand() == null
  132. || component.getBrand().getId() == null || StringUtils.isEmpty(component.getBrand().getNameCn())
  133. || StringUtils.isEmpty(component.getBrand().getUuid())) {
  134. return null;
  135. }
  136. Document document = new Document();
  137. document.add(new StringField(SearchConstants.COMPONENT_ID_FIELD, String.valueOf(component.getId()), Store.YES));
  138. document.add(new StringField(SearchConstants.COMPONENT_UUID_FIELD, component.getUuid(), Store.YES));
  139. // 转小写,以避免分词,又不会因大小写影响搜索
  140. document.add(new StringField(SearchConstants.COMPONENT_CODE_FIELD, component.getCode().toLowerCase(), Store.YES));
  141. document.add(new BinaryDocValuesField(SearchConstants.COMPONENT_CODE_FIELD, new BytesRef(component.getCode())));
  142. if (component.getVisitCount() != null) {
  143. document.add(new DoubleDocValuesField(SearchConstants.COMPONENT_VISIT_COUNT_FIELD, component.getVisitCount()));
  144. document.add(new LongField(SearchConstants.COMPONENT_VISIT_COUNT_FIELD, component.getVisitCount(), Store.YES));
  145. }
  146. if (component.getSearchCount() != null) {
  147. document.add(new DoubleDocValuesField(SearchConstants.COMPONENT_SEARCH_COUNT_FIELD, component.getSearchCount()));
  148. document.add(new LongField(SearchConstants.COMPONENT_SEARCH_COUNT_FIELD, component.getSearchCount(), Store.YES));
  149. }
  150. Kind kind = component.getKind();
  151. document.add(new StringField(SearchConstants.COMPONENT_KI_ID_FIELD, String.valueOf(kind.getId()), Store.YES));
  152. document.add(new TextField(SearchConstants.COMPONENT_KI_NAME_FIELD, kind.getNameCn(), Store.YES));
  153. document.add(new StringField(SearchConstants.COMPONENT_KI_NAME_UNTOKENIZED_FIELD, kind.getNameCn().toLowerCase(), Store.YES));
  154. document.add(new BinaryDocValuesField(SearchConstants.COMPONENT_KI_NAME_UNTOKENIZED_FIELD, new BytesRef(kind.getNameCn())));
  155. document.add(new StringField(SearchConstants.COMPONENT_KI_LEVEL_FIELD, String.valueOf(kind.getLevel()), Store.YES));
  156. if (kind.getVisitCount() != null) {
  157. document.add(new DoubleDocValuesField(SearchConstants.COMPONENT_KI_VISIT_COUNT_FIELD, kind.getVisitCount()));
  158. document.add(new LongField(SearchConstants.COMPONENT_KI_VISIT_COUNT_FIELD, kind.getVisitCount(), Store.YES));
  159. }
  160. if (kind.getSearchCount() != null) {
  161. document.add(new DoubleDocValuesField(SearchConstants.COMPONENT_KI_SEARCH_COUNT_FIELD, kind.getSearchCount()));
  162. document.add(new LongField(SearchConstants.COMPONENT_KI_SEARCH_COUNT_FIELD, kind.getSearchCount(), Store.YES));
  163. }
  164. Brand brand = component.getBrand();
  165. document.add(new StringField(SearchConstants.COMPONENT_BR_ID_FIELD, String.valueOf(brand.getId()), Store.YES));
  166. document.add(new TextField(SearchConstants.COMPONENT_BR_NAMECN_FIELD, brand.getNameCn(), Store.YES));
  167. document.add(new StringField(SearchConstants.COMPONENT_BR_NAMECN_UNTOKENIZED_FIELD, brand.getNameCn().toLowerCase(), Store.YES));
  168. document.add(new BinaryDocValuesField(SearchConstants.COMPONENT_BR_NAMECN_UNTOKENIZED_FIELD, new BytesRef(brand.getNameCn())));
  169. if(brand.getNameEn() != null){
  170. document.add(new TextField(SearchConstants.COMPONENT_BR_NAMEEN_FIELD, brand.getNameEn(), Store.YES));
  171. document.add(new StringField(SearchConstants.COMPONENT_BR_NAMEEN_UNTOKENIZED_FIELD, brand.getNameEn().toLowerCase(), Store.YES));
  172. document.add(new BinaryDocValuesField(SearchConstants.COMPONENT_BR_NAMEEN_UNTOKENIZED_FIELD, new BytesRef(brand.getNameEn())));
  173. }
  174. document.add(new StringField(SearchConstants.COMPONENT_BR_UUID_FIELD, brand.getUuid(), Store.YES));
  175. if (brand.getVisitCount() != null) {
  176. document.add(new DoubleDocValuesField(SearchConstants.COMPONENT_BR_VISIT_COUNT_FIELD, brand.getVisitCount()));
  177. document.add(new LongField(SearchConstants.COMPONENT_BR_VISIT_COUNT_FIELD, brand.getVisitCount(), Store.YES));
  178. }
  179. if (brand.getSearchCount() != null) {
  180. document.add(new DoubleDocValuesField(SearchConstants.COMPONENT_BR_SEARCH_COUNT_FIELD, brand.getSearchCount()));
  181. document.add(new LongField(SearchConstants.COMPONENT_BR_SEARCH_COUNT_FIELD, brand.getSearchCount(), Store.YES));
  182. }
  183. if (brand.getWeight() != null) {
  184. document.add(new DoubleDocValuesField(SearchConstants.COMPONENT_BR_WEIGHT_FIELD, brand.getWeight()));
  185. document.add(new DoubleField(SearchConstants.COMPONENT_BR_WEIGHT_FIELD, brand.getWeight(), Store.YES));
  186. }
  187. if (component.getReserve() != null) {
  188. document.add(new DoubleField(SearchConstants.COMPONENT_RESERVE_FIELD, component.getReserve(), Store.YES));
  189. }
  190. if (component.getSampleQty() != null) {
  191. document.add(
  192. new DoubleField(SearchConstants.COMPONENT_SAMPLE_QTY_FIELD, component.getSampleQty(), Store.YES));
  193. }
  194. if (component.getOriginalQty() != null) {
  195. document.add(new DoubleField(SearchConstants.COMPONENT_ORIGINAL_QTY_FIELD, component.getOriginalQty(),
  196. Store.YES));
  197. }
  198. if (component.getInactionStockQty() != null) {
  199. document.add(new DoubleField(SearchConstants.COMPONENT_INACTION_STOCK_QTY_FIELD,
  200. component.getInactionStockQty(), Store.YES));
  201. }
  202. // 属性值加入索引,索引中field的键:"pr_"前缀连接属性的id
  203. Set<PropertyValue> propertyValues = component.getProperties();
  204. for (PropertyValue propertyValue : propertyValues) {
  205. if (!StringUtils.isEmpty(propertyValue.getValue())) {
  206. String fieldKey = SearchConstants.COMPONENT_PROPERTY_PREFIX + propertyValue.getPropertyid();
  207. document.add(new StringField(fieldKey, propertyValue.getValue(), Store.YES));
  208. // 另建一份分词的属性索引,用于属性值联想时不区分大小写
  209. String fieldKeyTokenized = fieldKey + SearchConstants.COMPONENT_PROPERTY_TOKENIZED_SUFFIX;
  210. document.add(new TextField(fieldKeyTokenized, propertyValue.getValue(), Store.YES));
  211. }
  212. }
  213. return document;
  214. }
  215. /**
  216. * Goods对象转为Document
  217. *
  218. * @param goods
  219. * @return
  220. */
  221. public static Document toDocument(Goods goods) {
  222. if (goods == null ||
  223. (goods.getComponent() != null &&
  224. (StringUtils.isEmpty(goods.getComponent().getId()) ||
  225. StringUtils.isEmpty(goods.getComponent().getCode()) ||
  226. goods.getComponent().getKind() == null ||
  227. goods.getComponent().getBrand() == null))) {
  228. return null;
  229. }
  230. Document document = new Document();
  231. if(goods.getTradeGoods() != null){
  232. TradeGoods tradeGoods = goods.getTradeGoods();
  233. document.add(new StringField(SearchConstants.GOODS_GO_ID_FIELD, String.valueOf(tradeGoods.getId()), Store.YES));
  234. if (tradeGoods.getReserve() != null) {
  235. document.add(new DoubleDocValuesField(SearchConstants.GOODS_GO_RESERVE_FIELD, tradeGoods.getReserve()));
  236. document.add(new DoubleField(SearchConstants.GOODS_GO_RESERVE_FIELD, tradeGoods.getReserve(), Store.YES));
  237. }
  238. if (tradeGoods.getStatus() != null) {
  239. document.add(new StringField(SearchConstants.GOODS_GO_STATUS_FIELD, String.valueOf(tradeGoods.getStatus()), Store.YES));
  240. }
  241. if (tradeGoods.getMinPriceRMB() != null) {
  242. document.add(
  243. new DoubleDocValuesField(SearchConstants.GOODS_GO_MINPRICERMB_FIELD, tradeGoods.getMinPriceRMB()));
  244. document.add(
  245. new DoubleField(SearchConstants.GOODS_GO_MINPRICERMB_FIELD, tradeGoods.getMinPriceRMB(), Store.YES));
  246. }
  247. if (tradeGoods.getMinPriceUSD() != null) {
  248. document.add(
  249. new DoubleDocValuesField(SearchConstants.GOODS_GO_MINPRICEUSD_FIELD, tradeGoods.getMinPriceUSD()));
  250. document.add(
  251. new DoubleField(SearchConstants.GOODS_GO_MINPRICEUSD_FIELD, tradeGoods.getMinPriceUSD(), Store.YES));
  252. }
  253. if (tradeGoods.getCrName() != null) {
  254. document.add(new TextField(SearchConstants.GOODS_CRNAME_FIELD, tradeGoods.getCrName(), Store.YES));
  255. }
  256. if (tradeGoods.getVisitCount() != null) {
  257. document.add(new DoubleDocValuesField(SearchConstants.GOODS_GO_VISIT_COUNT_FIELD, tradeGoods.getVisitCount()));
  258. document.add(new LongField(SearchConstants.GOODS_GO_VISIT_COUNT_FIELD, tradeGoods.getVisitCount(), Store.YES));
  259. }
  260. if (tradeGoods.getUpdateDate() != null) {
  261. document.add(new DoubleDocValuesField(SearchConstants.GOODS_GO_UPDATE_DATE_FIELD, tradeGoods.getUpdateDate().getTime()));
  262. document.add(new LongField(SearchConstants.GOODS_GO_UPDATE_DATE_FIELD, tradeGoods.getUpdateDate().getTime(), Store.YES));
  263. }
  264. if (tradeGoods.getMinDelivery() != null) {
  265. document.add(new DoubleDocValuesField(SearchConstants.GOODS_GO_MINDELIVERY_FIELD, tradeGoods.getMinDelivery()));
  266. document.add(new LongField(SearchConstants.GOODS_GO_MINDELIVERY_FIELD, tradeGoods.getMinDelivery(), Store.YES));
  267. }
  268. } else {
  269. // 批次 id 为 null 时,存默认值,以便于后期搜索时做空值过滤
  270. document.add(new StringField(SearchConstants.GOODS_GO_ID_FIELD, NULL_VALUE, Store.YES));
  271. }
  272. if (goods.getStore() != null) {
  273. com.uas.search.model.Store store = goods.getStore();
  274. if (store.getUuid() != null) {
  275. document.add(new TextField(SearchConstants.GOODS_ST_UUID_FIELD, store.getUuid(), Store.YES));
  276. }
  277. if (store.getType() != null) {
  278. document.add(new TextField(SearchConstants.GOODS_ST_TYPE_FIELD, store.getType(), Store.YES));
  279. }
  280. }
  281. if(goods.getComponent() != null){
  282. Component component = goods.getComponent();
  283. document.add(new StringField(SearchConstants.GOODS_CMP_ID_FIELD, String.valueOf(component.getId()), Store.YES));
  284. document.add(new StringField(SearchConstants.GOODS_CMP_CODE_FIELD, component.getCode().toLowerCase(), Store.YES));
  285. document.add(new BinaryDocValuesField(SearchConstants.GOODS_CMP_CODE_FIELD, new BytesRef(component.getCode())));
  286. if (component.getDescription() != null) {
  287. document.add(new TextField(SearchConstants.GOODS_CMP_DESCRIPTION_FIELD, component.getDescription(), Store.YES));
  288. }
  289. if (component.getVisitCount() != null) {
  290. document.add(new DoubleDocValuesField(SearchConstants.GOODS_CMP_VISIT_COUNT_FIELD, component.getVisitCount()));
  291. document.add(new LongField(SearchConstants.GOODS_CMP_VISIT_COUNT_FIELD, component.getVisitCount(), Store.YES));
  292. }
  293. if (component.getSearchCount() != null) {
  294. document.add(new DoubleDocValuesField(SearchConstants.GOODS_CMP_SEARCH_COUNT_FIELD, component.getSearchCount()));
  295. document.add(new LongField(SearchConstants.GOODS_CMP_SEARCH_COUNT_FIELD, component.getSearchCount(), Store.YES));
  296. }
  297. Kind kind = component.getKind();
  298. if (kind.getId() != null) {
  299. document.add(new StringField(SearchConstants.GOODS_KI_ID_FIELD, String.valueOf(kind.getId()), Store.YES));
  300. }
  301. if (kind.getNameCn() != null) {
  302. document.add(new TextField(SearchConstants.GOODS_KI_NAME_CN_FIELD, kind.getNameCn(), Store.YES));
  303. document.add(new StringField(SearchConstants.GOODS_KI_NAME_CN_UNTOKENIZED_FIELD, kind.getNameCn().toLowerCase(), Store.YES));
  304. document.add(new BinaryDocValuesField(SearchConstants.GOODS_KI_NAME_CN_UNTOKENIZED_FIELD, new BytesRef( kind.getNameCn())));
  305. }
  306. if (kind.getLevel() != null) {
  307. document.add(new NumericDocValuesField(SearchConstants.GOODS_KI_LEVEL_FIELD, kind.getIsLeaf()));
  308. document.add(new LongField(SearchConstants.GOODS_KI_LEVEL_FIELD, kind.getIsLeaf(), Store.YES));
  309. }
  310. if (kind.getIsLeaf() != null) {
  311. document.add(
  312. new StringField(SearchConstants.GOODS_KI_ISLEAF_FIELD, String.valueOf(kind.getLevel()), Store.YES));
  313. }
  314. if (kind.getVisitCount() != null) {
  315. document.add(new DoubleDocValuesField(SearchConstants.GOODS_KI_VISIT_COUNT_FIELD, kind.getVisitCount()));
  316. document.add(new LongField(SearchConstants.GOODS_KI_VISIT_COUNT_FIELD, kind.getVisitCount(), Store.YES));
  317. }
  318. if (kind.getSearchCount() != null) {
  319. document.add(new DoubleDocValuesField(SearchConstants.GOODS_KI_SEARCH_COUNT_FIELD, kind.getSearchCount()));
  320. document.add(new LongField(SearchConstants.GOODS_KI_SEARCH_COUNT_FIELD, kind.getSearchCount(), Store.YES));
  321. }
  322. Brand brand = component.getBrand();
  323. if (brand.getId() != null) {
  324. document.add(new StringField(SearchConstants.GOODS_BR_ID_FIELD, String.valueOf(brand.getId()), Store.YES));
  325. }
  326. if (brand.getNameCn() != null) {
  327. document.add(new TextField(SearchConstants.GOODS_BR_NAME_CN_FIELD, brand.getNameCn(), Store.YES));
  328. document.add(new StringField(SearchConstants.GOODS_BR_NAME_CN_UNTOKENIZED_FIELD, brand.getNameCn().toLowerCase(), Store.YES));
  329. document.add(new BinaryDocValuesField(SearchConstants.GOODS_BR_NAME_CN_UNTOKENIZED_FIELD, new BytesRef(brand.getNameCn())));
  330. }
  331. if (brand.getNameEn() != null) {
  332. document.add(new TextField(SearchConstants.GOODS_BR_NAME_EN_FIELD, brand.getNameEn(), Store.YES));
  333. document.add(new StringField(SearchConstants.GOODS_BR_NAME_EN_UNTOKENIZED_FIELD, brand.getNameEn().toLowerCase(), Store.YES));
  334. document.add(new BinaryDocValuesField(SearchConstants.GOODS_BR_NAME_EN_UNTOKENIZED_FIELD, new BytesRef(brand.getNameEn())));
  335. }
  336. if (brand.getUuid() != null) {
  337. document.add(new StringField(SearchConstants.GOODS_BR_UUID_FIELD, brand.getUuid(), Store.YES));
  338. }
  339. if (brand.getVisitCount() != null) {
  340. document.add(new DoubleDocValuesField(SearchConstants.GOODS_BR_VISIT_COUNT_FIELD, brand.getVisitCount()));
  341. document.add(new LongField(SearchConstants.GOODS_BR_VISIT_COUNT_FIELD, brand.getVisitCount(), Store.YES));
  342. }
  343. if (brand.getSearchCount() != null) {
  344. document.add(new DoubleDocValuesField(SearchConstants.GOODS_BR_SEARCH_COUNT_FIELD, brand.getSearchCount()));
  345. document.add(new LongField(SearchConstants.GOODS_BR_SEARCH_COUNT_FIELD, brand.getSearchCount(), Store.YES));
  346. }
  347. if (brand.getWeight() != null) {
  348. document.add(new DoubleDocValuesField(SearchConstants.GOODS_BR_WEIGHT_FIELD, brand.getWeight()));
  349. document.add(new DoubleField(SearchConstants.GOODS_BR_WEIGHT_FIELD, brand.getWeight(), Store.YES));
  350. }
  351. } else {
  352. // 器件 id 为 null 时,存默认值,以便于后期搜索时做空值过滤
  353. document.add(new StringField(SearchConstants.GOODS_CMP_ID_FIELD, NULL_VALUE, Store.YES));
  354. }
  355. if(goods.getProducts() != null){
  356. Products products = goods.getProducts();
  357. if (products.getId() != null) {
  358. document.add(new StringField(SearchConstants.GOODS_PR_ID_FIELD, String.valueOf(products.getId()), Store.YES));
  359. }
  360. if(products.getPcmpCode() != null){
  361. document.add(new StringField(SearchConstants.GOODS_PR_PCMPCODE_FIELD, products.getPcmpCode().toLowerCase(), Store.YES));
  362. document.add(new BinaryDocValuesField(SearchConstants.GOODS_PR_PCMPCODE_FIELD, new BytesRef(products.getPcmpCode())));
  363. }
  364. }
  365. return document;
  366. }
  367. /**
  368. * Order对象转为Document
  369. *
  370. * @param order
  371. * Order对象
  372. * @return 转换的Document
  373. */
  374. public static Document toDocument(Order order) {
  375. if (order == null || order.getId() == null || StringUtils.isEmpty(order.getCode()) || order.getBuyeruu() == null
  376. || StringUtils.isEmpty(order.getBuyername()) || order.getBuyerEnterprise() == null
  377. || order.getBuyerEnterprise().getUu() == null
  378. || StringUtils.isEmpty(order.getBuyerEnterprise().getEnName()) || order.getCreatetime() == null
  379. || order.getStatus() == null) {
  380. return null;
  381. }
  382. Document document = new Document();
  383. document.add(new StringField(SearchConstants.ORDER_ID_FIELD, String.valueOf(order.getId()), Store.YES));
  384. // 利用正则搜索,code等不会被分词为多个词的字段存为TextField
  385. document.add(new TextField(SearchConstants.ORDER_CODE_FIELD, order.getCode(), Store.YES));
  386. document.add(
  387. new StringField(SearchConstants.ORDER_BUYERUU_FIELD, String.valueOf(order.getBuyeruu()), Store.YES));
  388. // 利用正则搜索,name等会被分词为多个词的字段存为StringField(不然会有边界问题,影响准确度)
  389. document.add(new StringField(SearchConstants.ORDER_BUYERNAME_FIELD, order.getBuyername(), Store.YES));
  390. document.add(new StringField(SearchConstants.ORDER_BUYERENUU_FIELD,
  391. String.valueOf(order.getBuyerEnterprise().getUu()), Store.YES));
  392. document.add(new StringField(SearchConstants.ORDER_BUYERENNAME_FIELD, order.getBuyerEnterprise().getEnName(),
  393. Store.YES));
  394. if (order.getSellerEnterprise() != null) {
  395. if (order.getSellerEnterprise().getUu() != null) {
  396. document.add(new StringField(SearchConstants.ORDER_SELLERENUU_FIELD,
  397. String.valueOf(order.getSellerEnterprise().getUu()), Store.YES));
  398. }
  399. if (order.getSellerEnterprise().getEnName() != null) {
  400. document.add(new StringField(SearchConstants.ORDER_SELLERENNAME_FIELD,
  401. order.getSellerEnterprise().getEnName(), Store.YES));
  402. }
  403. }
  404. document.add(new LongField(SearchConstants.CREATETIME_FIELD, order.getCreatetime().getTime(), Store.YES));
  405. document.add(new StringField(SearchConstants.ORDER_STATUS_FIELD, String.valueOf(order.getStatus()), Store.YES));
  406. // 明细以json的格式存储
  407. if (!CollectionUtils.isEmpty(order.getDetails())) {
  408. document.add(new TextField(SearchConstants.ORDER_DETAILS_FIELD, JSONObject.toJSONString(order.getDetails()),
  409. Store.YES));
  410. }
  411. return document;
  412. }
  413. /**
  414. * OrderInvoice对象转为Document
  415. *
  416. * @param orderInvoice
  417. * OrderInvoice对象
  418. * @return 转换的Document
  419. */
  420. public static Document toDocument(OrderInvoice orderInvoice) {
  421. if (orderInvoice == null || orderInvoice.getId() == null || StringUtils.isEmpty(orderInvoice.getCode())
  422. || orderInvoice.getBuyeruu() == null || StringUtils.isEmpty(orderInvoice.getBuyername())
  423. || orderInvoice.getBuyerEnterprise() == null || orderInvoice.getBuyerEnterprise().getUu() == null
  424. || StringUtils.isEmpty(orderInvoice.getBuyerEnterprise().getEnName())
  425. || orderInvoice.getCreatetime() == null || orderInvoice.getStatus() == null) {
  426. return null;
  427. }
  428. Document document = new Document();
  429. document.add(new StringField(SearchConstants.ORDER_INVOICE_ID_FIELD, String.valueOf(orderInvoice.getId()),
  430. Store.YES));
  431. document.add(new TextField(SearchConstants.ORDER_INVOICE_CODE_FIELD, orderInvoice.getCode(), Store.YES));
  432. document.add(new StringField(SearchConstants.ORDER_INVOICE_BUYERUU_FIELD,
  433. String.valueOf(orderInvoice.getBuyeruu()), Store.YES));
  434. document.add(
  435. new StringField(SearchConstants.ORDER_INVOICE_BUYERNAME_FIELD, orderInvoice.getBuyername(), Store.YES));
  436. document.add(new StringField(SearchConstants.ORDER_INVOICE_BUYERENUU_FIELD,
  437. String.valueOf(orderInvoice.getBuyerEnterprise().getUu()), Store.YES));
  438. document.add(new StringField(SearchConstants.ORDER_INVOICE_BUYERENNAME_FIELD,
  439. orderInvoice.getBuyerEnterprise().getEnName(), Store.YES));
  440. document.add(
  441. new LongField(SearchConstants.CREATETIME_FIELD, orderInvoice.getCreatetime().getTime(), Store.YES));
  442. document.add(new StringField(SearchConstants.ORDER_INVOICE_STATUS_FIELD,
  443. String.valueOf(orderInvoice.getStatus()), Store.YES));
  444. if (!CollectionUtils.isEmpty(orderInvoice.getDetails())) {
  445. document.add(new TextField(SearchConstants.ORDER_INVOICE_DETAILS_FIELD,
  446. JSONObject.toJSONString(orderInvoice.getDetails()), Store.YES));
  447. }
  448. return document;
  449. }
  450. /**
  451. * Purchase对象转为Document
  452. *
  453. * @param purchase
  454. * Purchase对象
  455. * @return 转换的Document
  456. */
  457. public static Document toDocument(Purchase purchase) {
  458. if (purchase == null || purchase.getId() == null || StringUtils.isEmpty(purchase.getCode())
  459. || purchase.getSellerenuu() == null || StringUtils.isEmpty(purchase.getSellerenname())
  460. || purchase.getCreatetime() == null || purchase.getStatus() == null) {
  461. return null;
  462. }
  463. Document document = new Document();
  464. document.add(new StringField(SearchConstants.PURCHASE_ID_FIELD, String.valueOf(purchase.getId()), Store.YES));
  465. document.add(new TextField(SearchConstants.PURCHASE_CODE_FIELD, purchase.getCode(), Store.YES));
  466. document.add(new StringField(SearchConstants.PURCHASE_SELLERENUU_FIELD,
  467. String.valueOf(purchase.getSellerenuu()), Store.YES));
  468. document.add(
  469. new StringField(SearchConstants.PURCHASE_SELLERENNAME_FIELD, purchase.getSellerenname(), Store.YES));
  470. document.add(new LongField(SearchConstants.CREATETIME_FIELD, purchase.getCreatetime().getTime(), Store.YES));
  471. document.add(new StringField(SearchConstants.PURCHASE_STATUS_FIELD, String.valueOf(purchase.getStatus()),
  472. Store.YES));
  473. if (!CollectionUtils.isEmpty(purchase.getDetails())) {
  474. document.add(new TextField(SearchConstants.PURCHASE_DETAILS_FIELD,
  475. JSONObject.toJSONString(purchase.getDetails()), Store.YES));
  476. }
  477. return document;
  478. }
  479. /**
  480. * PurchaseInvoice对象转为Document
  481. *
  482. * @param purchaseInvoice
  483. * PurchaseInvoice对象
  484. * @return 转换的Document
  485. */
  486. public static Document toDocument(PurchaseInvoice purchaseInvoice) {
  487. if (purchaseInvoice == null || purchaseInvoice.getId() == null || StringUtils.isEmpty(purchaseInvoice.getCode())
  488. || purchaseInvoice.getSellerenuu() == null || StringUtils.isEmpty(purchaseInvoice.getSellerenname())
  489. || purchaseInvoice.getCreatetime() == null || purchaseInvoice.getStatus() == null) {
  490. return null;
  491. }
  492. Document document = new Document();
  493. document.add(new StringField(SearchConstants.PURCHASE_INVOICE_ID_FIELD, String.valueOf(purchaseInvoice.getId()),
  494. Store.YES));
  495. document.add(new TextField(SearchConstants.PURCHASE_INVOICE_CODE_FIELD, purchaseInvoice.getCode(), Store.YES));
  496. document.add(new StringField(SearchConstants.PURCHASE_INVOICE_SELLERENUU_FIELD,
  497. String.valueOf(purchaseInvoice.getSellerenuu()), Store.YES));
  498. document.add(new StringField(SearchConstants.PURCHASE_INVOICE_SELLERENNAME_FIELD,
  499. purchaseInvoice.getSellerenname(), Store.YES));
  500. document.add(
  501. new LongField(SearchConstants.CREATETIME_FIELD, purchaseInvoice.getCreatetime().getTime(), Store.YES));
  502. document.add(new StringField(SearchConstants.PURCHASE_INVOICE_STATUS_FIELD,
  503. String.valueOf(purchaseInvoice.getStatus()), Store.YES));
  504. if (!CollectionUtils.isEmpty(purchaseInvoice.getDetails())) {
  505. document.add(new TextField(SearchConstants.PURCHASE_INVOICE_DETAILS_FIELD,
  506. JSONObject.toJSONString(purchaseInvoice.getDetails()), Store.YES));
  507. }
  508. return document;
  509. }
  510. }