|
|
@@ -58,11 +58,23 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
|
|
|
@Override
|
|
|
public List<Long> getKindIds(String keyword) {
|
|
|
+ return getKindIds(keyword, Occur.MUST);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据关键字搜索产品类目id
|
|
|
+ *
|
|
|
+ * @param keyword
|
|
|
+ * 关键词
|
|
|
+ * @param occur
|
|
|
+ * @return 符合条件的类目id
|
|
|
+ */
|
|
|
+ private List<Long> getKindIds(String keyword, Occur occur) {
|
|
|
if (SearchUtils.isKeywordInvalid(keyword)) {
|
|
|
throw new SearchException("搜索关键词无效:" + keyword);
|
|
|
}
|
|
|
List<Long> ids = new ArrayList<Long>();
|
|
|
- BooleanQuery booleanQuery = SearchUtils.getBooleanQuery(SearchConstants.KIND_NAMECN_FIELD, keyword);
|
|
|
+ BooleanQuery booleanQuery = SearchUtils.getBooleanQuery(SearchConstants.KIND_NAMECN_FIELD, keyword, occur);
|
|
|
logger.info(booleanQuery.toString());
|
|
|
List<Document> documents = SearchUtils.getDocuments(SearchConstants.KIND_TABLE_NAME, booleanQuery);
|
|
|
for (Document document : documents) {
|
|
|
@@ -91,14 +103,26 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
|
|
|
@Override
|
|
|
public List<Long> getBrandIds(String keyword) {
|
|
|
+ return getBrandIds(keyword, Occur.MUST);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据关键字搜索产品品牌id
|
|
|
+ *
|
|
|
+ * @param keyword
|
|
|
+ * 关键词
|
|
|
+ * @param occur
|
|
|
+ * @return 符合条件的品牌id
|
|
|
+ */
|
|
|
+ private List<Long> getBrandIds(String keyword, Occur occur) {
|
|
|
if (SearchUtils.isKeywordInvalid(keyword)) {
|
|
|
throw new SearchException("搜索关键词无效:" + keyword);
|
|
|
}
|
|
|
List<Long> ids = new ArrayList<Long>();
|
|
|
BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
- booleanQuery.add(SearchUtils.getBooleanQuery(SearchConstants.BRAND_NAMECN_FIELD, keyword),
|
|
|
+ booleanQuery.add(SearchUtils.getBooleanQuery(SearchConstants.BRAND_NAMECN_FIELD, keyword, occur),
|
|
|
BooleanClause.Occur.SHOULD);
|
|
|
- booleanQuery.add(SearchUtils.getBooleanQuery(SearchConstants.BRAND_NAMEEN_FIELD, keyword),
|
|
|
+ booleanQuery.add(SearchUtils.getBooleanQuery(SearchConstants.BRAND_NAMEEN_FIELD, keyword, occur),
|
|
|
BooleanClause.Occur.SHOULD);
|
|
|
logger.info(booleanQuery.toString());
|
|
|
List<Document> documents = SearchUtils.getDocuments(SearchConstants.BRAND_TABLE_NAME, booleanQuery);
|
|
|
@@ -133,18 +157,40 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Map<String, Object> getComponentIds(String keyword, PageParams page) {
|
|
|
+ public Map<String, Object> getComponentIds(String keyword, PageParams pageParams) {
|
|
|
+ Map<String, Object> searchComponentIds = getComponentIds(keyword, pageParams, null, null);
|
|
|
+ int total = (int) searchComponentIds.get("total");
|
|
|
+ if (total != 0) {
|
|
|
+ return searchComponentIds;
|
|
|
+ }
|
|
|
+ List<Long> kindIds = getKindIds(keyword, Occur.SHOULD);
|
|
|
+ List<Long> brandIds = getBrandIds(keyword, Occur.SHOULD);
|
|
|
+ return getComponentIds(null, pageParams, kindIds, brandIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据关键词搜索产品
|
|
|
+ *
|
|
|
+ * @param keyword
|
|
|
+ * @param pageParams
|
|
|
+ * @param kindIds
|
|
|
+ * @param brandIds
|
|
|
+ * @return
|
|
|
+ * @throws SearchException
|
|
|
+ */
|
|
|
+ private Map<String, Object> getComponentIds(String keyword, PageParams pageParams, List<Long> kindIds,
|
|
|
+ List<Long> brandIds) throws SearchException {
|
|
|
// 因为器件、属性值的数据量远比类目、品牌大得多,而且器件搜索可能还需进行分页,
|
|
|
- // 所以涉及器件、属性值的搜索,都不能像类目和品牌一样直接利用SearchUtils.getDocuments方法
|
|
|
+ // 所以涉及器件、属性值的搜索,大都不能像类目和品牌一样直接利用SearchUtils.getDocuments方法
|
|
|
IndexSearcher indexSearcher = SearchUtils.getIndexSearcher(SearchConstants.COMPONENT_TABLE_NAME);
|
|
|
|
|
|
- if (page == null) {
|
|
|
- page = new PageParams();
|
|
|
+ if (pageParams == null) {
|
|
|
+ pageParams = new PageParams();
|
|
|
}
|
|
|
- if (page.getPage() == 0)
|
|
|
- page.setPage(1);
|
|
|
- if (page.getSize() == 0)
|
|
|
- page.setSize(20);
|
|
|
+ if (pageParams.getPage() == 0)
|
|
|
+ pageParams.setPage(1);
|
|
|
+ if (pageParams.getSize() == 0)
|
|
|
+ pageParams.setSize(20);
|
|
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
List<Long> ids = new ArrayList<Long>();
|
|
|
@@ -156,7 +202,7 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
booleanQuery.add(prefixQuery, BooleanClause.Occur.MUST);
|
|
|
}
|
|
|
|
|
|
- Map<String, Object> filters = page.getFilters();
|
|
|
+ Map<String, Object> filters = pageParams.getFilters();
|
|
|
if (!CollectionUtils.isEmpty(filters)) {
|
|
|
// 筛选类目
|
|
|
if (!StringUtils.isEmpty(filters.get(com.uas.search.utils.SearchConstants.COMPONENT_KINDID_KEY))) {
|
|
|
@@ -225,15 +271,32 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ if (!CollectionUtils.isEmpty(kindIds)) {
|
|
|
+ BooleanQuery booleanQuery2 = new BooleanQuery();
|
|
|
+ for (Long id : kindIds) {
|
|
|
+ booleanQuery2.add(new TermQuery(new Term(SearchConstants.COMPONENT_KINDID_FIELD, id.toString())),
|
|
|
+ Occur.SHOULD);
|
|
|
+ }
|
|
|
+ booleanQuery.add(booleanQuery2, Occur.MUST);
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(brandIds)) {
|
|
|
+ BooleanQuery booleanQuery2 = new BooleanQuery();
|
|
|
+ for (Long id : brandIds) {
|
|
|
+ booleanQuery2.add(new TermQuery(new Term(SearchConstants.COMPONENT_BRANDID_FIELD, id.toString())),
|
|
|
+ Occur.SHOULD);
|
|
|
+ }
|
|
|
+ booleanQuery.add(booleanQuery2, Occur.MUST);
|
|
|
+ }
|
|
|
logger.info(booleanQuery.toString());
|
|
|
TopDocs hits;
|
|
|
- if (page.getPage() > 1) {// 不是第一页
|
|
|
- TopDocs previousHits = indexSearcher.search(booleanQuery, (page.getPage() - 1) * page.getSize());
|
|
|
+ if (pageParams.getPage() > 1) {// 不是第一页
|
|
|
+ TopDocs previousHits = indexSearcher.search(booleanQuery,
|
|
|
+ (pageParams.getPage() - 1) * pageParams.getSize());
|
|
|
ScoreDoc[] previousScoreDocs = previousHits.scoreDocs;
|
|
|
ScoreDoc after = previousScoreDocs[previousScoreDocs.length - 1];
|
|
|
- hits = indexSearcher.searchAfter(after, booleanQuery, page.getSize());
|
|
|
+ hits = indexSearcher.searchAfter(after, booleanQuery, pageParams.getSize());
|
|
|
} else {
|
|
|
- hits = indexSearcher.search(booleanQuery, page.getSize());
|
|
|
+ hits = indexSearcher.search(booleanQuery, pageParams.getSize());
|
|
|
}
|
|
|
|
|
|
ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
@@ -246,8 +309,8 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
|
|
|
ids.add(Long.parseLong(componentId));
|
|
|
}
|
|
|
map.put("componentIds", ids);
|
|
|
- map.put("page", page.getPage());
|
|
|
- map.put("size", page.getSize());
|
|
|
+ map.put("page", pageParams.getPage());
|
|
|
+ map.put("size", pageParams.getSize());
|
|
|
map.put("total", hits.totalHits);
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|