|
|
@@ -1,17 +1,24 @@
|
|
|
package com.uas.search.console.controller;
|
|
|
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.PrintWriter;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Map.Entry;
|
|
|
import java.util.Set;
|
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
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 com.alibaba.druid.util.StringUtils;
|
|
|
@@ -24,9 +31,14 @@ import com.uas.search.console.model.ComponentSimpleInfo;
|
|
|
import com.uas.search.console.model.GoodsSimpleInfo;
|
|
|
import com.uas.search.console.model.KindSimpleInfo;
|
|
|
import com.uas.search.console.service.InnerSearchService;
|
|
|
+import com.uas.search.console.service.impl.IndexServiceImpl;
|
|
|
+import com.uas.search.console.util.FileUtils;
|
|
|
+import com.uas.search.console.util.SearchUtils;
|
|
|
+import com.uas.search.exception.SearchException;
|
|
|
import com.uas.search.model.CollectField;
|
|
|
import com.uas.search.model.PageParams;
|
|
|
import com.uas.search.model.PageParams.FilterField;
|
|
|
+import com.uas.search.model.SPage;
|
|
|
import com.uas.search.model.Sort;
|
|
|
import com.uas.search.model.Sort.Field;
|
|
|
import com.uas.search.service.SearchService;
|
|
|
@@ -47,6 +59,8 @@ public class SearchController {
|
|
|
private InnerSearchService innerSearchService = ContextUtils.getApplicationContext().getBean("searchServiceImpl",
|
|
|
InnerSearchService.class);
|
|
|
|
|
|
+ private Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
+
|
|
|
@RequestMapping("/kindIds/{keyword}")
|
|
|
@ResponseBody
|
|
|
public List<Long> seachKindIds(@PathVariable String keyword) {
|
|
|
@@ -288,4 +302,67 @@ public class SearchController {
|
|
|
public GoodsSimpleInfo getGoods(@PathVariable String id) {
|
|
|
return innerSearchService.getGoods(id);
|
|
|
}
|
|
|
+
|
|
|
+ @RequestMapping("/allObjects")
|
|
|
+ @ResponseBody
|
|
|
+ public SPage<Object> getAllObjects(@RequestParam(required = true) String tableName, int page, int size) {
|
|
|
+ return innerSearchService.getAllObjects(tableName.toLowerCase(), page, size);
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/allObjectsToFiles")
|
|
|
+ @ResponseBody
|
|
|
+ public String writeAllObjectsToFiles(@RequestParam(required = true) String tableName) {
|
|
|
+ int page = 1;
|
|
|
+ int size = 1000;
|
|
|
+ // 不能边更新索引边分页获取索引中的数据,因为索引更新后,分页顺序可能也会变化,
|
|
|
+ // 所以要先把数据保存到本地,等待全部获取之后重建索引
|
|
|
+ Long startTime = new Date().getTime();
|
|
|
+ // 先删除旧的文件
|
|
|
+ FileUtils.deleteSubFiles(new File(SearchUtils.getDataPath(tableName)));
|
|
|
+
|
|
|
+ SPage<Object> sPage = innerSearchService.getAllObjects(tableName.toLowerCase(), page, size);
|
|
|
+ // 索引中数据的总数目
|
|
|
+ long totalElements = sPage.getTotalElement();
|
|
|
+ logger.info("发现数据:" + totalElements + "条");
|
|
|
+ int fileIndex = 1;
|
|
|
+ PrintWriter printWriter = null;
|
|
|
+ int count = 0;
|
|
|
+ try {
|
|
|
+ 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 * 100.0 / totalElements));
|
|
|
+
|
|
|
+ if (++page > sPage.getTotalPage()) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ sPage = innerSearchService.getAllObjects(tableName.toLowerCase(), page, size);
|
|
|
+ }
|
|
|
+ printWriter.flush();
|
|
|
+ printWriter.close();
|
|
|
+ } catch (FileNotFoundException | SecurityException | IllegalArgumentException e) {
|
|
|
+ throw new SearchException(e).setDetailedMessage(e);
|
|
|
+ }
|
|
|
+ Long endTime = new Date().getTime();
|
|
|
+ String message = String.format("写入数据%s条,耗时%.2fs\n ", totalElements, (endTime - startTime) / 1000.0);
|
|
|
+ logger.info(message);
|
|
|
+ return message;
|
|
|
+ }
|
|
|
}
|