Browse Source

完善属性过滤

sunyj 9 years ago
parent
commit
df44fce5e5

+ 1 - 0
search-console/src/main/java/com/uas/search/console/controller/SearchController.java

@@ -103,6 +103,7 @@ public class SearchController {
 		PageParams pageParams = new PageParams();
 		Map<String, Object> filters = new HashMap<>();
 		filters.put("pr_329", "HRC");
+		filters.put("20", "Fast Blow");
 		pageParams.setFilters(filters);
 		@SuppressWarnings("unchecked")
 		List<Long> ids = (List<Long>) searchService.getComponentIds(keyword, pageParams).get("components");

+ 11 - 9
search-console/src/main/java/com/uas/search/console/service/impl/IndexServiceImpl.java

@@ -171,14 +171,14 @@ public class IndexServiceImpl implements IndexService {
 		try {
 			// 从本地路径读取器件数据
 			File[] files = new File(SearchConstants.COMPONENT_WITH_PROPERTY_DIR).listFiles();
-			// if(files.length == 0){
-			// System.out.println("创建器件索引失败,原因:器件数据文件不存在!");
-			// return 0L;
-			// }
+			if (files == null || files.length == 0) {
+				System.out.println("创建器件索引失败,原因:器件数据文件不存在!");
+				return 0L;
+			}
 			// 将要创建的索引总数目约为:文件数目*单个文件的行数
-			long totalSize = files.length * MergeComponentData.SIMGLE_FILE_MAX_SIZE;
+			long totalSize = files.length * MergeComponentData.SINGLE_FILE_MAX_SIZE;
 			for (File file : files) {
-				System.out.println("创建器件索引自文件: " + file.getName());
+				System.out.println("读取文件: " + file.getName());
 				BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
 				String line = null;
 				while (!StringUtils.isEmpty(line = bufferedReader.readLine())) {
@@ -206,7 +206,7 @@ public class IndexServiceImpl implements IndexService {
 	}
 
 	/**
-	 * 创建器件索引
+	 * 创建器件索引,从数据库取数据
 	 * 
 	 * @return 写入的器件索引数
 	 * @throws IOException
@@ -315,8 +315,10 @@ public class IndexServiceImpl implements IndexService {
 		// 属性值加入索引,索引中field的键:"pr_"前缀连接属性的id
 		Set<PropertyValue> propertyValues = component.getProperties();
 		for (PropertyValue propertyValue : propertyValues) {
-			String fieldKey = SearchConstants.COMPONENT_PROPERTY_PREFIX + propertyValue.getPropertyid();
-			document.add(new StringField(fieldKey, propertyValue.getValue(), Store.YES));
+			if (!StringUtils.isEmpty(propertyValue.getValue())) {
+				String fieldKey = SearchConstants.COMPONENT_PROPERTY_PREFIX + propertyValue.getPropertyid();
+				document.add(new StringField(fieldKey, propertyValue.getValue(), Store.YES));
+			}
 		}
 		return document;
 	}

+ 4 - 4
search-console/src/main/java/com/uas/search/console/service/impl/SearchServiceImpl.java

@@ -267,17 +267,17 @@ public class SearchServiceImpl implements SearchService {
 
 				// 属性过滤
 				for (String key : filters.keySet()) {
-					if (!key.startsWith(SearchConstants.COMPONENT_PROPERTY_PREFIX)) {
-						key = SearchConstants.COMPONENT_PROPERTY_PREFIX + key;
-					}
 					String value = String.valueOf(filters.get(key));
 					if (!StringUtils.isEmpty(value)) {
+						if (!key.startsWith(SearchConstants.COMPONENT_PROPERTY_PREFIX)) {
+							key = SearchConstants.COMPONENT_PROPERTY_PREFIX + key;
+						}
 						TermQuery propertyQuery = new TermQuery(new Term(key, value));
 						booleanQuery.add(propertyQuery, BooleanClause.Occur.MUST);
 					}
 				}
 			}
-
+			System.out.println(booleanQuery);
 			TopDocs hits;
 			if (page.getPage() > 1) {// 不是第一页
 				TopDocs previousHits = searcher.search(booleanQuery, (page.getPage() - 1) * page.getSize());

+ 2 - 2
search-console/src/main/java/com/uas/search/console/util/MergeComponentData.java

@@ -24,7 +24,7 @@ import com.uas.search.console.model.PropertyValue;
 public class MergeComponentData {
 
 	// 单个文件存储的最大数据数目
-	public static final int SIMGLE_FILE_MAX_SIZE = 100000;
+	public static final int SINGLE_FILE_MAX_SIZE = 100000;
 
 	// 器件和属性值两个文本文件的路径
 	private static final String DATA_DIR = "C:\\Users\\sunyj-pc\\Desktop\\data";
@@ -85,7 +85,7 @@ public class MergeComponentData {
 				dataCount++;
 				System.out.println(dataCount);
 				// 一个文件存放100000条数据,一旦超过,写入新的文件
-				if (dataCount > SIMGLE_FILE_MAX_SIZE) {
+				if (dataCount > SINGLE_FILE_MAX_SIZE) {
 					System.out.println("--------------------------------new file");
 					dataCount = 1;
 					printWriter.flush();