Browse Source

sort similar keywords using StringFieldComparatorSource

sunyj 8 years ago
parent
commit
68220881ea

+ 26 - 33
mall-search/src/main/java/com/uas/search/service/impl/SearchServiceImpl.java

@@ -12,10 +12,11 @@ import com.uas.search.grouping.DistinctGroupCollector;
 import com.uas.search.grouping.GoodsGroupCollector;
 import com.uas.search.model.*;
 import com.uas.search.service.SearchService;
-import com.uas.search.sort.SimilarValuesFieldComparatorSource;
 import com.uas.search.sort.StringFieldComparatorSource;
+import com.uas.search.util.CollectionUtils;
 import com.uas.search.util.DocumentToObjectUtils;
 import com.uas.search.util.SearchUtils;
+import com.uas.search.util.StringUtils;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.index.Term;
 import org.apache.lucene.search.*;
@@ -24,8 +25,6 @@ import org.apache.lucene.search.SortField.Type;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
-import com.uas.search.util.CollectionUtils;
-import com.uas.search.util.StringUtils;
 
 import java.io.IOException;
 import java.net.URLDecoder;
@@ -511,7 +510,9 @@ public class SearchServiceImpl implements SearchService {
 			PrefixQuery prefixQuery = new PrefixQuery(
 					new Term(SearchConstants.COMPONENT_CODE_FIELD, componentCode.toLowerCase()));
 			logger.info(prefixQuery.toString());
-			TopDocs hits = indexSearcher.search(prefixQuery, size);
+
+            Sort sort = new Sort(new SortField(SearchConstants.COMPONENT_CODE_FIELD, new StringFieldComparatorSource(componentCode)));
+			TopDocs hits = indexSearcher.search(prefixQuery, size, sort);
 			ScoreDoc[] scoreDocs = hits.scoreDocs;
 			for (ScoreDoc scoreDoc : scoreDocs) {
 				Set<String> fieldsToLoad = new HashSet<>();
@@ -550,7 +551,9 @@ public class SearchServiceImpl implements SearchService {
 				BooleanClause.Occur.SHOULD);
 		logger.info(booleanQuery.toString());
 
-		List<Document> documents = SearchUtils.getDocuments(SearchConstants.BRAND_TABLE_NAME, booleanQuery, null, size)
+        Sort sort = new Sort(new SortField(SearchConstants.BRAND_NAMEEN_UNTOKENIZED_FIELD, new StringFieldComparatorSource(brandName)),
+                new SortField(SearchConstants.BRAND_NAMECN_UNTOKENIZED_FIELD, new StringFieldComparatorSource(brandName)));
+		List<Document> documents = SearchUtils.getDocuments(SearchConstants.BRAND_TABLE_NAME, booleanQuery, sort, null, size)
 				.getContent();
 		for (Document document : documents) {
 			Map<String, Object> brand = new HashMap<>();
@@ -613,7 +616,9 @@ public class SearchServiceImpl implements SearchService {
 			}
 		}
 		logger.info(booleanQuery.toString());
-		List<Document> documents = SearchUtils.getDocuments(SearchConstants.KIND_TABLE_NAME, booleanQuery, null, size).getContent();
+
+        Sort sort = new Sort(new SortField(SearchConstants.KIND_NAMECN_UNTOKENIZED_FIELD, new StringFieldComparatorSource(kindName)));
+		List<Document> documents = SearchUtils.getDocuments(SearchConstants.KIND_TABLE_NAME, booleanQuery, sort, null, size).getContent();
 		for (Document document : documents) {
 			Map<String, Object> map = new HashMap<>();
 			map.put("id", Long.parseLong(document.get(SearchConstants.KIND_ID_FIELD)));
@@ -693,7 +698,7 @@ public class SearchServiceImpl implements SearchService {
 	 */
 	private List<String> getSimilarComponentCodes(String componentCode, Integer size) {
 		return getSimilarValues(SearchConstants.COMPONENT_TABLE_NAME, SearchConstants.COMPONENT_CODE_FIELD,
-				componentCode.toLowerCase(), QueryType.PREFIX_QUERY, size);
+                SearchConstants.COMPONENT_CODE_FIELD, componentCode.toLowerCase(), size);
 	}
 
 	/**
@@ -706,7 +711,7 @@ public class SearchServiceImpl implements SearchService {
 	private List<String> getSimilarBrandNames(String brandName, Integer size) {
 		// 获取相似的中文品牌
 		List<String> nameCns = getSimilarValues(SearchConstants.BRAND_TABLE_NAME, SearchConstants.BRAND_NAMECN_FIELD,
-				brandName, QueryType.BOOLEAN_QUERY, size);
+                SearchConstants.BRAND_NAMECN_UNTOKENIZED_FIELD, brandName, size);
 		// 相似的中文品牌数量足够,直接返回
 		if (nameCns != null && nameCns.size() == SIMILAR_NUM) {
 			return nameCns;
@@ -715,7 +720,7 @@ public class SearchServiceImpl implements SearchService {
 		List<String> names = nameCns;
 		// 获取相似的英文品牌
 		List<String> nameEns = getSimilarValues(SearchConstants.BRAND_TABLE_NAME, SearchConstants.BRAND_NAMEEN_FIELD,
-				brandName, QueryType.BOOLEAN_QUERY, size);
+                SearchConstants.BRAND_NAMEEN_UNTOKENIZED_FIELD, brandName, size);
 		names.addAll(nameEns);
 		return names;
 	}
@@ -728,12 +733,8 @@ public class SearchServiceImpl implements SearchService {
 	 * @return
 	 */
 	private List<String> getSimilarKindNames(String kindName, Integer size) {
-		return getSimilarValues(SearchConstants.KIND_TABLE_NAME, SearchConstants.KIND_NAMECN_FIELD, kindName,
-				QueryType.BOOLEAN_QUERY, size);
-	}
-
-	private enum QueryType {
-		BOOLEAN_QUERY, PREFIX_QUERY
+		return getSimilarValues(SearchConstants.KIND_TABLE_NAME, SearchConstants.KIND_NAMECN_FIELD,
+                SearchConstants.KIND_NAMECN_UNTOKENIZED_FIELD, kindName, size);
 	}
 
 	/**
@@ -745,26 +746,18 @@ public class SearchServiceImpl implements SearchService {
 	 * @param size 指定的联想词数目
 	 * @return
 	 */
-	private List<String> getSimilarValues(String tableName, String field, String keyword, QueryType queryType, Integer size) {
+	private List<String> getSimilarValues(String tableName, String field, String sortField, String keyword, Integer size) {
 		if (SearchUtils.isKeywordInvalid(keyword)) {
 			throw new SearchException("输入无效:" + keyword);
 		}
-		if (queryType == null) {
-			queryType = QueryType.BOOLEAN_QUERY;
-		}
 		IndexSearcher indexSearcher = SearchUtils.getIndexSearcher(tableName);
 
 		List<String> result = new ArrayList<>();
 		try {
-			Query query = null;
-			if (queryType == QueryType.BOOLEAN_QUERY) {
-				query = SearchUtils.getBooleanQuery(field, keyword);
-			} else if (queryType == QueryType.PREFIX_QUERY) {
-				query = new PrefixQuery(new Term(field, keyword));
-			}
+			Query query = SearchUtils.getBooleanQuery(field, keyword);
 			logger.info(query.toString());
-			Sort sort = new Sort(new SortField(field, new SimilarValuesFieldComparatorSource()));
-			TopDocs hits = indexSearcher.search(query, size, sort, true, false);
+			Sort sort = new Sort(new SortField(sortField, new StringFieldComparatorSource(keyword)));
+			TopDocs hits = indexSearcher.search(query, size, sort);
 			ScoreDoc[] scoreDocs = hits.scoreDocs;
 			for (ScoreDoc scoreDoc : scoreDocs) {
 				Set<String> fieldsToLoad = new HashSet<>();
@@ -803,18 +796,18 @@ public class SearchServiceImpl implements SearchService {
 	}
 
 	/**
-	 * 删除collection内startIndex(含)后的元素
+	 * 删除集合内 startIndex(含)后的元素
 	 * 
-	 * @param collection
+	 * @param list
 	 * @param startIndex
 	 */
-	private void removeElements(Collection<? extends String> collection, int startIndex) {
-		if (CollectionUtils.isEmpty(collection)) {
+	private void removeElements(List<? extends String> list, int startIndex) {
+		if (CollectionUtils.isEmpty(list)) {
 			return;
 		}
-		int listsSize = collection.size();
+		int listsSize = list.size();
 		for (int i = listsSize - 1; i >= startIndex; i--) {
-			collection.remove(i);
+			list.remove(i);
 		}
 	}