|
|
@@ -1,6 +1,9 @@
|
|
|
package com.uas.search.console.service.impl;
|
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
import java.io.File;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.FileReader;
|
|
|
import java.io.IOException;
|
|
|
import java.nio.file.Paths;
|
|
|
import java.util.Date;
|
|
|
@@ -20,6 +23,7 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.uas.platform.core.model.PageInfo;
|
|
|
import com.uas.platform.core.model.PageParams;
|
|
|
import com.uas.search.console.dao.BrandSimpleInfoDao;
|
|
|
@@ -31,6 +35,7 @@ import com.uas.search.console.model.KindSimpleInfo;
|
|
|
import com.uas.search.console.model.PropertyValue;
|
|
|
import com.uas.search.console.service.IndexService;
|
|
|
import com.uas.search.console.support.IndexWriterManager;
|
|
|
+import com.uas.search.console.util.MergeComponentData;
|
|
|
import com.uas.search.console.util.SearchConstants;
|
|
|
|
|
|
/**
|
|
|
@@ -97,7 +102,8 @@ public class IndexServiceImpl implements IndexService {
|
|
|
Long brandTime = new Date().getTime();
|
|
|
System.out.println("创建品牌索引: " + brandSize + "条,耗时 " + (brandTime - kindTime) + " ms\n");
|
|
|
|
|
|
- Long componentSize = createComponentIndexs();
|
|
|
+ // Long componentSize = createComponentIndexs();
|
|
|
+ Long componentSize = createComponentIndexesWithFiles();
|
|
|
Long componentTime = new Date().getTime();
|
|
|
System.out.println("创建器件索引: " + componentSize + "条,耗时 " + (componentTime - brandTime) + " ms\n");
|
|
|
|
|
|
@@ -159,6 +165,46 @@ public class IndexServiceImpl implements IndexService {
|
|
|
return (long) brands.size();
|
|
|
}
|
|
|
|
|
|
+ private Long createComponentIndexesWithFiles() {
|
|
|
+ System.out.println("正在创建器件索引...");
|
|
|
+ Long size = 0L;
|
|
|
+ try {
|
|
|
+ // 从本地路径读取器件数据
|
|
|
+ File[] files = new File(SearchConstants.COMPONENT_WITH_PROPERTY_DIR).listFiles();
|
|
|
+ // if(files.length == 0){
|
|
|
+ // System.out.println("创建器件索引失败,原因:器件数据文件不存在!");
|
|
|
+ // return 0L;
|
|
|
+ // }
|
|
|
+ // 将要创建的索引总数目约为:文件数目*单个文件的行数
|
|
|
+ long totalSize = files.length * MergeComponentData.SIMGLE_FILE_MAX_SIZE;
|
|
|
+ for (File file : files) {
|
|
|
+ System.out.println("创建器件索引自文件: " + file.getName());
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
|
|
|
+ String line = null;
|
|
|
+ while (!StringUtils.isEmpty(line = bufferedReader.readLine())) {
|
|
|
+ ComponentSimpleInfo component = JSONObject.parseObject(line, ComponentSimpleInfo.class);
|
|
|
+ Document document = toDocument(component);
|
|
|
+ if (document != null) {
|
|
|
+ size++;
|
|
|
+ // 每创建10000条,打印一次进度
|
|
|
+ if (size % 10000 == 0) {
|
|
|
+ System.out.printf("Component indexed...................%.2f%%\n", size * 100.0 / totalSize);
|
|
|
+ }
|
|
|
+ indexWriter.addDocument(document);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ indexWriter.commit();
|
|
|
+ bufferedReader.close();
|
|
|
+ }
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ System.out.println("创建器件索引失败,原因:器件数据文件不存在!");
|
|
|
+ return 0L;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return size;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 创建器件索引
|
|
|
*
|
|
|
@@ -177,6 +223,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
Page<ComponentSimpleInfo> pageResult = componentDao.findAll(info);
|
|
|
long totalElements = pageResult.getTotalElements();
|
|
|
System.out.println("Number of components: " + totalElements);
|
|
|
+ // 用于记录上次提交索引时的创建进度
|
|
|
double recordProgress = 0;
|
|
|
while (totalElements > size) {
|
|
|
List<ComponentSimpleInfo> components = pageResult.getContent();
|
|
|
@@ -191,8 +238,8 @@ public class IndexServiceImpl implements IndexService {
|
|
|
// 器件索引的创建进度(百分比)
|
|
|
double indexProgress = size * 100.0 / totalElements;
|
|
|
System.out.printf("Component indexed...................%.2f%%\n", indexProgress);
|
|
|
- // 每创建10%,提交一次,避免内存耗尽,发生OutOfMemoryError
|
|
|
- if (indexProgress - recordProgress > 10) {
|
|
|
+ // 每创建5%,提交一次,避免内存耗尽,发生OutOfMemoryError
|
|
|
+ if (indexProgress - recordProgress > 5) {
|
|
|
indexWriter.commit();
|
|
|
recordProgress = indexProgress;
|
|
|
}
|
|
|
@@ -265,14 +312,11 @@ public class IndexServiceImpl implements IndexService {
|
|
|
document.add(new StringField(SearchConstants.COMPONENT_BRANDID_FIELD, String.valueOf(component.getBrandid()),
|
|
|
Store.YES));
|
|
|
|
|
|
- // 属性值加入索引,索引中field的键:"pr_"前缀连接属性id
|
|
|
+ // 属性值加入索引,索引中field的键:"pr_"前缀连接属性的id
|
|
|
Set<PropertyValue> propertyValues = component.getProperties();
|
|
|
for (PropertyValue propertyValue : propertyValues) {
|
|
|
- if (!StringUtils.isEmpty(propertyValue.getValue())) {
|
|
|
- String fieldKey = new StringBuilder(SearchConstants.COMPONENT_PROPERTY_PREFIX)
|
|
|
- .append(propertyValue.getId()).toString();
|
|
|
- document.add(new StringField(fieldKey, propertyValue.getValue(), Store.YES));
|
|
|
- }
|
|
|
+ String fieldKey = SearchConstants.COMPONENT_PROPERTY_PREFIX + propertyValue.getPropertyid();
|
|
|
+ document.add(new StringField(fieldKey, propertyValue.getValue(), Store.YES));
|
|
|
}
|
|
|
return document;
|
|
|
}
|