Sfoglia il codice sorgente

修正索引创建

sunyj 9 anni fa
parent
commit
756b87cbf0

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

@@ -50,7 +50,7 @@ public class SearchController {
 	}
 
 	/**
-	 * 搜索产品品牌
+	 * 搜索产品品牌id
 	 * 
 	 * @param keyword
 	 * @return
@@ -61,6 +61,17 @@ public class SearchController {
 		return searchService.getBrandIds(keyword);
 	}
 
+	/**
+	 * 搜索产品品牌
+	 * 
+	 * @param keyword
+	 * @return
+	 */
+	@RequestMapping("/brands")
+	@ResponseBody
+	public List<Map<String, Object>> searchBrand(String keyword) {
+		return searchService.getBrands(keyword);
+	}
 	/**
 	 * 搜索产品
 	 * 

+ 46 - 37
search-console/src/main/java/com/uas/search/console/service/impl/IndexServiceImpl.java

@@ -217,7 +217,8 @@ public class IndexServiceImpl implements IndexService {
 	 */
 	private Document toDocument(BrandSimpleInfo brand) {
 		Document document = new Document();
-		if (brand == null || brand.getId() == null || StringUtils.isEmpty(brand.getNameCn())) {
+		if (brand == null || brand.getId() == null || StringUtils.isEmpty(brand.getNameCn())
+				|| StringUtils.isEmpty(brand.getUuid())) {
 			return null;
 		}
 		// 不用LongField,否则后续实时更新索引时无法根据id进行更新
@@ -234,7 +235,8 @@ public class IndexServiceImpl implements IndexService {
 	 * @return
 	 */
 	private Document toDocument(ComponentSimpleInfo component) {
-		if (component == null || component.getId() == null || StringUtils.isEmpty(component.getCode())) {
+		if (component == null || component.getId() == null || StringUtils.isEmpty(component.getCode())
+				|| component.getKindid() == null || component.getBrandid() == null) {
 			return null;
 		}
 		Document document = new Document();
@@ -244,10 +246,8 @@ public class IndexServiceImpl implements IndexService {
 		String code = component.getCode().toLowerCase();
 		document.add(new StringField(SearchConstants.COMPONENT_CODE_FIELD, code, Store.YES));
 
-		Long kindid = component.getKindid();
-		if (kindid != null) {
-			document.add(new StringField(SearchConstants.COMPONENT_KINDID_FIELD, String.valueOf(kindid), Store.YES));
-		}
+		document.add(new StringField(SearchConstants.COMPONENT_KINDID_FIELD, String.valueOf(component.getKindid()),
+				Store.YES));
 		// if (component.getKind() != null) {
 		// 不用LongField
 		// document.add(new StringField(SearchConstants.COMPONENT_KINDID_FIELD,
@@ -258,21 +258,21 @@ public class IndexServiceImpl implements IndexService {
 		// Store.YES));
 		// }
 
-		Long brandid = component.getBrandid();
-		if (brandid != null) {
-			document.add(new StringField(SearchConstants.COMPONENT_BRANDID_FIELD, String.valueOf(brandid), Store.YES));
-		}
-		// if (component.getBrand() != null) {
-		// 不用LongField
-		// document.add(new StringField(SearchConstants.COMPONENT_BRANDID_FIELD,
-		// String.valueOf(component.getBrand().getId()), Store.YES));
-		// document.add(new
-		// StringField(SearchConstants.COMPONENT_BRANDUUID_FIELD,
-		// String.valueOf(component.getBrand().getUuid()), Store.YES));
-		// document.add(new
-		// StringField(SearchConstants.COMPONENT_BRANDNAME_FIELD,
-		// String.valueOf(component.getBrand().getNameCn()), Store.YES));
-		// }
+		document.add(new StringField(SearchConstants.COMPONENT_BRANDID_FIELD, String.valueOf(component.getBrandid()),
+				Store.YES));
+				// if (component.getBrand() != null) {
+				// 不用LongField
+				// document.add(new
+				// StringField(SearchConstants.COMPONENT_BRANDID_FIELD,
+				// String.valueOf(component.getBrand().getId()), Store.YES));
+				// document.add(new
+				// StringField(SearchConstants.COMPONENT_BRANDUUID_FIELD,
+				// String.valueOf(component.getBrand().getUuid()), Store.YES));
+				// document.add(new
+				// StringField(SearchConstants.COMPONENT_BRANDNAME_FIELD,
+				// String.valueOf(component.getBrand().getNameCn()),
+				// Store.YES));
+				// }
 
 		// TODO 属性值加入索引
 		return document;
@@ -310,11 +310,20 @@ public class IndexServiceImpl implements IndexService {
 		try {
 			indexWriter = indexWriterManager.get();
 			if (obj instanceof KindSimpleInfo) {
-				indexWriter.addDocument(toDocument((KindSimpleInfo) obj));
+				Document document = toDocument((KindSimpleInfo) obj);
+				if (document != null) {
+					indexWriter.addDocument(document);
+				}
 			} else if (obj instanceof BrandSimpleInfo) {
-				indexWriter.addDocument(toDocument((BrandSimpleInfo) obj));
+				Document document = toDocument((BrandSimpleInfo) obj);
+				if (document != null) {
+					indexWriter.addDocument(document);
+				}
 			} else if (obj instanceof ComponentSimpleInfo) {
-				indexWriter.addDocument(toDocument((ComponentSimpleInfo) obj));
+				Document document = toDocument((ComponentSimpleInfo) obj);
+				if (document != null) {
+					indexWriter.addDocument(document);
+				}
 			} else {
 				SearchConstants.logger.error("message parsing failed!");
 			}
@@ -337,25 +346,25 @@ public class IndexServiceImpl implements IndexService {
 			indexWriter = indexWriterManager.get();
 			if (obj instanceof KindSimpleInfo) {
 				KindSimpleInfo kind = (KindSimpleInfo) obj;
-				Long kindid = kind.getId();
-				if (kindid == null) {
-					return;
+				Document document = toDocument(kind);
+				if (document != null) {
+					indexWriter.updateDocument(new Term(SearchConstants.KIND_ID_FIELD, String.valueOf(kind.getId())),
+							document);
 				}
-				indexWriter.updateDocument(new Term(SearchConstants.KIND_ID_FIELD, String.valueOf(kindid)),
-						toDocument(kind));
 			} else if (obj instanceof BrandSimpleInfo) {
 				BrandSimpleInfo brand = (BrandSimpleInfo) obj;
-				Long brandid = brand.getId();
-				if (brandid == null) {
-					return;
+				Document document = toDocument((BrandSimpleInfo) obj);
+				if (document != null) {
+					indexWriter.updateDocument(new Term(SearchConstants.BRAND_ID_FIELD, String.valueOf(brand.getId())),
+							document);
 				}
-				indexWriter.updateDocument(new Term(SearchConstants.BRAND_ID_FIELD, String.valueOf(brandid)),
-						toDocument(brand));
 			} else if (obj instanceof ComponentSimpleInfo) {
 				ComponentSimpleInfo component = (ComponentSimpleInfo) obj;
-				indexWriter.updateDocument(
-						new Term(SearchConstants.COMPONENT_ID_FIELD, String.valueOf(component.getId())),
-						toDocument(component));
+				Document document = toDocument(component);
+				if (document != null) {
+					indexWriter.updateDocument(
+							new Term(SearchConstants.COMPONENT_ID_FIELD, String.valueOf(component.getId())), document);
+				}
 			} else {
 				SearchConstants.logger.error("message parsing failed!");
 			}