| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- 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<Long> 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<Map<String, Object>> getSimilarPCmpCodes(@RequestParam String keyword, Integer size,
- HttpServletRequest request) {
- long start = System.currentTimeMillis();
- List<Map<String, Object>> 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<Map<String, Object>> 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<Long> seachKindIds(@RequestParam String keyword, Integer page, Integer size,
- HttpServletRequest request) throws IOException {
- return searchService.getKindIds(keyword, page, size);
- }
- @RequestMapping("/kinds")
- @ResponseBody
- public SPage<Map<String, Object>> seachKinds(@RequestParam String keyword, Integer page, Integer size,
- HttpServletRequest request) throws IOException {
- return searchService.getKinds(keyword, page, size);
- }
- @RequestMapping("/brandIds")
- @ResponseBody
- public SPage<Long> searchBrandIds(@RequestParam String keyword, Integer page, Integer size,
- HttpServletRequest request) throws IOException {
- return searchService.getBrandIds(keyword, page, size);
- }
- @RequestMapping("/brands")
- @ResponseBody
- public SPage<Map<String, Object>> 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<Map<String, Object>> 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<String, Object> searchSellersWithKind(@RequestParam String keyword, Integer page, Integer size, HttpServletRequest request) throws IOException {
- return searchService.getSellersWithKind(keyword, page, size);
- }
- @RequestMapping("/componentIds")
- @ResponseBody
- public Map<String, Object> 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<Component> searchComponents(String keyword, String params, HttpServletRequest request) {
- @SuppressWarnings("unchecked")
- List<Long> ids = (List<Long>) searchComponentIds(keyword, params, request).get("componentIds");
- return findComponents(ids);
- }
- private List<Component> findComponents(List<Long> 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<Long> searchKindIdsBySearchComponent(String keyword, String brandId, HttpServletRequest request) {
- return searchService.getKindIdsBySearchComponent(keyword, brandId);
- }
- @RequestMapping("/kindsByComponent")
- @ResponseBody
- public Set<Map<String, Object>> searchKindsBySearchComponent(String keyword, String brandId,
- HttpServletRequest request) {
- return searchService.getKindsBySearchComponent(keyword, brandId);
- }
- @RequestMapping("/brandIdsByComponent")
- @ResponseBody
- public Set<Long> searchBrandIdsBySearchComponent(String keyword, String kindId, HttpServletRequest request) {
- return searchService.getBrandIdsBySearchComponent(keyword, kindId);
- }
- @RequestMapping("/brandsByComponent")
- @ResponseBody
- public Set<Map<String, Object>> searchBrandsBySearchComponent(String keyword, String kindId,
- HttpServletRequest request) {
- return searchService.getBrandsBySearchComponent(keyword, kindId);
- }
- @RequestMapping("/similarKeywords")
- @ResponseBody
- public List<String> getSimilarKeywords(@RequestParam String keyword, Integer size, HttpServletRequest request) {
- return searchService.getSimilarKeywords(keyword, size);
- }
- @RequestMapping("/similarComponents")
- @ResponseBody
- public List<Map<String, Object>> getSimilarComponents(@RequestParam String keyword, Integer size,
- HttpServletRequest request) {
- return searchService.getSimilarComponents(keyword, size);
- }
- @RequestMapping("/similarBrands")
- @ResponseBody
- public List<Map<String, Object>> getSimilarBrands(@RequestParam String keyword, Integer size, HttpServletRequest request) throws IOException {
- return searchService.getSimilarBrands(keyword, size);
- }
- @RequestMapping("/similarKinds")
- @ResponseBody
- public List<Map<String, Object>> getSimilarKinds(@RequestParam String keyword, Integer size, HttpServletRequest request) throws IOException {
- return searchService.getSimilarKinds(keyword, size);
- }
- @RequestMapping("/similarLeafKinds")
- @ResponseBody
- public List<Map<String, Object>> getSimilarLeafKinds(@RequestParam String keyword, Integer size, HttpServletRequest request) throws IOException {
- return searchService.getSimilarLeafKinds(keyword, size);
- }
- @RequestMapping("/similarKindsByLevel")
- @ResponseBody
- public List<Map<String, Object>> getSimilarKindsByLevel(@RequestParam String keyword, Short level, Integer size, HttpServletRequest request) throws IOException {
- return searchService.getSimilarKindsByLevel(keyword, level, size);
- }
- @RequestMapping("/similarPropertyValues")
- @ResponseBody
- public List<Map<String, String>> getSimilarPropertyValues(Long kindId, Long propertyId, String keyword, Long topNum,
- HttpServletRequest request) {
- return searchService.getSimilarPropertyValues(kindId, propertyId, keyword, topNum);
- }
- public Map<FilterField, Object> parseFilters(JSONObject json){
- Map<FilterField, Object> filters = new HashMap<>();
- if(json!=null){
- Set<Entry<String, Object>> entrySet = json.entrySet();
- for (Entry<String, Object> 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<Object> 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<String, Object> getGoodsIds(String keyword, String params, HttpServletRequest request) throws IOException {
- long start = System.currentTimeMillis();
- PageParams pageParams = params == null ? null : JSONObject.parseObject(params, PageParams.class);
- Map<String, Object> 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<String, Object> getPCBGoodsIds(String keyword, String params, HttpServletRequest request) throws IOException {
- long start = System.currentTimeMillis();
- PageParams pageParams = params == null ? null : JSONObject.parseObject(params, PageParams.class);
- Map<String, Object> 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<Map<String, Object>> collectBySearchGoods(String keyword, @RequestParam String collectedField, String filters, HttpServletRequest request) throws IOException {
- long start = System.currentTimeMillis();
- Map<FilterField, Object> filtersMap = new HashMap<>();
- if(!StringUtils.isEmpty(filters)){
- JSONObject json = JSONObject.parseObject(filters);
- Set<Entry<String, Object>> entrySet = json.entrySet();
- for (Entry<String, Object> entry : entrySet) {
- filtersMap.put(FilterField.valueOf(entry.getKey().toUpperCase()), entry.getValue());
- }
- }
- List<Map<String, Object>> 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<Map<String, Object>> collectBySearchPCBGoods(String keyword, @RequestParam String collectedField, String filters, HttpServletRequest request) throws IOException {
- long start = System.currentTimeMillis();
- Map<FilterField, Object> filtersMap = new HashMap<>();
- if(!StringUtils.isEmpty(filters)){
- JSONObject json = JSONObject.parseObject(filters);
- Set<Entry<String, Object>> entrySet = json.entrySet();
- for (Entry<String, Object> entry : entrySet) {
- filtersMap.put(FilterField.valueOf(entry.getKey().toUpperCase()), entry.getValue());
- }
- }
- List<Map<String, Object>> 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<Object> 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<Object> 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<Object> 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;
- }
- }
|