|
@@ -383,116 +383,83 @@ public class SearchServiceImpl implements SearchService {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public Set<Long> getKindIdsBySearchComponent(String keyword, String brandId) {
|
|
public Set<Long> getKindIdsBySearchComponent(String keyword, String brandId) {
|
|
|
- if (SearchUtils.isKeywordInvalid(keyword)) {
|
|
|
|
|
- throw new SearchException("搜索关键词无效:" + keyword);
|
|
|
|
|
|
|
+ Query filter = null;
|
|
|
|
|
+ // 筛选品牌
|
|
|
|
|
+ if (!StringUtils.isEmpty(brandId)) {
|
|
|
|
|
+ filter = new TermQuery(new Term(SearchConstants.COMPONENT_BR_ID_FIELD, brandId));
|
|
|
}
|
|
}
|
|
|
- IndexSearcher indexSearcher = SearchUtils.getIndexSearcher(SearchConstants.COMPONENT_TABLE_NAME);
|
|
|
|
|
-
|
|
|
|
|
- Set<Long> kindIds = new HashSet<Long>();
|
|
|
|
|
- try {
|
|
|
|
|
- BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
|
|
-
|
|
|
|
|
- keyword = URLDecoder.decode(keyword, "UTF-8");
|
|
|
|
|
- booleanQuery.add(setBoost(keyword), BooleanClause.Occur.MUST);
|
|
|
|
|
-
|
|
|
|
|
- // 筛选品牌
|
|
|
|
|
- if (!StringUtils.isEmpty(brandId)) {
|
|
|
|
|
- TermQuery brandQuery = new TermQuery(new Term(SearchConstants.COMPONENT_BR_ID_FIELD, brandId));
|
|
|
|
|
- booleanQuery.add(brandQuery, BooleanClause.Occur.MUST);
|
|
|
|
|
- }
|
|
|
|
|
- logger.info(booleanQuery.toString());
|
|
|
|
|
|
|
+ return collectBySearchComponent(keyword, filter, SearchConstants.COMPONENT_KI_ID_FIELD).getValues();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- DistinctGroupCollector collector = new DistinctGroupCollector(SearchConstants.COMPONENT_KI_ID_FIELD);
|
|
|
|
|
- indexSearcher.search(booleanQuery, collector);
|
|
|
|
|
- kindIds = collector.getValues();
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- logger.error("", e);
|
|
|
|
|
- } finally {
|
|
|
|
|
- SearchUtils.releaseIndexSearcher(indexSearcher);
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Set<Map<String, Object>> getKindsBySearchComponent(String keyword, String brandId) {
|
|
|
|
|
+ Query filter = null;
|
|
|
|
|
+ // 筛选品牌
|
|
|
|
|
+ if (!StringUtils.isEmpty(brandId)) {
|
|
|
|
|
+ filter = new TermQuery(new Term(SearchConstants.COMPONENT_BR_ID_FIELD, brandId));
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return kindIds;
|
|
|
|
|
|
|
+ return collectBySearchComponent(keyword, filter, SearchConstants.KIND_ID_FIELD,
|
|
|
|
|
+ new DistinctGroupCollector.CollectField(SearchConstants.KIND_ID_FIELD, "id"),
|
|
|
|
|
+ new DistinctGroupCollector.CollectField(SearchConstants.KIND_NAMECN_FIELD, "nameCn"))
|
|
|
|
|
+ .getCollectValues();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public List<Map<String, Object>> getKindsBySearchComponent(String keyword, String brandId) {
|
|
|
|
|
- Set<Long> kindIds = getKindIdsBySearchComponent(keyword, brandId);
|
|
|
|
|
- List<Map<String, Object>> kinds = new ArrayList<Map<String, Object>>();
|
|
|
|
|
- if (CollectionUtils.isEmpty(kindIds)) {
|
|
|
|
|
- return kinds;
|
|
|
|
|
|
|
+ public Set<Long> getBrandIdsBySearchComponent(String keyword, String kindId) {
|
|
|
|
|
+ Query filter = null;
|
|
|
|
|
+ // 筛选类目
|
|
|
|
|
+ if (!StringUtils.isEmpty(kindId)) {
|
|
|
|
|
+ filter = new TermQuery(new Term(SearchConstants.COMPONENT_KI_ID_FIELD, kindId));
|
|
|
}
|
|
}
|
|
|
|
|
+ return collectBySearchComponent(keyword, filter, SearchConstants.COMPONENT_BR_ID_FIELD).getValues();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
|
|
- for (Long kindId : kindIds) {
|
|
|
|
|
- booleanQuery.add(new TermQuery(new Term(SearchConstants.KIND_ID_FIELD, String.valueOf(kindId))),
|
|
|
|
|
- BooleanClause.Occur.SHOULD);
|
|
|
|
|
- }
|
|
|
|
|
- List<Document> documents = SearchUtils.getDocuments(SearchConstants.KIND_TABLE_NAME, booleanQuery).getContent();
|
|
|
|
|
- for (Document document : documents) {
|
|
|
|
|
- Map<String, Object> kind = new HashMap<String, Object>();
|
|
|
|
|
- kind.put("id", Long.parseLong(document.get(SearchConstants.KIND_ID_FIELD)));
|
|
|
|
|
- kind.put("nameCn", document.get(SearchConstants.KIND_NAMECN_FIELD));
|
|
|
|
|
- kinds.add(kind);
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Set<Map<String, Object>> getBrandsBySearchComponent(String keyword, String kindId) {
|
|
|
|
|
+ Query filter = null;
|
|
|
|
|
+ // 筛选类目
|
|
|
|
|
+ if (!StringUtils.isEmpty(kindId)) {
|
|
|
|
|
+ filter = new TermQuery(new Term(SearchConstants.COMPONENT_KI_ID_FIELD, kindId));
|
|
|
}
|
|
}
|
|
|
- return kinds;
|
|
|
|
|
|
|
+ return collectBySearchComponent(keyword, filter, SearchConstants.COMPONENT_BR_ID_FIELD,
|
|
|
|
|
+ new DistinctGroupCollector.CollectField(SearchConstants.COMPONENT_BR_ID_FIELD, "id"),
|
|
|
|
|
+ new DistinctGroupCollector.CollectField(SearchConstants.COMPONENT_BR_UUID_FIELD, "uuid"),
|
|
|
|
|
+ new DistinctGroupCollector.CollectField(SearchConstants.COMPONENT_BR_NAMECN_FIELD, "nameCn"))
|
|
|
|
|
+ .getCollectValues();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
- public Set<Long> getBrandIdsBySearchComponent(String keyword, String kindId) {
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 搜索器件时统计指定信息
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param keyword 关键词
|
|
|
|
|
+ * @param filter 过滤条件
|
|
|
|
|
+ * @param groupField 统计的单个字段(多为 id)
|
|
|
|
|
+ * @param collectFields 统计的多个字段(详细信息),可为空
|
|
|
|
|
+ * @return 统计信息
|
|
|
|
|
+ */
|
|
|
|
|
+ private DistinctGroupCollector collectBySearchComponent(String keyword, Query filter, String groupField, DistinctGroupCollector.CollectField... collectFields){
|
|
|
if (SearchUtils.isKeywordInvalid(keyword)) {
|
|
if (SearchUtils.isKeywordInvalid(keyword)) {
|
|
|
throw new SearchException("搜索关键词无效:" + keyword);
|
|
throw new SearchException("搜索关键词无效:" + keyword);
|
|
|
}
|
|
}
|
|
|
IndexSearcher indexSearcher = SearchUtils.getIndexSearcher(SearchConstants.COMPONENT_TABLE_NAME);
|
|
IndexSearcher indexSearcher = SearchUtils.getIndexSearcher(SearchConstants.COMPONENT_TABLE_NAME);
|
|
|
-
|
|
|
|
|
- Set<Long> brandIds = new HashSet<Long>();
|
|
|
|
|
try {
|
|
try {
|
|
|
BooleanQuery booleanQuery = new BooleanQuery();
|
|
BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
-
|
|
|
|
|
keyword = URLDecoder.decode(keyword, "UTF-8");
|
|
keyword = URLDecoder.decode(keyword, "UTF-8");
|
|
|
booleanQuery.add(setBoost(keyword), BooleanClause.Occur.MUST);
|
|
booleanQuery.add(setBoost(keyword), BooleanClause.Occur.MUST);
|
|
|
|
|
|
|
|
- // 筛选类目
|
|
|
|
|
- if (!StringUtils.isEmpty(kindId)) {
|
|
|
|
|
- TermQuery kindQuery = new TermQuery(new Term(SearchConstants.COMPONENT_KI_ID_FIELD, kindId));
|
|
|
|
|
- booleanQuery.add(kindQuery, BooleanClause.Occur.MUST);
|
|
|
|
|
|
|
+ if(filter != null){
|
|
|
|
|
+ booleanQuery.add(filter, Occur.FILTER);
|
|
|
}
|
|
}
|
|
|
logger.info(booleanQuery.toString());
|
|
logger.info(booleanQuery.toString());
|
|
|
|
|
|
|
|
- DistinctGroupCollector collector = new DistinctGroupCollector(SearchConstants.COMPONENT_BR_ID_FIELD);
|
|
|
|
|
|
|
+ DistinctGroupCollector collector = new DistinctGroupCollector(groupField, collectFields);
|
|
|
indexSearcher.search(booleanQuery, collector);
|
|
indexSearcher.search(booleanQuery, collector);
|
|
|
- brandIds = collector.getValues();
|
|
|
|
|
|
|
+ return collector;
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
- logger.error("", e);
|
|
|
|
|
|
|
+ throw new IllegalStateException("统计失败", e);
|
|
|
} finally {
|
|
} finally {
|
|
|
SearchUtils.releaseIndexSearcher(indexSearcher);
|
|
SearchUtils.releaseIndexSearcher(indexSearcher);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return brandIds;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public List<Map<String, Object>> getBrandsBySearchComponent(String keyword, String kindId) {
|
|
|
|
|
- Set<Long> brandIds = getBrandIdsBySearchComponent(keyword, kindId);
|
|
|
|
|
- List<Map<String, Object>> brands = new ArrayList<Map<String, Object>>();
|
|
|
|
|
- if (CollectionUtils.isEmpty(brandIds)) {
|
|
|
|
|
- return brands;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- BooleanQuery booleanQuery = new BooleanQuery();
|
|
|
|
|
- for (Long brandId : brandIds) {
|
|
|
|
|
- booleanQuery.add(new TermQuery(new Term(SearchConstants.BRAND_ID_FIELD, String.valueOf(brandId))),
|
|
|
|
|
- BooleanClause.Occur.SHOULD);
|
|
|
|
|
- }
|
|
|
|
|
- List<Document> documents = SearchUtils.getDocuments(SearchConstants.BRAND_TABLE_NAME, booleanQuery)
|
|
|
|
|
- .getContent();
|
|
|
|
|
- for (Document document : documents) {
|
|
|
|
|
- Map<String, Object> brand = new HashMap<String, Object>();
|
|
|
|
|
- brand.put("id", Long.parseLong(document.get(SearchConstants.BRAND_ID_FIELD)));
|
|
|
|
|
- brand.put("uuid", document.get(SearchConstants.BRAND_UUID_FIELD));
|
|
|
|
|
- brand.put("nameCn", document.get(SearchConstants.BRAND_NAMECN_FIELD));
|
|
|
|
|
- brands.add(brand);
|
|
|
|
|
- }
|
|
|
|
|
- return brands;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|