|
|
@@ -809,10 +809,10 @@ public class SearchServiceImpl implements SearchService {
|
|
|
// 先根据品牌搜索,品牌不存在再搜索型号等
|
|
|
keywordFields.add(SearchConstants.GOODS_BR_NAME_CN_UNTOKENIZED_FIELD);
|
|
|
keywordFields.add(SearchConstants.GOODS_BR_NAME_EN_UNTOKENIZED_FIELD);
|
|
|
- Map<String, Object> goodsIds = getGoodsIds(keyword, keywordFields, false, pageParams);
|
|
|
+ Map<String, Object> goodsIds = getGoodsIds(keyword, keywordFields, false, pageParams, false);
|
|
|
if (CollectionUtils.isEmpty(goodsIds) || goodsIds.get("componentIds") == null
|
|
|
|| JSONObject.parseArray(goodsIds.get("componentIds").toString()).isEmpty()) {
|
|
|
- goodsIds = getGoodsIds(keyword, null, true, pageParams);
|
|
|
+ goodsIds = getGoodsIds(keyword, null, true, pageParams, true);
|
|
|
}
|
|
|
return goodsIds;
|
|
|
}
|
|
|
@@ -824,11 +824,12 @@ public class SearchServiceImpl implements SearchService {
|
|
|
* @param tokenized
|
|
|
* 是否分词
|
|
|
* @param pageParams
|
|
|
+ * @param recursivelyGet 是否递归获取(逐步降低精度,直到只匹配一个字符)
|
|
|
* @return
|
|
|
* @throws SearchException
|
|
|
*/
|
|
|
private Map<String, Object> getGoodsIds(String keyword, List<String> keywordFields, Boolean tokenized,
|
|
|
- PageParams pageParams) throws SearchException {
|
|
|
+ PageParams pageParams, Boolean recursivelyGet) throws SearchException {
|
|
|
// 因为器件、属性值的数据量远比类目、品牌大得多,而且器件搜索可能还需进行分页,
|
|
|
// 所以涉及器件、属性值的搜索,大都不能像类目和品牌一样直接利用SearchUtils.getDocuments方法
|
|
|
IndexSearcher indexSearcher = SearchUtils.getIndexSearcher(SearchConstants.GOODS_TABLE_NAME);
|
|
|
@@ -961,6 +962,10 @@ public class SearchServiceImpl implements SearchService {
|
|
|
hits = indexSearcher.search(booleanQuery, pageParams.getSize(), sort, true, false);
|
|
|
}
|
|
|
|
|
|
+ // 如果没有结果,则降低精度,直至 keyword 长度为 1
|
|
|
+ if(recursivelyGet && hits.totalHits < 1 && !SearchUtils.isKeywordInvalid(keyword) && keyword.length() > 1){
|
|
|
+ return getGoodsIds(keyword.substring(0, keyword.length() - 1), keywordFields, tokenized, pageParams, recursivelyGet);
|
|
|
+ }
|
|
|
ScoreDoc[] scoreDocs = hits.scoreDocs;
|
|
|
for (ScoreDoc scoreDoc : scoreDocs) {
|
|
|
// 数据量太大,需要指定将获取的数据(以免载入不必要的数据,降低速度)
|
|
|
@@ -997,9 +1002,9 @@ public class SearchServiceImpl implements SearchService {
|
|
|
// 先根据品牌搜索,品牌不存在再搜索型号等
|
|
|
keywordFields.add(SearchConstants.GOODS_BR_NAME_CN_UNTOKENIZED_FIELD);
|
|
|
keywordFields.add(SearchConstants.GOODS_BR_NAME_EN_UNTOKENIZED_FIELD);
|
|
|
- List<Map<String, Object>> result = collectBySearchGoods(keyword, keywordFields, false, collectedField, filters);
|
|
|
+ List<Map<String, Object>> result = collectBySearchGoods(keyword, keywordFields, false, collectedField, filters, false);
|
|
|
if (CollectionUtils.isEmpty(result)) {
|
|
|
- result = collectBySearchGoods(keyword, null, true, collectedField, filters);
|
|
|
+ result = collectBySearchGoods(keyword, null, true, collectedField, filters, true);
|
|
|
}
|
|
|
return result;
|
|
|
|
|
|
@@ -1013,13 +1018,23 @@ public class SearchServiceImpl implements SearchService {
|
|
|
* 是否分词
|
|
|
* @param collectedField
|
|
|
* @param filters
|
|
|
+ * @param recursivelyGet 是否递归获取(逐步降低精度,直到只匹配一个字符)
|
|
|
* @return
|
|
|
*/
|
|
|
private List<Map<String, Object>> collectBySearchGoods(String keyword, List<String> keywordFields,
|
|
|
- Boolean tokenized, CollectField collectedField, Map<FilterField, Object> filters) {
|
|
|
+ Boolean tokenized, CollectField collectedField, Map<FilterField, Object> filters, Boolean recursivelyGet) {
|
|
|
if (collectedField == null && CollectionUtils.isEmpty(filters)) {
|
|
|
throw new SearchException("参数不合法:collectedField=" + collectedField + ", filter=" + filters);
|
|
|
}
|
|
|
+ // 与批次搜索的搜索词保持一致,最终有结果的 keyword 是相同的
|
|
|
+ if(recursivelyGet != null && recursivelyGet){
|
|
|
+ PageParams pageParams = new PageParams();
|
|
|
+ pageParams.setFilters(filters);
|
|
|
+ Map<String, Object> goodsIds = getGoodsIds(keyword, keywordFields, tokenized, pageParams, false);
|
|
|
+ if(Integer.parseInt(goodsIds.get("total").toString()) <1){
|
|
|
+ return collectBySearchGoods(keyword.substring(0, keyword.length() - 1), keywordFields, tokenized, collectedField, filters, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
IndexSearcher indexSearcher = SearchUtils.getIndexSearcher(SearchConstants.GOODS_TABLE_NAME);
|
|
|
|
|
|
List<Map<String, Object>> result = new ArrayList<>();
|