소스 검색

同一器件的属性值的rowid替换掉空格和+,用空格拼接后建立一份索引

sunyj 9 년 전
부모
커밋
f22d8a1e74

+ 8 - 10
search-console/src/main/java/com/uas/search/console/service/impl/IndexServiceImpl.java

@@ -35,7 +35,7 @@ import com.uas.search.console.dao.KindSimpleInfoDao;
 import com.uas.search.console.model.BrandSimpleInfo;
 import com.uas.search.console.model.ComponentSimpleInfo;
 import com.uas.search.console.model.KindSimpleInfo;
-import com.uas.search.console.model.PropertyValue;
+import com.uas.search.console.model.PropertyValueSimpleInfo;
 import com.uas.search.console.service.IndexService;
 import com.uas.search.console.support.IndexWriterManager;
 import com.uas.search.console.util.SearchConstants;
@@ -270,9 +270,9 @@ public class IndexServiceImpl implements IndexService {
 				List<ComponentSimpleInfo> components = pageResult.getContent();
 				for (ComponentSimpleInfo component : components) {
 					// 属性值的value为空,不写入索引
-					Set<PropertyValue> properties = component.getProperties();
-					Set<PropertyValue> removingProperties = new HashSet<>();
-					for (PropertyValue property : properties) {
+					Set<PropertyValueSimpleInfo> properties = component.getProperties();
+					Set<PropertyValueSimpleInfo> removingProperties = new HashSet<>();
+					for (PropertyValueSimpleInfo property : properties) {
 						if (StringUtils.isEmpty(property.getValue())) {
 							removingProperties.add(property);
 						}
@@ -420,19 +420,17 @@ public class IndexServiceImpl implements IndexService {
 		document.add(new StringField(SearchConstants.COMPONENT_ROWID_FIELD, component.getRowid(), Store.YES));
 
 		// 属性值加入索引,索引中field的键:"pr_"前缀连接属性的id
-		Set<PropertyValue> propertyValues = component.getProperties();
+		Set<PropertyValueSimpleInfo> propertyValues = component.getProperties();
 		StringBuilder stringBuilder = new StringBuilder();
-		for (PropertyValue propertyValue : propertyValues) {
+		for (PropertyValueSimpleInfo propertyValue : propertyValues) {
 			if (!StringUtils.isEmpty(propertyValue.getValue())) {
 				/*
 				 * 将属性值每行记录的rowid拼接在一起建立索引, 用于实时更新索引服务中 (属性值记录被删除时,
 				 * 无法根据DCN返回的属性值记录的rowid,得知需要更新哪个器件的索引),
 				 * 通过该rowid,从本地索引中获得相应器件的rowid, 再根据该rowid从数据库中获取器件信息, 更新本地索引
 				 */
-				stringBuilder.append(propertyValue.getRowid()).append(", ");
-				// TODO 删除
-				// document.add(new StringField(propertyValue.getRowid(), "1",
-				// Store.YES));
+				// 将rowid中的符号+和空格替换为_,并以空格将rowid隔开
+				stringBuilder.append(propertyValue.getRowid().replaceAll("[ +]+", "_")).append(" ");
 
 				String fieldKey = SearchConstants.COMPONENT_PROPERTY_PREFIX + propertyValue.getPropertyid();
 				document.add(new StringField(fieldKey, propertyValue.getValue(), Store.YES));

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

@@ -901,14 +901,21 @@ public class SearchServiceImpl implements SearchService {
 			throw new SystemError(message);
 		}
 
-		PrefixQuery query = new PrefixQuery(new Term(rowid, ""));
 		try {
-			TopDocs topDocs = indexSearcher.search(query, TOP_NUM);
-			if (topDocs.totalHits != 0) {
-				logger.error("属性值rowid为 " + rowid + " 的器件数量不正确: totalHits " + topDocs.totalHits);
+			rowid = rowid.replaceAll("[ +]+", "_");
+			BooleanQuery booleanQuery = getBooleanQuery(SearchConstants.COMPONENT_PROPERTY_ROWIDS_FIELD, rowid);
+			logger.info(booleanQuery);
+			TopDocs topDocs = indexSearcher.search(booleanQuery, TOP_NUM);
+			for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
+				Document document = indexSearcher.doc(scoreDoc.doc);
+				String propertyRowids = document.get(SearchConstants.COMPONENT_PROPERTY_ROWIDS_FIELD);
+				// 因分词后,不区分大小写,可能会匹配到很多结果
+				if (propertyRowids.contains(rowid)) {
+					return document.get(SearchConstants.COMPONENT_ROWID_FIELD);
+				}
 			}
-			Document document = indexSearcher.doc(topDocs.scoreDocs[0].doc);
-			return document.get(SearchConstants.COMPONENT_ROWID_FIELD);
+			logger.error(
+					"属性值rowid为 " + rowid + " 匹配(不区分大小写的情况下)到的器件数量为: totalHits " + topDocs.totalHits + ", 但未找到完全匹配的器件");
 		} catch (NumberFormatException e) {
 			e.printStackTrace();
 		} catch (IOException e) {