|
|
@@ -59,14 +59,13 @@ public class SearchServiceImpl implements SearchService {
|
|
|
if (isKeywordInvalid(keyword)) {
|
|
|
throw new IllegalArgumentException("搜索关键词无效");
|
|
|
}
|
|
|
-
|
|
|
- List<Long> ids = new ArrayList<Long>();
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
throw new RuntimeException("获取索引文件失败");
|
|
|
}
|
|
|
|
|
|
+ List<Long> ids = new ArrayList<Long>();
|
|
|
try {
|
|
|
BooleanQuery booleanQuery = getBooleanQuery(SearchConstants.KIND_NAMECN_FIELD, keyword);
|
|
|
TopDocs hits = searcher.search(booleanQuery, TOP_NUM);
|
|
|
@@ -90,14 +89,13 @@ public class SearchServiceImpl implements SearchService {
|
|
|
if (isKeywordInvalid(keyword)) {
|
|
|
throw new IllegalArgumentException("搜索关键词无效");
|
|
|
}
|
|
|
-
|
|
|
- List<Map<String, Object>> kinds = new ArrayList<Map<String, Object>>();
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
throw new RuntimeException("获取索引文件失败");
|
|
|
}
|
|
|
|
|
|
+ List<Map<String, Object>> kinds = new ArrayList<Map<String, Object>>();
|
|
|
try {
|
|
|
BooleanQuery booleanQuery = getBooleanQuery(SearchConstants.KIND_NAMECN_FIELD, keyword);
|
|
|
TopDocs hits = searcher.search(booleanQuery, TOP_NUM);
|
|
|
@@ -122,14 +120,13 @@ public class SearchServiceImpl implements SearchService {
|
|
|
if (isKeywordInvalid(keyword)) {
|
|
|
throw new IllegalArgumentException("搜索关键词无效");
|
|
|
}
|
|
|
-
|
|
|
- List<Long> ids = new ArrayList<Long>();
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
throw new RuntimeException("获取索引文件失败");
|
|
|
}
|
|
|
|
|
|
+ List<Long> ids = new ArrayList<Long>();
|
|
|
try {
|
|
|
BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
booleanQuery.add(getBooleanQuery(SearchConstants.BRAND_NAMECN_FIELD, keyword), BooleanClause.Occur.SHOULD);
|
|
|
@@ -157,14 +154,13 @@ public class SearchServiceImpl implements SearchService {
|
|
|
if (isKeywordInvalid(keyword)) {
|
|
|
throw new IllegalArgumentException("搜索关键词无效");
|
|
|
}
|
|
|
-
|
|
|
- List<Map<String, Object>> brands = new ArrayList<Map<String, Object>>();
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
throw new RuntimeException("获取索引文件失败");
|
|
|
}
|
|
|
|
|
|
+ List<Map<String, Object>> brands = new ArrayList<Map<String, Object>>();
|
|
|
try {
|
|
|
BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
booleanQuery.add(getBooleanQuery(SearchConstants.BRAND_NAMECN_FIELD, keyword), BooleanClause.Occur.SHOULD);
|
|
|
@@ -191,8 +187,6 @@ public class SearchServiceImpl implements SearchService {
|
|
|
|
|
|
@Override
|
|
|
public Map<String, Object> getComponentIds(String keyword, PageParams page) {
|
|
|
- Map<String, Object> map = new HashMap<String, Object>();
|
|
|
- List<Long> ids = new ArrayList<Long>();
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
@@ -207,6 +201,8 @@ public class SearchServiceImpl implements SearchService {
|
|
|
if (page.getSize() == 0)
|
|
|
page.setSize(20);
|
|
|
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
+ List<Long> ids = new ArrayList<Long>();
|
|
|
try {
|
|
|
BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
if (!isKeywordInvalid(keyword)) {
|
|
|
@@ -282,14 +278,13 @@ public class SearchServiceImpl implements SearchService {
|
|
|
if (isKeywordInvalid(keyword)) {
|
|
|
throw new IllegalArgumentException("搜索关键词无效");
|
|
|
}
|
|
|
-
|
|
|
- Set<Long> kindIds = new HashSet<Long>();
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
throw new RuntimeException("获取索引文件失败");
|
|
|
}
|
|
|
|
|
|
+ Set<Long> kindIds = new HashSet<Long>();
|
|
|
try {
|
|
|
BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
|
|
|
@@ -367,14 +362,13 @@ public class SearchServiceImpl implements SearchService {
|
|
|
if (isKeywordInvalid(keyword)) {
|
|
|
throw new IllegalArgumentException("搜索关键词无效");
|
|
|
}
|
|
|
-
|
|
|
- Set<Long> brandIds = new HashSet<Long>();
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
throw new RuntimeException("获取索引文件失败");
|
|
|
}
|
|
|
|
|
|
+ Set<Long> brandIds = new HashSet<Long>();
|
|
|
try {
|
|
|
BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
|
|
|
@@ -449,34 +443,45 @@ public class SearchServiceImpl implements SearchService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<String> getSimilarComponentCodes(String componentCode) {
|
|
|
- if (isKeywordInvalid(componentCode)) {
|
|
|
- throw new IllegalArgumentException("输入无效");
|
|
|
- }
|
|
|
-
|
|
|
- List<String> codes = new ArrayList<>();
|
|
|
- searcherManager.maybeReopen();
|
|
|
- IndexSearcher searcher = searcherManager.get();
|
|
|
- if (searcher == null) {
|
|
|
- throw new RuntimeException("获取索引文件失败");
|
|
|
+ public List<String> getSimilarKeywords(String keyword) {
|
|
|
+ // 相似的器件原厂型号数量足够,直接返回
|
|
|
+ List<String> componentCodes = getSimilarComponentCodes(keyword);
|
|
|
+ if (componentCodes != null && componentCodes.size() == SIMILAR_NUM) {
|
|
|
+ return componentCodes;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> result = componentCodes;
|
|
|
+ if (result == null) {
|
|
|
+ result = new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取相似类目
|
|
|
+ List<String> kindNames = getSimilarKindNames(keyword);
|
|
|
+ if (!CollectionUtils.isEmpty(kindNames)) {
|
|
|
+ result.addAll(kindNames);
|
|
|
+ // 如果总的数量超出SIMILAR_NUM,去除多余的元素
|
|
|
+ if (result.size() > SIMILAR_NUM) {
|
|
|
+ removeElements(result, SIMILAR_NUM);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- try {
|
|
|
- BooleanQuery booleanQuery = getBooleanQuery(SearchConstants.COMPONENT_CODE_FIELD, componentCode);
|
|
|
- TopDocs hits = searcher.search(booleanQuery, SIMILAR_NUM);
|
|
|
- ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
- for (ScoreDoc scoreDoc : scoreDocs) {
|
|
|
- Set<String> fieldsToLoad = new HashSet<>();
|
|
|
- fieldsToLoad.add(SearchConstants.COMPONENT_CODE_FIELD);
|
|
|
- Document document = searcher.doc(scoreDoc.doc, fieldsToLoad);
|
|
|
- codes.add(document.get(SearchConstants.COMPONENT_CODE_FIELD));
|
|
|
+ // 获取相似品牌
|
|
|
+ List<String> brandNames = getSimilarBrandNames(keyword);
|
|
|
+ if (!CollectionUtils.isEmpty(brandNames)) {
|
|
|
+ result.addAll(brandNames);
|
|
|
+ if (result.size() > SIMILAR_NUM) {
|
|
|
+ removeElements(result, SIMILAR_NUM);
|
|
|
+ return result;
|
|
|
}
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- searcherManager.release(searcher);
|
|
|
}
|
|
|
- return codes;
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<String> getSimilarComponentCodes(String componentCode) {
|
|
|
+ return getSimilarValues(SearchConstants.COMPONENT_CODE_FIELD, componentCode);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -484,14 +489,13 @@ public class SearchServiceImpl implements SearchService {
|
|
|
if (isKeywordInvalid(brandName)) {
|
|
|
throw new IllegalArgumentException("输入无效");
|
|
|
}
|
|
|
-
|
|
|
- List<Map<String, Object>> brands = new ArrayList<Map<String, Object>>();
|
|
|
searcherManager.maybeReopen();
|
|
|
IndexSearcher searcher = searcherManager.get();
|
|
|
if (searcher == null) {
|
|
|
throw new RuntimeException("获取索引文件失败");
|
|
|
}
|
|
|
|
|
|
+ List<Map<String, Object>> brands = new ArrayList<Map<String, Object>>();
|
|
|
// 品牌名称带有空格,并且中英文名并无一定顺序,因此对nameCn、nameEn均要搜索
|
|
|
BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
try {
|
|
|
@@ -525,6 +529,83 @@ public class SearchServiceImpl implements SearchService {
|
|
|
return brands;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据输入获取相似的品牌名称
|
|
|
+ *
|
|
|
+ * @param brandName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<String> getSimilarBrandNames(String brandName) {
|
|
|
+ // 获取相似的中文品牌
|
|
|
+ List<String> nameCns = getSimilarValues(SearchConstants.BRAND_NAMECN_FIELD, brandName);
|
|
|
+ // 相似的中文品牌数量足够,直接返回
|
|
|
+ if (nameCns != null && nameCns.size() == SIMILAR_NUM) {
|
|
|
+ return nameCns;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> names = nameCns;
|
|
|
+ if (names == null) {
|
|
|
+ names = new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取相似的英文品牌
|
|
|
+ List<String> nameEns = getSimilarValues(SearchConstants.BRAND_NAMEEN_FIELD, brandName);
|
|
|
+ if (CollectionUtils.isEmpty(nameEns)) {
|
|
|
+ return names;
|
|
|
+ }
|
|
|
+ names.addAll(nameEns);
|
|
|
+ if (names.size() > SIMILAR_NUM) {
|
|
|
+ removeElements(names, SIMILAR_NUM);
|
|
|
+ }
|
|
|
+ return names;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据输入获取相似的类目名称
|
|
|
+ *
|
|
|
+ * @param kindName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<String> getSimilarKindNames(String kindName) {
|
|
|
+ return getSimilarValues(SearchConstants.KIND_NAMECN_FIELD, kindName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据输入值获取该域相似的值
|
|
|
+ *
|
|
|
+ * @param field
|
|
|
+ * @param keyword
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<String> getSimilarValues(String field, String keyword) {
|
|
|
+ if (isKeywordInvalid(keyword)) {
|
|
|
+ throw new IllegalArgumentException("输入无效");
|
|
|
+ }
|
|
|
+ searcherManager.maybeReopen();
|
|
|
+ IndexSearcher indexSearcher = searcherManager.get();
|
|
|
+ if (indexSearcher == null) {
|
|
|
+ throw new RuntimeException("获取索引文件失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> result = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ BooleanQuery booleanQuery = getBooleanQuery(field, keyword);
|
|
|
+ TopDocs hits = indexSearcher.search(booleanQuery, SIMILAR_NUM);
|
|
|
+ ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
+ for (ScoreDoc scoreDoc : scoreDocs) {
|
|
|
+ Set<String> fieldsToLoad = new HashSet<>();
|
|
|
+ fieldsToLoad.add(field);
|
|
|
+ Document document = indexSearcher.doc(scoreDoc.doc, fieldsToLoad);
|
|
|
+ result.add(document.get(field));
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ searcherManager.release(indexSearcher);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 对搜索词进行分词后组合得到BooleanQuery
|
|
|
*
|
|
|
@@ -568,4 +649,20 @@ public class SearchServiceImpl implements SearchService {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 删除lists内startIndex(含)后的元素
|
|
|
+ *
|
|
|
+ * @param lists
|
|
|
+ * @param startIndex
|
|
|
+ */
|
|
|
+ private void removeElements(List<String> lists, int startIndex) {
|
|
|
+ if (CollectionUtils.isEmpty(lists)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ int listsSize = lists.size();
|
|
|
+ for (int i = listsSize - 1; i >= startIndex; i--) {
|
|
|
+ lists.remove(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|