package com.uas.search.controller; import com.alibaba.fastjson.JSONObject; import com.uas.search.constant.model.CollectField; import com.uas.search.constant.model.PageParams; import com.uas.search.constant.model.PageParams.FilterField; import com.uas.search.constant.model.SPage; import com.uas.search.dao.ComponentDao; import com.uas.search.model.*; import com.uas.search.service.SearchService; import com.uas.search.service.impl.IndexServiceImpl; import com.uas.search.util.CollectionUtils; import com.uas.search.util.FileUtils; import com.uas.search.util.SearchUtils; import com.uas.search.util.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.*; import java.util.Map.Entry; /** * 搜索请求 * * @author suntg * @since 2016年8月1日上午9:18:05 */ @Controller @RequestMapping("/search") public class SearchController { @Autowired private SearchService searchService; private Logger logger = LoggerFactory.getLogger(getClass()); /** * 查询所有物料 * @param keyword 关键词 * @param page 页码 * @param size 尺寸 * @param enUU 企业UU * @param type 类型 standard 标准 nStandard 非标 其他为所有 * @param request request * @return idPage * @throws IOException 输入异常 */ @RequestMapping("/productIds") @ResponseBody public SPage searchProductIds(String keyword, Integer page, Integer size, Long enUU, String type, HttpServletRequest request) throws IOException { return searchService.getProductIds(enUU, keyword, page, size, type); } /** * 获取标准型号联想词 * @param keyword 关键词 * @param size 尺寸 * @param request * @return */ @RequestMapping("/product/similarPCmpCodes") @ResponseBody public List> getSimilarPCmpCodes(@RequestParam String keyword, Integer size, HttpServletRequest request) { long start = System.currentTimeMillis(); List> temp = searchService.getSimilarPCmpCodes(keyword, size); System.out.println("eee" + (System.currentTimeMillis() - start)); return temp; } /** * 获取类目联想词 * @param keyword 关键词 * @param size 尺寸 * @param request * @return */ @RequestMapping("/product/similarKind") @ResponseBody public List> getSimilarKind(@RequestParam String keyword, Integer size, HttpServletRequest request) { return searchService.getSimilarKind(keyword, size); } /** * 根据id获取索引详情 * @param id * @param request * @return * @throws IOException */ @RequestMapping("/product/{id}") @ResponseBody public V_Products getProduct(@PathVariable Long id, HttpServletRequest request) throws IOException { return searchService.getProduct(id); } @RequestMapping("/kindIds") @ResponseBody public SPage seachKindIds(@RequestParam String keyword, Integer page, Integer size, HttpServletRequest request) throws IOException { return searchService.getKindIds(keyword, page, size); } @RequestMapping("/kinds") @ResponseBody public SPage> seachKinds(@RequestParam String keyword, Integer page, Integer size, HttpServletRequest request) throws IOException { return searchService.getKinds(keyword, page, size); } @RequestMapping("/brandIds") @ResponseBody public SPage searchBrandIds(@RequestParam String keyword, Integer page, Integer size, HttpServletRequest request) throws IOException { return searchService.getBrandIds(keyword, page, size); } @RequestMapping("/brands") @ResponseBody public SPage> searchBrand(@RequestParam String keyword, Integer page, Integer size, HttpServletRequest request) throws IOException { return searchService.getBrands(keyword, page, size); } /** * 根据关键词搜索品牌(精确命中后,提供卖家信息) * @param keyword 关键词 * @param page 页码 * @param size 页数 * @param request 请求 * @return * @throws IOException */ @RequestMapping("/brandsAndSellers") @ResponseBody public SPage> searchBrandAndSellers(@RequestParam String keyword, Integer page, Integer size, HttpServletRequest request) throws IOException { return searchService.getBrandsAndSellers(keyword, page, size); } /** * 根据类目关键词搜索卖家信息 * @param keyword 关键词 * @param page 页码 * @param size 页数 * @param request 请求 * @return * @throws IOException */ @RequestMapping("/kindsAndSellers") @ResponseBody public Map searchSellersWithKind(@RequestParam String keyword, Integer page, Integer size, HttpServletRequest request) throws IOException { return searchService.getSellersWithKind(keyword, page, size); } @RequestMapping("/componentIds") @ResponseBody public Map searchComponentIds(@RequestParam String keyword, String params, HttpServletRequest request) { PageParams pageParams = params==null?null:JSONObject.parseObject(params, PageParams.class); return searchService.getComponentIds(keyword, pageParams); } @Autowired private ComponentDao componentDao; @RequestMapping("/components") @ResponseBody public List searchComponents(String keyword, String params, HttpServletRequest request) { @SuppressWarnings("unchecked") List ids = (List) searchComponentIds(keyword, params, request).get("componentIds"); return findComponents(ids); } private List findComponents(List ids){ if (CollectionUtils.isEmpty(ids)) { return new ArrayList<>(); } Long[] idsLong = new Long[ids.size()]; int i = 0; for (Long id : ids) { idsLong[i++] = id; } return componentDao.findAll(ids); } @RequestMapping("/kindIdsByComponent") @ResponseBody public Set searchKindIdsBySearchComponent(String keyword, String brandId, HttpServletRequest request) { return searchService.getKindIdsBySearchComponent(keyword, brandId); } @RequestMapping("/kindsByComponent") @ResponseBody public Set> searchKindsBySearchComponent(String keyword, String brandId, HttpServletRequest request) { return searchService.getKindsBySearchComponent(keyword, brandId); } @RequestMapping("/brandIdsByComponent") @ResponseBody public Set searchBrandIdsBySearchComponent(String keyword, String kindId, HttpServletRequest request) { return searchService.getBrandIdsBySearchComponent(keyword, kindId); } @RequestMapping("/brandsByComponent") @ResponseBody public Set> searchBrandsBySearchComponent(String keyword, String kindId, HttpServletRequest request) { return searchService.getBrandsBySearchComponent(keyword, kindId); } @RequestMapping("/similarKeywords") @ResponseBody public List getSimilarKeywords(@RequestParam String keyword, Integer size, HttpServletRequest request) { return searchService.getSimilarKeywords(keyword, size); } @RequestMapping("/similarComponents") @ResponseBody public List> getSimilarComponents(@RequestParam String keyword, Integer size, HttpServletRequest request) { return searchService.getSimilarComponents(keyword, size); } @RequestMapping("/similarBrands") @ResponseBody public List> getSimilarBrands(@RequestParam String keyword, Integer size, HttpServletRequest request) throws IOException { return searchService.getSimilarBrands(keyword, size); } @RequestMapping("/similarKinds") @ResponseBody public List> getSimilarKinds(@RequestParam String keyword, Integer size, HttpServletRequest request) throws IOException { return searchService.getSimilarKinds(keyword, size); } @RequestMapping("/similarLeafKinds") @ResponseBody public List> getSimilarLeafKinds(@RequestParam String keyword, Integer size, HttpServletRequest request) throws IOException { return searchService.getSimilarLeafKinds(keyword, size); } @RequestMapping("/similarKindsByLevel") @ResponseBody public List> getSimilarKindsByLevel(@RequestParam String keyword, Short level, Integer size, HttpServletRequest request) throws IOException { return searchService.getSimilarKindsByLevel(keyword, level, size); } @RequestMapping("/similarPropertyValues") @ResponseBody public List> getSimilarPropertyValues(Long kindId, Long propertyId, String keyword, Long topNum, HttpServletRequest request) { return searchService.getSimilarPropertyValues(kindId, propertyId, keyword, topNum); } public Map parseFilters(JSONObject json){ Map filters = new HashMap<>(); if(json!=null){ Set> entrySet = json.entrySet(); for (Entry entry : entrySet) { FilterField field = FilterField.valueOf(entry.getKey()); String value = entry.getValue().toString(); switch (field) { case GOODS_KINDID: case GOODS_BRANDID: case GOODS_STORE_TYPE: case GOODS_CRNAME: String[] strs = value.split(","); List values = new ArrayList<>(); for (String str : strs) { values.add(str); } filters.put(field, values); break; default: filters.put(field, value); } } } return filters; } @RequestMapping("/goodsIds") @ResponseBody public Map getGoodsIds(String keyword, String params, HttpServletRequest request) throws IOException { long start = System.currentTimeMillis(); PageParams pageParams = params == null ? null : JSONObject.parseObject(params, PageParams.class); Map goodsIds = searchService.getGoodsIds(keyword, pageParams); logger.info(String.format("goodsIds\t%s\t%.3fs", keyword, (System.currentTimeMillis() - start) / 1000.0)); return goodsIds; } @RequestMapping("/pcbGoodsIds") @ResponseBody public Map getPCBGoodsIds(String keyword, String params, HttpServletRequest request) throws IOException { long start = System.currentTimeMillis(); PageParams pageParams = params == null ? null : JSONObject.parseObject(params, PageParams.class); Map pcbGoodsIds = searchService.getPCBGoodsIds(keyword, pageParams); logger.info(String.format("pcbGoodsIds\t%s\t%.3fs", keyword, (System.currentTimeMillis() - start) / 1000.0)); return pcbGoodsIds; } @RequestMapping("/collectBySearchGoods") @ResponseBody public List> collectBySearchGoods(String keyword, @RequestParam String collectedField, String filters, HttpServletRequest request) throws IOException { long start = System.currentTimeMillis(); Map filtersMap = new HashMap<>(); if(!StringUtils.isEmpty(filters)){ JSONObject json = JSONObject.parseObject(filters); Set> entrySet = json.entrySet(); for (Entry entry : entrySet) { filtersMap.put(FilterField.valueOf(entry.getKey().toUpperCase()), entry.getValue()); } } List> maps = searchService.collectBySearchGoods(keyword, CollectField.valueOf(collectedField.toUpperCase()), filtersMap); logger.info(String.format("collect\t%s\t%.3fs", keyword, (System.currentTimeMillis()-start)/1000.0)); return maps; } @RequestMapping("/collectBySearchPCBGoods") @ResponseBody public List> collectBySearchPCBGoods(String keyword, @RequestParam String collectedField, String filters, HttpServletRequest request) throws IOException { long start = System.currentTimeMillis(); Map filtersMap = new HashMap<>(); if(!StringUtils.isEmpty(filters)){ JSONObject json = JSONObject.parseObject(filters); Set> entrySet = json.entrySet(); for (Entry entry : entrySet) { filtersMap.put(FilterField.valueOf(entry.getKey().toUpperCase()), entry.getValue()); } } List> maps = searchService.collectBySearchPCBGoods(keyword, CollectField.valueOf(collectedField.toUpperCase()), filtersMap); logger.info(String.format("collect\t%s\t%.3fs", keyword, (System.currentTimeMillis()-start)/1000.0)); return maps; } @RequestMapping("/kind/{id}") @ResponseBody public Kind getKind(@PathVariable Long id, HttpServletRequest request) throws IOException { return searchService.getKind(id); } @RequestMapping("/brand/{id}") @ResponseBody public Brand getBrand(@PathVariable Long id, HttpServletRequest request) throws IOException { return searchService.getBrand(id); } @RequestMapping("/component/{id}") @ResponseBody public Component getComponent(@PathVariable Long id, HttpServletRequest request) throws IOException { return searchService.getComponent(id); } @RequestMapping("/goods/{id}") @ResponseBody public Goods getGoods(@PathVariable String id, HttpServletRequest request) throws IOException { return searchService.getGoods(id); } @RequestMapping("/pcbGoods/{id}") @ResponseBody public PCBGoods getPCBGoods(@PathVariable String id, HttpServletRequest request) throws IOException { return searchService.getPCBGoods(id); } @RequestMapping("/objects") @ResponseBody public SPage getObjects(@RequestParam String tableName, String keyword, String field, Boolean tokenized, Integer page, Integer size, HttpServletRequest request) throws IOException { return searchService.getObjects(tableName.toLowerCase(), keyword, field, tokenized, page == null ? 0 : page, size == null ? 0 : size); } @RequestMapping("/allObjectsToFiles") @ResponseBody public String writeAllObjectsToFiles(@RequestParam String tableName, HttpServletRequest request) throws IOException { int page = 1; int size = 1000; // 不能边更新索引边分页获取索引中的数据,因为索引更新后,分页顺序可能也会变化, // 所以要先把数据保存到本地,等待全部获取之后重建索引 Long startTime = new Date().getTime(); // 先删除旧的文件 FileUtils.deleteSubFiles(new File(SearchUtils.getDataPath(tableName))); SPage sPage = searchService.getObjects(tableName.toLowerCase(), null, null, null, page, size); // 索引中数据的总数目 long totalElements = sPage.getTotalElement(); logger.info("发现数据:" + totalElements + "条"); int fileIndex = 1; PrintWriter printWriter = null; int count = 0; File file = new File(SearchUtils.getDataPath(tableName)); if (!file.exists()) { file.mkdirs(); } printWriter = new PrintWriter(SearchUtils.getDataPath(tableName) + "/" + fileIndex + ".txt"); while (true) { // 一个文件存放100000条数据,一旦超过,写入新的文件 if (count > IndexServiceImpl.SINGLE_FILE_MAX_SIZE) { count = 1; printWriter.flush(); printWriter.close(); fileIndex++; printWriter = new PrintWriter(SearchUtils.getDataPath(tableName) + "/" + fileIndex + ".txt"); } List content = sPage.getContent(); for (Object element : content) { printWriter.println(JSONObject.toJSONString(element)); count++; } logger.info(String.format(tableName + "...................%.2f%%", ((page - 1) * size + content.size()) * 100.0 / totalElements)); if (++page > sPage.getTotalPage()) { break; } sPage = searchService.getObjects(tableName.toLowerCase(), null, null, null, page, size); } printWriter.flush(); printWriter.close(); Long endTime = new Date().getTime(); String message = String.format("写入数据%s条,耗时%.2fs\n ", totalElements, (endTime - startTime) / 1000.0); logger.info(message); return message; } }