SearchController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. package com.uas.search.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.uas.search.constant.model.CollectField;
  4. import com.uas.search.constant.model.PageParams;
  5. import com.uas.search.constant.model.PageParams.FilterField;
  6. import com.uas.search.constant.model.SPage;
  7. import com.uas.search.dao.ComponentDao;
  8. import com.uas.search.model.*;
  9. import com.uas.search.service.SearchService;
  10. import com.uas.search.service.impl.IndexServiceImpl;
  11. import com.uas.search.util.CollectionUtils;
  12. import com.uas.search.util.FileUtils;
  13. import com.uas.search.util.SearchUtils;
  14. import com.uas.search.util.StringUtils;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.web.bind.annotation.PathVariable;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RequestParam;
  22. import org.springframework.web.bind.annotation.ResponseBody;
  23. import javax.servlet.http.HttpServletRequest;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.io.PrintWriter;
  27. import java.util.*;
  28. import java.util.Map.Entry;
  29. /**
  30. * 搜索请求
  31. *
  32. * @author suntg
  33. * @since 2016年8月1日上午9:18:05
  34. */
  35. @Controller
  36. @RequestMapping("/search")
  37. public class SearchController {
  38. @Autowired
  39. private SearchService searchService;
  40. private Logger logger = LoggerFactory.getLogger(getClass());
  41. /**
  42. * 查询所有物料
  43. * @param keyword 关键词
  44. * @param page 页码
  45. * @param size 尺寸
  46. * @param enUU 企业UU
  47. * @param type 类型 standard 标准 nStandard 非标 其他为所有
  48. * @param request request
  49. * @return idPage
  50. * @throws IOException 输入异常
  51. */
  52. @RequestMapping("/productIds")
  53. @ResponseBody
  54. public SPage<Long> searchProductIds(String keyword, Integer page, Integer size, Long enUU, String type,
  55. HttpServletRequest request) throws IOException {
  56. return searchService.getProductIds(enUU, keyword, page, size, type);
  57. }
  58. /**
  59. * 获取标准型号联想词
  60. * @param keyword 关键词
  61. * @param size 尺寸
  62. * @param request
  63. * @return
  64. */
  65. @RequestMapping("/product/similarPCmpCodes")
  66. @ResponseBody
  67. public List<Map<String, Object>> getSimilarPCmpCodes(@RequestParam String keyword, Integer size,
  68. HttpServletRequest request) {
  69. long start = System.currentTimeMillis();
  70. List<Map<String, Object>> temp = searchService.getSimilarPCmpCodes(keyword, size);
  71. System.out.println("eee" + (System.currentTimeMillis() - start));
  72. return temp;
  73. }
  74. /**
  75. * 获取类目联想词
  76. * @param keyword 关键词
  77. * @param size 尺寸
  78. * @param request
  79. * @return
  80. */
  81. @RequestMapping("/product/similarKind")
  82. @ResponseBody
  83. public List<Map<String, Object>> getSimilarKind(@RequestParam String keyword, Integer size,
  84. HttpServletRequest request) {
  85. return searchService.getSimilarKind(keyword, size);
  86. }
  87. /**
  88. * 根据id获取索引详情
  89. * @param id
  90. * @param request
  91. * @return
  92. * @throws IOException
  93. */
  94. @RequestMapping("/product/{id}")
  95. @ResponseBody
  96. public V_Products getProduct(@PathVariable Long id, HttpServletRequest request) throws IOException {
  97. return searchService.getProduct(id);
  98. }
  99. @RequestMapping("/kindIds")
  100. @ResponseBody
  101. public SPage<Long> seachKindIds(@RequestParam String keyword, Integer page, Integer size,
  102. HttpServletRequest request) throws IOException {
  103. return searchService.getKindIds(keyword, page, size);
  104. }
  105. @RequestMapping("/kinds")
  106. @ResponseBody
  107. public SPage<Map<String, Object>> seachKinds(@RequestParam String keyword, Integer page, Integer size,
  108. HttpServletRequest request) throws IOException {
  109. return searchService.getKinds(keyword, page, size);
  110. }
  111. @RequestMapping("/brandIds")
  112. @ResponseBody
  113. public SPage<Long> searchBrandIds(@RequestParam String keyword, Integer page, Integer size,
  114. HttpServletRequest request) throws IOException {
  115. return searchService.getBrandIds(keyword, page, size);
  116. }
  117. @RequestMapping("/brands")
  118. @ResponseBody
  119. public SPage<Map<String, Object>> searchBrand(@RequestParam String keyword, Integer page, Integer size,
  120. HttpServletRequest request) throws IOException {
  121. return searchService.getBrands(keyword, page, size);
  122. }
  123. /**
  124. * 根据关键词搜索品牌(精确命中后,提供卖家信息)
  125. * @param keyword 关键词
  126. * @param page 页码
  127. * @param size 页数
  128. * @param request 请求
  129. * @return
  130. * @throws IOException
  131. */
  132. @RequestMapping("/brandsAndSellers")
  133. @ResponseBody
  134. public SPage<Map<String, Object>> searchBrandAndSellers(@RequestParam String keyword, Integer page, Integer size, HttpServletRequest request) throws IOException {
  135. return searchService.getBrandsAndSellers(keyword, page, size);
  136. }
  137. /**
  138. * 根据类目关键词搜索卖家信息
  139. * @param keyword 关键词
  140. * @param page 页码
  141. * @param size 页数
  142. * @param request 请求
  143. * @return
  144. * @throws IOException
  145. */
  146. @RequestMapping("/kindsAndSellers")
  147. @ResponseBody
  148. public Map<String, Object> searchSellersWithKind(@RequestParam String keyword, Integer page, Integer size, HttpServletRequest request) throws IOException {
  149. return searchService.getSellersWithKind(keyword, page, size);
  150. }
  151. @RequestMapping("/componentIds")
  152. @ResponseBody
  153. public Map<String, Object> searchComponentIds(@RequestParam String keyword, String params, HttpServletRequest request) {
  154. PageParams pageParams = params==null?null:JSONObject.parseObject(params, PageParams.class);
  155. return searchService.getComponentIds(keyword, pageParams);
  156. }
  157. @Autowired
  158. private ComponentDao componentDao;
  159. @RequestMapping("/components")
  160. @ResponseBody
  161. public List<Component> searchComponents(String keyword, String params, HttpServletRequest request) {
  162. @SuppressWarnings("unchecked")
  163. List<Long> ids = (List<Long>) searchComponentIds(keyword, params, request).get("componentIds");
  164. return findComponents(ids);
  165. }
  166. private List<Component> findComponents(List<Long> ids){
  167. if (CollectionUtils.isEmpty(ids)) {
  168. return new ArrayList<>();
  169. }
  170. Long[] idsLong = new Long[ids.size()];
  171. int i = 0;
  172. for (Long id : ids) {
  173. idsLong[i++] = id;
  174. }
  175. return componentDao.findAll(ids);
  176. }
  177. @RequestMapping("/kindIdsByComponent")
  178. @ResponseBody
  179. public Set<Long> searchKindIdsBySearchComponent(String keyword, String brandId, HttpServletRequest request) {
  180. return searchService.getKindIdsBySearchComponent(keyword, brandId);
  181. }
  182. @RequestMapping("/kindsByComponent")
  183. @ResponseBody
  184. public Set<Map<String, Object>> searchKindsBySearchComponent(String keyword, String brandId,
  185. HttpServletRequest request) {
  186. return searchService.getKindsBySearchComponent(keyword, brandId);
  187. }
  188. @RequestMapping("/brandIdsByComponent")
  189. @ResponseBody
  190. public Set<Long> searchBrandIdsBySearchComponent(String keyword, String kindId, HttpServletRequest request) {
  191. return searchService.getBrandIdsBySearchComponent(keyword, kindId);
  192. }
  193. @RequestMapping("/brandsByComponent")
  194. @ResponseBody
  195. public Set<Map<String, Object>> searchBrandsBySearchComponent(String keyword, String kindId,
  196. HttpServletRequest request) {
  197. return searchService.getBrandsBySearchComponent(keyword, kindId);
  198. }
  199. @RequestMapping("/similarKeywords")
  200. @ResponseBody
  201. public List<String> getSimilarKeywords(@RequestParam String keyword, Integer size, HttpServletRequest request) {
  202. return searchService.getSimilarKeywords(keyword, size);
  203. }
  204. @RequestMapping("/similarComponents")
  205. @ResponseBody
  206. public List<Map<String, Object>> getSimilarComponents(@RequestParam String keyword, Integer size,
  207. HttpServletRequest request) {
  208. return searchService.getSimilarComponents(keyword, size);
  209. }
  210. @RequestMapping("/similarBrands")
  211. @ResponseBody
  212. public List<Map<String, Object>> getSimilarBrands(@RequestParam String keyword, Integer size, HttpServletRequest request) throws IOException {
  213. return searchService.getSimilarBrands(keyword, size);
  214. }
  215. @RequestMapping("/similarKinds")
  216. @ResponseBody
  217. public List<Map<String, Object>> getSimilarKinds(@RequestParam String keyword, Integer size, HttpServletRequest request) throws IOException {
  218. return searchService.getSimilarKinds(keyword, size);
  219. }
  220. @RequestMapping("/similarLeafKinds")
  221. @ResponseBody
  222. public List<Map<String, Object>> getSimilarLeafKinds(@RequestParam String keyword, Integer size, HttpServletRequest request) throws IOException {
  223. return searchService.getSimilarLeafKinds(keyword, size);
  224. }
  225. @RequestMapping("/similarKindsByLevel")
  226. @ResponseBody
  227. public List<Map<String, Object>> getSimilarKindsByLevel(@RequestParam String keyword, Short level, Integer size, HttpServletRequest request) throws IOException {
  228. return searchService.getSimilarKindsByLevel(keyword, level, size);
  229. }
  230. @RequestMapping("/similarPropertyValues")
  231. @ResponseBody
  232. public List<Map<String, String>> getSimilarPropertyValues(Long kindId, Long propertyId, String keyword, Long topNum,
  233. HttpServletRequest request) {
  234. return searchService.getSimilarPropertyValues(kindId, propertyId, keyword, topNum);
  235. }
  236. public Map<FilterField, Object> parseFilters(JSONObject json){
  237. Map<FilterField, Object> filters = new HashMap<>();
  238. if(json!=null){
  239. Set<Entry<String, Object>> entrySet = json.entrySet();
  240. for (Entry<String, Object> entry : entrySet) {
  241. FilterField field = FilterField.valueOf(entry.getKey());
  242. String value = entry.getValue().toString();
  243. switch (field) {
  244. case GOODS_KINDID:
  245. case GOODS_BRANDID:
  246. case GOODS_STORE_TYPE:
  247. case GOODS_CRNAME:
  248. String[] strs = value.split(",");
  249. List<Object> values = new ArrayList<>();
  250. for (String str : strs) {
  251. values.add(str);
  252. }
  253. filters.put(field, values);
  254. break;
  255. default:
  256. filters.put(field, value);
  257. }
  258. }
  259. }
  260. return filters;
  261. }
  262. @RequestMapping("/goodsIds")
  263. @ResponseBody
  264. public Map<String, Object> getGoodsIds(String keyword, String params, HttpServletRequest request) throws IOException {
  265. long start = System.currentTimeMillis();
  266. PageParams pageParams = params == null ? null : JSONObject.parseObject(params, PageParams.class);
  267. Map<String, Object> goodsIds = searchService.getGoodsIds(keyword, pageParams);
  268. logger.info(String.format("goodsIds\t%s\t%.3fs", keyword, (System.currentTimeMillis() - start) / 1000.0));
  269. return goodsIds;
  270. }
  271. @RequestMapping("/pcbGoodsIds")
  272. @ResponseBody
  273. public Map<String, Object> getPCBGoodsIds(String keyword, String params, HttpServletRequest request) throws IOException {
  274. long start = System.currentTimeMillis();
  275. PageParams pageParams = params == null ? null : JSONObject.parseObject(params, PageParams.class);
  276. Map<String, Object> pcbGoodsIds = searchService.getPCBGoodsIds(keyword, pageParams);
  277. logger.info(String.format("pcbGoodsIds\t%s\t%.3fs", keyword, (System.currentTimeMillis() - start) / 1000.0));
  278. return pcbGoodsIds;
  279. }
  280. @RequestMapping("/collectBySearchGoods")
  281. @ResponseBody
  282. public List<Map<String, Object>> collectBySearchGoods(String keyword, @RequestParam String collectedField, String filters, HttpServletRequest request) throws IOException {
  283. long start = System.currentTimeMillis();
  284. Map<FilterField, Object> filtersMap = new HashMap<>();
  285. if(!StringUtils.isEmpty(filters)){
  286. JSONObject json = JSONObject.parseObject(filters);
  287. Set<Entry<String, Object>> entrySet = json.entrySet();
  288. for (Entry<String, Object> entry : entrySet) {
  289. filtersMap.put(FilterField.valueOf(entry.getKey().toUpperCase()), entry.getValue());
  290. }
  291. }
  292. List<Map<String, Object>> maps = searchService.collectBySearchGoods(keyword, CollectField.valueOf(collectedField.toUpperCase()),
  293. filtersMap);
  294. logger.info(String.format("collect\t%s\t%.3fs", keyword, (System.currentTimeMillis()-start)/1000.0));
  295. return maps;
  296. }
  297. @RequestMapping("/collectBySearchPCBGoods")
  298. @ResponseBody
  299. public List<Map<String, Object>> collectBySearchPCBGoods(String keyword, @RequestParam String collectedField, String filters, HttpServletRequest request) throws IOException {
  300. long start = System.currentTimeMillis();
  301. Map<FilterField, Object> filtersMap = new HashMap<>();
  302. if(!StringUtils.isEmpty(filters)){
  303. JSONObject json = JSONObject.parseObject(filters);
  304. Set<Entry<String, Object>> entrySet = json.entrySet();
  305. for (Entry<String, Object> entry : entrySet) {
  306. filtersMap.put(FilterField.valueOf(entry.getKey().toUpperCase()), entry.getValue());
  307. }
  308. }
  309. List<Map<String, Object>> maps = searchService.collectBySearchPCBGoods(keyword, CollectField.valueOf(collectedField.toUpperCase()),
  310. filtersMap);
  311. logger.info(String.format("collect\t%s\t%.3fs", keyword, (System.currentTimeMillis()-start)/1000.0));
  312. return maps;
  313. }
  314. @RequestMapping("/kind/{id}")
  315. @ResponseBody
  316. public Kind getKind(@PathVariable Long id, HttpServletRequest request) throws IOException {
  317. return searchService.getKind(id);
  318. }
  319. @RequestMapping("/brand/{id}")
  320. @ResponseBody
  321. public Brand getBrand(@PathVariable Long id, HttpServletRequest request) throws IOException {
  322. return searchService.getBrand(id);
  323. }
  324. @RequestMapping("/component/{id}")
  325. @ResponseBody
  326. public Component getComponent(@PathVariable Long id, HttpServletRequest request) throws IOException {
  327. return searchService.getComponent(id);
  328. }
  329. @RequestMapping("/goods/{id}")
  330. @ResponseBody
  331. public Goods getGoods(@PathVariable String id, HttpServletRequest request) throws IOException {
  332. return searchService.getGoods(id);
  333. }
  334. @RequestMapping("/pcbGoods/{id}")
  335. @ResponseBody
  336. public PCBGoods getPCBGoods(@PathVariable String id, HttpServletRequest request) throws IOException {
  337. return searchService.getPCBGoods(id);
  338. }
  339. @RequestMapping("/objects")
  340. @ResponseBody
  341. public SPage<Object> getObjects(@RequestParam String tableName, String keyword, String field,
  342. Boolean tokenized, Integer page, Integer size, HttpServletRequest request) throws IOException {
  343. return searchService.getObjects(tableName.toLowerCase(), keyword, field, tokenized, page == null ? 0 : page,
  344. size == null ? 0 : size);
  345. }
  346. @RequestMapping("/allObjectsToFiles")
  347. @ResponseBody
  348. public String writeAllObjectsToFiles(@RequestParam String tableName, HttpServletRequest request) throws IOException {
  349. int page = 1;
  350. int size = 1000;
  351. // 不能边更新索引边分页获取索引中的数据,因为索引更新后,分页顺序可能也会变化,
  352. // 所以要先把数据保存到本地,等待全部获取之后重建索引
  353. Long startTime = new Date().getTime();
  354. // 先删除旧的文件
  355. FileUtils.deleteSubFiles(new File(SearchUtils.getDataPath(tableName)));
  356. SPage<Object> sPage = searchService.getObjects(tableName.toLowerCase(), null, null, null, page, size);
  357. // 索引中数据的总数目
  358. long totalElements = sPage.getTotalElement();
  359. logger.info("发现数据:" + totalElements + "条");
  360. int fileIndex = 1;
  361. PrintWriter printWriter = null;
  362. int count = 0;
  363. File file = new File(SearchUtils.getDataPath(tableName));
  364. if (!file.exists()) {
  365. file.mkdirs();
  366. }
  367. printWriter = new PrintWriter(SearchUtils.getDataPath(tableName) + "/" + fileIndex + ".txt");
  368. while (true) {
  369. // 一个文件存放100000条数据,一旦超过,写入新的文件
  370. if (count > IndexServiceImpl.SINGLE_FILE_MAX_SIZE) {
  371. count = 1;
  372. printWriter.flush();
  373. printWriter.close();
  374. fileIndex++;
  375. printWriter = new PrintWriter(SearchUtils.getDataPath(tableName) + "/" + fileIndex + ".txt");
  376. }
  377. List<Object> content = sPage.getContent();
  378. for (Object element : content) {
  379. printWriter.println(JSONObject.toJSONString(element));
  380. count++;
  381. }
  382. logger.info(String.format(tableName + "...................%.2f%%",
  383. ((page - 1) * size + content.size()) * 100.0 / totalElements));
  384. if (++page > sPage.getTotalPage()) {
  385. break;
  386. }
  387. sPage = searchService.getObjects(tableName.toLowerCase(), null, null, null, page, size);
  388. }
  389. printWriter.flush();
  390. printWriter.close();
  391. Long endTime = new Date().getTime();
  392. String message = String.format("写入数据%s条,耗时%.2fs\n ", totalElements, (endTime - startTime) / 1000.0);
  393. logger.info(message);
  394. return message;
  395. }
  396. }