Browse Source

提取根据各对象id获取索引中对象数据的方法

sunyj 9 năm trước cách đây
mục cha
commit
60d690ba93

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

@@ -675,94 +675,43 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
 
 	@Override
 	public KindSimpleInfo getKind(Long kindId) {
-		if (kindId == null) {
-			throw new SearchException("输入无效:" + kindId);
-		}
-		IndexSearcher indexSearcher = SearchUtils.getIndexSearcher();
-
 		KindSimpleInfo kind = null;
-		try {
-			TermQuery query = new TermQuery(new Term(SearchConstants.KIND_ID_FIELD, String.valueOf(kindId)));
-			TopDocs topDocs = indexSearcher.search(query, 1);
-			int totalHits = topDocs.totalHits;
-			if (totalHits > 1) {
-				throw new SearchException("索引中存在不止一个类目:kindId=" + kindId);
-			} else if (totalHits == 1) {
-				Document document = indexSearcher.doc(topDocs.scoreDocs[0].doc);
-				kind = new KindSimpleInfo();
-				kind.setId(kindId);
-				kind.setNameCn(document.get(SearchConstants.KIND_NAMECN_FIELD));
-				kind.setIsLeaf(Short.valueOf(document.get(SearchConstants.KIND_ISLEAF_FIELD)));
-				kind.setLevel(Short.valueOf(document.get(SearchConstants.KIND_LEVEL_FIELD)));
-			}
-
-		} catch (IOException e) {
-			e.printStackTrace();
-		} finally {
-			SearchUtils.releaseIndexSearcher(indexSearcher);
+		Document document = SearchUtils.getDocumentById(SearchConstants.KIND_ID_FIELD, kindId);
+		if (document != null) {
+			kind = new KindSimpleInfo();
+			kind.setId(kindId);
+			kind.setNameCn(document.get(SearchConstants.KIND_NAMECN_FIELD));
+			kind.setIsLeaf(Short.valueOf(document.get(SearchConstants.KIND_ISLEAF_FIELD)));
+			kind.setLevel(Short.valueOf(document.get(SearchConstants.KIND_LEVEL_FIELD)));
 		}
 		return kind;
 	}
 
 	@Override
 	public BrandSimpleInfo getBrand(Long brandId) {
-		if (brandId == null) {
-			throw new SearchException("输入无效:" + brandId);
-		}
-		IndexSearcher indexSearcher = SearchUtils.getIndexSearcher();
-
 		BrandSimpleInfo brand = null;
-		try {
-			TermQuery query = new TermQuery(new Term(SearchConstants.BRAND_ID_FIELD, String.valueOf(brandId)));
-			TopDocs topDocs = indexSearcher.search(query, 1);
-			int totalHits = topDocs.totalHits;
-			if (totalHits > 1) {
-				throw new SearchException("索引中存在不止一个品牌:brandId=" + brandId);
-			} else if (totalHits == 1) {
-				Document document = indexSearcher.doc(topDocs.scoreDocs[0].doc);
-				brand = new BrandSimpleInfo();
-				brand.setId(brandId);
-				brand.setNameCn(document.get(SearchConstants.BRAND_NAMECN_FIELD));
-				brand.setNameEn(document.get(SearchConstants.BRAND_NAMEEN_FIELD));
-				brand.setUuid(document.get(SearchConstants.BRAND_UUID_FIELD));
-			}
-
-		} catch (IOException e) {
-			e.printStackTrace();
-		} finally {
-			SearchUtils.releaseIndexSearcher(indexSearcher);
+		Document document = SearchUtils.getDocumentById(SearchConstants.BRAND_ID_FIELD, brandId);
+		if (document != null) {
+			brand = new BrandSimpleInfo();
+			brand.setId(brandId);
+			brand.setNameCn(document.get(SearchConstants.BRAND_NAMECN_FIELD));
+			brand.setNameEn(document.get(SearchConstants.BRAND_NAMEEN_FIELD));
+			brand.setUuid(document.get(SearchConstants.BRAND_UUID_FIELD));
 		}
 		return brand;
 	}
 
 	@Override
 	public ComponentSimpleInfo getComponent(Long componentId) {
-		if (componentId == null) {
-			throw new SearchException("输入无效:" + componentId);
-		}
-		IndexSearcher indexSearcher = SearchUtils.getIndexSearcher();
-
 		ComponentSimpleInfo component = null;
-		try {
-			TermQuery query = new TermQuery(new Term(SearchConstants.COMPONENT_ID_FIELD, String.valueOf(componentId)));
-			TopDocs topDocs = indexSearcher.search(query, 1);
-			int totalHits = topDocs.totalHits;
-			if (totalHits > 1) {
-				throw new SearchException("索引中存在不止一个器件:componentId=" + componentId);
-			} else if (totalHits == 1) {
-				Document document = indexSearcher.doc(topDocs.scoreDocs[0].doc);
-				component = new ComponentSimpleInfo();
-				component.setId(componentId);
-				component.setCode(document.get(SearchConstants.COMPONENT_CODE_FIELD));
-				component.setUuid(document.get(SearchConstants.COMPONENT_UUID_FIELD));
-				component.setKindid(Long.valueOf(document.get(SearchConstants.COMPONENT_KINDID_FIELD)));
-				component.setBrandid(Long.valueOf(document.get(SearchConstants.COMPONENT_BRANDID_FIELD)));
-			}
-
-		} catch (IOException e) {
-			e.printStackTrace();
-		} finally {
-			SearchUtils.releaseIndexSearcher(indexSearcher);
+		Document document = SearchUtils.getDocumentById(SearchConstants.COMPONENT_ID_FIELD, componentId);
+		if (document != null) {
+			component = new ComponentSimpleInfo();
+			component.setId(componentId);
+			component.setCode(document.get(SearchConstants.COMPONENT_CODE_FIELD));
+			component.setUuid(document.get(SearchConstants.COMPONENT_UUID_FIELD));
+			component.setKindid(Long.valueOf(document.get(SearchConstants.COMPONENT_KINDID_FIELD)));
+			component.setBrandid(Long.valueOf(document.get(SearchConstants.COMPONENT_BRANDID_FIELD)));
 		}
 		return component;
 	}

+ 23 - 0
search-console/src/main/java/com/uas/search/console/util/SearchUtils.java

@@ -17,6 +17,7 @@ import org.apache.lucene.search.Query;
 import org.apache.lucene.search.ScoreDoc;
 import org.apache.lucene.search.TermQuery;
 import org.apache.lucene.search.TopDocs;
+import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 import org.wltea.analyzer.lucene.IKAnalyzer;
 
@@ -104,6 +105,28 @@ public class SearchUtils {
 		searcherManager.release(indexSearcher);
 	}
 
+	/**
+	 * 根据域和搜索词获取Document(每个id最多只能对应一条数据)
+	 * 
+	 * @param field
+	 *            搜索域
+	 * @param keyword
+	 *            搜索词
+	 * @return Document
+	 */
+	public static Document getDocumentById(String field, Long id) {
+		if (id == null) {
+			throw new SearchException("输入无效:" + id);
+		}
+		List<Document> documents = getDocuments(field, Long.toString(id), 1);
+		if (CollectionUtils.isEmpty(documents)) {
+			return null;
+		} else if (documents.size() > 1) {
+			throw new SearchException("索引中存在不止一个对象:" + field + "=" + id);
+		}
+		return documents.get(0);
+	}
+
 	/**
 	 * 根据域和搜索词获取Document列表
 	 *