Authored by hugufei

模糊搜索支持资源位

@@ -3,6 +3,7 @@ package com.yoho.search.service.helper; @@ -3,6 +3,7 @@ package com.yoho.search.service.helper;
3 import com.alibaba.fastjson.JSONObject; 3 import com.alibaba.fastjson.JSONObject;
4 import com.yoho.search.aop.productlist.ProductListWithSkn; 4 import com.yoho.search.aop.productlist.ProductListWithSkn;
5 import com.yoho.search.base.utils.ProductIndexEsField; 5 import com.yoho.search.base.utils.ProductIndexEsField;
  6 +import com.yoho.search.common.SearchRequestParams;
6 import com.yoho.search.core.es.model.SearchParam; 7 import com.yoho.search.core.es.model.SearchParam;
7 import com.yoho.search.core.es.model.SearchResult; 8 import com.yoho.search.core.es.model.SearchResult;
8 import com.yoho.search.service.index.*; 9 import com.yoho.search.service.index.*;
@@ -10,7 +11,6 @@ import com.yoho.search.service.index.promotion.PromotionIndexBaseService; @@ -10,7 +11,6 @@ import com.yoho.search.service.index.promotion.PromotionIndexBaseService;
10 import com.yoho.search.service.index.promotion.PromotionPriceService; 11 import com.yoho.search.service.index.promotion.PromotionPriceService;
11 import com.yoho.search.service.recall.strategy.NotRecallTypeEnum; 12 import com.yoho.search.service.recall.strategy.NotRecallTypeEnum;
12 import org.apache.commons.collections.MapUtils; 13 import org.apache.commons.collections.MapUtils;
13 -import org.apache.commons.lang.StringUtils;  
14 import org.springframework.beans.factory.annotation.Autowired; 14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.stereotype.Component; 15 import org.springframework.stereotype.Component;
16 16
@@ -60,19 +60,19 @@ public class ProductListHelper { @@ -60,19 +60,19 @@ public class ProductListHelper {
60 60
61 private SearchParam innerBuildProductListSearchParam(Map<String, String> paramMap, boolean needPersional, boolean containPhrase) throws Exception { 61 private SearchParam innerBuildProductListSearchParam(Map<String, String> paramMap, boolean needPersional, boolean containPhrase) throws Exception {
62 // 1)验证查询条数 62 // 1)验证查询条数
63 - int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));  
64 - int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));  
65 - if (page < 1 || pageSize < 0) { 63 + int page = MapUtils.getIntValue(paramMap,SearchRequestParams.PARAM_SEARCH_PAGE,1);
  64 + int viewNum = MapUtils.getIntValue(paramMap, SearchRequestParams.PARAM_SEARCH_VIEWNUM,20);
  65 + if (page < 1 || viewNum < 0) {
66 throw new IllegalArgumentException("分页参数不合法"); 66 throw new IllegalArgumentException("分页参数不合法");
67 } 67 }
68 - if (pageSize > 100) {  
69 - pageSize = 100; 68 + if (viewNum > 100) {
  69 + viewNum = 100;
70 } 70 }
71 // 2)构建基本查询参数 71 // 2)构建基本查询参数
72 SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, needPersional); 72 SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, needPersional);
73 searchParam.setAggregationBuilders(null); 73 searchParam.setAggregationBuilders(null);
74 - searchParam.setSize(pageSize);  
75 - searchParam.setOffset((page - 1) * pageSize); 74 + searchParam.setSize(viewNum);
  75 + searchParam.setOffset((page - 1) * viewNum);
76 // 3)设置排序字段 76 // 3)设置排序字段
77 searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap)); 77 searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap));
78 // 4)设置返回的结果 78 // 4)设置返回的结果
  1 +package com.yoho.search.service.index;
  2 +
  3 +import com.google.common.cache.CacheBuilder;
  4 +import com.google.common.cache.CacheLoader;
  5 +import com.google.common.cache.LoadingCache;
  6 +import com.yoho.search.base.utils.DateUtil;
  7 +import com.yoho.search.base.utils.ISearchConstants;
  8 +import com.yoho.search.common.SearchCommonService;
  9 +import com.yoho.search.common.utils.ImageUrlAssist;
  10 +import com.yoho.search.core.es.model.SearchParam;
  11 +import com.yoho.search.core.es.model.SearchResult;
  12 +import org.apache.commons.collections.CollectionUtils;
  13 +import org.apache.commons.collections.MapUtils;
  14 +import org.elasticsearch.index.query.BoolQueryBuilder;
  15 +import org.elasticsearch.index.query.QueryBuilders;
  16 +import org.elasticsearch.search.sort.SortBuilder;
  17 +import org.elasticsearch.search.sort.SortBuilders;
  18 +import org.elasticsearch.search.sort.SortOrder;
  19 +import org.slf4j.Logger;
  20 +import org.slf4j.LoggerFactory;
  21 +import org.springframework.beans.factory.annotation.Autowired;
  22 +import org.springframework.stereotype.Service;
  23 +
  24 +import java.util.*;
  25 +import java.util.concurrent.TimeUnit;
  26 +import java.util.stream.Collectors;
  27 +
  28 +@Service
  29 +public class CsSearchResourceFuzzyIndexBaseService {
  30 +
  31 + private final Logger logger = LoggerFactory.getLogger(this.getClass());
  32 +
  33 + @Autowired
  34 + private SearchCommonService searchCommonService;
  35 +
  36 + //Guava Cache
  37 + private LoadingCache<String, List<Map<String, Object>>> fuzzySearchResourceCache = CacheBuilder.newBuilder()
  38 + .maximumSize(100).expireAfterWrite(3, TimeUnit.MINUTES).build(new CacheLoader<String, List<Map<String, Object>>>() {
  39 + public List<Map<String, Object>> load(String key) {
  40 + return queryValidFuzzySearchResourceFromEs(1000);
  41 + }
  42 + });
  43 +
  44 + private List<Map<String, Object>> queryValidFuzzySearchResourceFromEs(int limit) {
  45 + try {
  46 + SearchParam searchParam = new SearchParam();
  47 +
  48 + //1、设置filter
  49 + BoolQueryBuilder filter = QueryBuilders.boolQuery();
  50 + long current = DateUtil.getCurrentTimeSecond();
  51 + filter.must(QueryBuilders.rangeQuery("beginTime").lte(current));
  52 + filter.must(QueryBuilders.rangeQuery("endTime").gte(current));
  53 + searchParam.setFiter(filter);
  54 +
  55 + //2、设置分页参数
  56 + searchParam.setOffset(0);
  57 + searchParam.setSize(limit);
  58 +
  59 + //3、设置排序字段
  60 + List<SortBuilder<?>> sortBuilder = new ArrayList<>();
  61 + sortBuilder.add(SortBuilders.fieldSort("orderBy").order(SortOrder.DESC));
  62 + sortBuilder.add(SortBuilders.fieldSort("id").order(SortOrder.DESC));
  63 + searchParam.setSortBuilders(sortBuilder);
  64 +
  65 + //4、执行搜索
  66 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_CS_SEARCH_RESOURCE_FUZZY, searchParam);
  67 +
  68 + //5、构造返回结果
  69 + List<Map<String, Object>> results = searchResult.getResultList();
  70 + if (CollectionUtils.isEmpty(results)) {
  71 + return new ArrayList<>();
  72 + }
  73 + List<Map<String, Object>> resultList = new ArrayList<>();
  74 + for (Map<String, Object> esMap : results) {
  75 + resultList.add(this.getResourceMap(esMap));
  76 + }
  77 + return resultList;
  78 + } catch (Exception e) {
  79 + logger.error(e.getMessage());
  80 + return new ArrayList<>();
  81 + }
  82 + }
  83 +
  84 + private Map<String, Object> getResourceMap(Map<String, Object> esMap) {
  85 + Map<String, Object> map = new HashMap<>();
  86 + map.put("id", MapUtils.getIntValue(esMap, "id"));
  87 + map.put("keyword", MapUtils.getString(esMap, "keyword",""));
  88 + map.put("link_url", MapUtils.getString(esMap, "linkUrl", ""));
  89 + String image = ImageUrlAssist.getAllProductPicUrl(MapUtils.getString(esMap, "image", ""), "goodsimg", "center", "d2hpdGU=");
  90 + map.put("image", image);
  91 + return map;
  92 + }
  93 +
  94 + public List<Map<String, Object>> queryFuzzySearchResourcesByKeyWord(String keyword){
  95 + try {
  96 + List<Map<String, Object>> cacheResult = fuzzySearchResourceCache.get("DEFAULT");
  97 + return cacheResult.stream().filter(resource->{
  98 + if(MapUtils.getString(resource,"keyword").equalsIgnoreCase(keyword)){
  99 + return true;
  100 + }else{
  101 + return false;
  102 + }
  103 + }).collect(Collectors.toList());
  104 + } catch (Exception e) {
  105 + logger.error(e.getMessage(), e);
  106 + return new ArrayList<>(0);
  107 + }
  108 + }
  109 +
  110 +}
@@ -4,8 +4,11 @@ import com.alibaba.fastjson.JSONObject; @@ -4,8 +4,11 @@ import com.alibaba.fastjson.JSONObject;
4 import com.yoho.search.base.utils.ConvertUtils; 4 import com.yoho.search.base.utils.ConvertUtils;
5 import com.yoho.search.base.utils.SearchCollectionUtils; 5 import com.yoho.search.base.utils.SearchCollectionUtils;
6 import com.yoho.search.common.SearchDynamicConfigService; 6 import com.yoho.search.common.SearchDynamicConfigService;
  7 +import com.yoho.search.common.SearchRequestParams;
7 import com.yoho.search.common.utils.ABUserPartitionUtils; 8 import com.yoho.search.common.utils.ABUserPartitionUtils;
  9 +import com.yoho.search.core.es.model.SearchParam;
8 import com.yoho.search.service.helper.SearchCommonHelper; 10 import com.yoho.search.service.helper.SearchCommonHelper;
  11 +import com.yoho.search.service.index.CsSearchResourceFuzzyIndexBaseService;
9 import com.yoho.search.service.index.CsSearchResourceIndexBaseService; 12 import com.yoho.search.service.index.CsSearchResourceIndexBaseService;
10 import org.apache.commons.collections.MapUtils; 13 import org.apache.commons.collections.MapUtils;
11 import org.slf4j.Logger; 14 import org.slf4j.Logger;
@@ -15,7 +18,6 @@ import org.springframework.stereotype.Component; @@ -15,7 +18,6 @@ import org.springframework.stereotype.Component;
15 import org.springframework.util.CollectionUtils; 18 import org.springframework.util.CollectionUtils;
16 19
17 import java.util.ArrayList; 20 import java.util.ArrayList;
18 -import java.util.Collections;  
19 import java.util.List; 21 import java.util.List;
20 import java.util.Map; 22 import java.util.Map;
21 import java.util.stream.Collectors; 23 import java.util.stream.Collectors;
@@ -28,6 +30,9 @@ public class CsSearchResourceService { @@ -28,6 +30,9 @@ public class CsSearchResourceService {
28 @Autowired 30 @Autowired
29 private CsSearchResourceIndexBaseService csSearchResourceIndexBaseService; 31 private CsSearchResourceIndexBaseService csSearchResourceIndexBaseService;
30 @Autowired 32 @Autowired
  33 + private CsSearchResourceFuzzyIndexBaseService searchResourceFuzzyIndexBaseService;
  34 +
  35 + @Autowired
31 private SearchCommonHelper searchCommonHelper; 36 private SearchCommonHelper searchCommonHelper;
32 @Autowired 37 @Autowired
33 private ABUserPartitionUtils abUserPartitionUtils; 38 private ABUserPartitionUtils abUserPartitionUtils;
@@ -119,8 +124,8 @@ public class CsSearchResourceService { @@ -119,8 +124,8 @@ public class CsSearchResourceService {
119 return new ArrayList<>(); 124 return new ArrayList<>();
120 } 125 }
121 int[] index = new int[countPerSize]; 126 int[] index = new int[countPerSize];
122 - index[0] = viewNum / 4;  
123 - index[1] = viewNum * 3 / 4; 127 + index[0] = viewNum * 1/4;
  128 + index[1] = viewNum * 3/4;
124 for (int i = 0; i < subResourceList.size(); i++) { 129 for (int i = 0; i < subResourceList.size(); i++) {
125 subResourceList.get(i).put("index", index[i]); 130 subResourceList.get(i).put("index", index[i]);
126 } 131 }
@@ -129,4 +134,10 @@ public class CsSearchResourceService { @@ -129,4 +134,10 @@ public class CsSearchResourceService {
129 return subResourceList; 134 return subResourceList;
130 } 135 }
131 136
  137 + //获取全部的资源位直通车
  138 + public List<Map<String, Object>> queryAllFuzzySearchResource(Map<String, String> paramMap) {
  139 + String keyword = MapUtils.getString(paramMap, SearchRequestParams.PARAM_SEARCH_QUERY,"");
  140 + return searchResourceFuzzyIndexBaseService.queryFuzzySearchResourcesByKeyWord(keyword);
  141 + }
  142 +
132 } 143 }
@@ -2,6 +2,7 @@ package com.yoho.search.service.scene.pages; @@ -2,6 +2,7 @@ package com.yoho.search.service.scene.pages;
2 2
3 import com.alibaba.fastjson.JSONArray; 3 import com.alibaba.fastjson.JSONArray;
4 import com.alibaba.fastjson.JSONObject; 4 import com.alibaba.fastjson.JSONObject;
  5 +import com.yoho.search.base.utils.JsonUtil;
5 import com.yoho.search.base.utils.SearchCollectionUtils; 6 import com.yoho.search.base.utils.SearchCollectionUtils;
6 import com.yoho.search.base.utils.SearchPageIdDefine; 7 import com.yoho.search.base.utils.SearchPageIdDefine;
7 import com.yoho.search.common.SearchDynamicConfigService; 8 import com.yoho.search.common.SearchDynamicConfigService;
@@ -10,11 +11,12 @@ import com.yoho.search.common.utils.SearchApiResultUtils; @@ -10,11 +11,12 @@ import com.yoho.search.common.utils.SearchApiResultUtils;
10 import com.yoho.search.models.SearchApiResult; 11 import com.yoho.search.models.SearchApiResult;
11 import com.yoho.search.service.helper.SearchCommonHelper; 12 import com.yoho.search.service.helper.SearchCommonHelper;
12 import com.yoho.search.service.helper.SearchKeyWordHelper; 13 import com.yoho.search.service.helper.SearchKeyWordHelper;
  14 +import com.yoho.search.service.scene.general.CsSearchResourceService;
13 import com.yoho.search.service.scene.pages.entrance.ProductListSwitchService; 15 import com.yoho.search.service.scene.pages.entrance.ProductListSwitchService;
14 import com.yoho.search.service.scene.pages.selections.PageAggregationHelper; 16 import com.yoho.search.service.scene.pages.selections.PageAggregationHelper;
15 import com.yoho.search.service.scene.pages.selections.PageSelectionsBrandsService; 17 import com.yoho.search.service.scene.pages.selections.PageSelectionsBrandsService;
16 import com.yoho.search.service.scene.pages.selections.PageSelectionsService; 18 import com.yoho.search.service.scene.pages.selections.PageSelectionsService;
17 -import com.yoho.search.service.scene.shopbrand.ShopListService; 19 +import com.yoho.search.service.scene.shopbrand.ShopListFuzzyService;
18 import com.yoho.search.service.scene.suggest.RecommendWordsService; 20 import com.yoho.search.service.scene.suggest.RecommendWordsService;
19 import org.apache.commons.collections.MapUtils; 21 import org.apache.commons.collections.MapUtils;
20 import org.apache.commons.lang.StringUtils; 22 import org.apache.commons.lang.StringUtils;
@@ -22,9 +24,11 @@ import org.slf4j.Logger; @@ -22,9 +24,11 @@ import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory; 24 import org.slf4j.LoggerFactory;
23 import org.springframework.beans.factory.annotation.Autowired; 25 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.stereotype.Service; 26 import org.springframework.stereotype.Service;
25 -import org.springframework.util.CollectionUtils;  
26 27
27 -import java.util.*; 28 +import java.util.ArrayList;
  29 +import java.util.Collections;
  30 +import java.util.List;
  31 +import java.util.Map;
28 import java.util.concurrent.CompletableFuture; 32 import java.util.concurrent.CompletableFuture;
29 import java.util.concurrent.ExecutorService; 33 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors; 34 import java.util.concurrent.Executors;
@@ -52,7 +56,10 @@ public class FuzzySceneService extends AbstractPageSceneService { @@ -52,7 +56,10 @@ public class FuzzySceneService extends AbstractPageSceneService {
52 @Autowired 56 @Autowired
53 private ProductListSwitchService productListSwitchService; 57 private ProductListSwitchService productListSwitchService;
54 @Autowired 58 @Autowired
55 - private ShopListService shopListService; 59 + private ShopListFuzzyService shopListFuzzyService;
  60 + @Autowired
  61 + private CsSearchResourceService csSearchResourceService;
  62 +
56 63
57 private ExecutorService executor = Executors.newFixedThreadPool(20); 64 private ExecutorService executor = Executors.newFixedThreadPool(20);
58 65
@@ -82,6 +89,7 @@ public class FuzzySceneService extends AbstractPageSceneService { @@ -82,6 +89,7 @@ public class FuzzySceneService extends AbstractPageSceneService {
82 if (StringUtils.isBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY))) { 89 if (StringUtils.isBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY))) {
83 return new SearchApiResult().setCode(400).setMessage("请传query参数"); 90 return new SearchApiResult().setCode(400).setMessage("请传query参数");
84 } 91 }
  92 +
85 // 2、添加默认参数 93 // 2、添加默认参数
86 this.addParamsToParamMap(paramMap); 94 this.addParamsToParamMap(paramMap);
87 95
@@ -94,8 +102,8 @@ public class FuzzySceneService extends AbstractPageSceneService { @@ -94,8 +102,8 @@ public class FuzzySceneService extends AbstractPageSceneService {
94 // 5、获取促销专题 102 // 5、获取促销专题
95 //CompletableFuture<SearchApiResult> promotionsFuture = CompletableFuture.supplyAsync(() -> pageAggregationHelper.sceneAggPromotion(this.newParamMap(paramMap)), executor); 103 //CompletableFuture<SearchApiResult> promotionsFuture = CompletableFuture.supplyAsync(() -> pageAggregationHelper.sceneAggPromotion(this.newParamMap(paramMap)), executor);
96 104
97 - // 6、获取店铺结果  
98 - CompletableFuture<SearchApiResult> shopsFuture = CompletableFuture.supplyAsync(() -> shopListService.searchShopList(convertParamMap(paramMap)), executor); 105 + // 6、获取店铺查询结果
  106 + CompletableFuture<JSONObject> shopsFuture = CompletableFuture.supplyAsync(() -> shopListFuzzyService.queryShopListForFuzzyScene(this.newParamMap(paramMap)), executor);
99 107
100 // 7、加入建议词 108 // 7、加入建议词
101 SearchApiResult productListResult = productListuture.get(); 109 SearchApiResult productListResult = productListuture.get();
@@ -116,129 +124,87 @@ public class FuzzySceneService extends AbstractPageSceneService { @@ -116,129 +124,87 @@ public class FuzzySceneService extends AbstractPageSceneService {
116 //SearchApiResult promotions = promotionsFuture.get(); 124 //SearchApiResult promotions = promotionsFuture.get();
117 //dataMap.put(RECOMMEND_PROMOTION_LIST, pageAggregationHelper.subRecommendPromotions(promotions.getData(), this.getPage(paramMap), 1)); 125 //dataMap.put(RECOMMEND_PROMOTION_LIST, pageAggregationHelper.subRecommendPromotions(promotions.getData(), this.getPage(paramMap), 1));
118 126
119 - // 11、处理店铺结果  
120 - SearchApiResult shops = shopsFuture.get();  
121 - dealShopListResult(shops, dataMap, paramMap); 127 + // 11、获取全部的资源位直通车
  128 + List<Map<String, Object>> allFuzzySearchResource = csSearchResourceService.queryAllFuzzySearchResource(paramMap);
122 129
123 - // 12、返回最终结果 130 + // 12、处理资源位和店铺分页
  131 + dealPageData(paramMap, allFuzzySearchResource, shopsFuture, dataMap);
  132 +
  133 + // 13、返回最终结果
124 return productListResult; 134 return productListResult;
125 } catch (Exception e) { 135 } catch (Exception e) {
126 return SearchApiResultUtils.errorSearchApiResult(logger, paramMap, e); 136 return SearchApiResultUtils.errorSearchApiResult(logger, paramMap, e);
127 } 137 }
128 } 138 }
129 139
130 - private Map<String, String> convertParamMap(Map<String, String> paramMap) {  
131 - String query = paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY);  
132 - Map<String, String> newMap = new HashMap<>(paramMap);  
133 - newMap.put(SearchRequestParams.PARAM_SEARCH_SHOPS_KEYWORD, query);  
134 - return newMap;  
135 - }  
136 -  
137 - /**  
138 - * 处理shopList节点  
139 - *  
140 - * @param shops  
141 - * @param dataMap  
142 - * @param paramMap  
143 - */  
144 - private void dealShopListResult(SearchApiResult shops, JSONObject dataMap, Map<String, String> paramMap) {  
145 - // 1、参数检测  
146 - dataMap.put("shop_list", Collections.emptyList());  
147 - dataMap.put("shop_list_page", Collections.emptyList());  
148 - if (shops == null) {  
149 - return;  
150 - }  
151 - JSONObject jsonObject = (JSONObject) shops.getData();  
152 - if (jsonObject == null) {  
153 - return;  
154 - }  
155 - JSONArray jsonArray = jsonObject.getJSONArray("shop_list");  
156 - if (CollectionUtils.isEmpty(jsonArray)) {  
157 - return;  
158 - }  
159 -  
160 - // 2、第一个数据或ufo数据放shop_list节点,放在前面  
161 - List<JSONObject> tempFirstShopList = new ArrayList<>();  
162 - List<JSONObject> tempOtherShopList = new ArrayList<>();  
163 - for (int i = 0; i < jsonArray.size(); i++) {  
164 - JSONObject subJsonObject = jsonArray.getJSONObject(i);  
165 - if (i == 0 || MapUtils.getObject(subJsonObject, "ufo_brand") != null) {  
166 - tempFirstShopList.add(subJsonObject);  
167 - } else {  
168 - tempOtherShopList.add(subJsonObject);  
169 - }  
170 - }  
171 -  
172 - // 3、处理realFirstShops: 将tempFirstShopList中有search_show_image的节点放前面,如果都有,则顺序不变  
173 - List<JSONObject> realFirstShops = new ArrayList<>();  
174 - Iterator<JSONObject> iterator = tempFirstShopList.iterator();  
175 - while (iterator.hasNext()) {  
176 - JSONObject shopInfo = iterator.next();  
177 - JSONObject tbl_brand = shopInfo.getJSONObject("tbl_brand");  
178 - JSONObject ufo_brand = shopInfo.getJSONObject("ufo_brand");  
179 - JSONObject yoho_shop = shopInfo.getJSONObject("yoho_shop");  
180 - if (checkSearchShowImage(tbl_brand) || checkSearchShowImage(ufo_brand) || checkSearchShowImage(yoho_shop)) {  
181 - realFirstShops.add(shopInfo);  
182 - iterator.remove(); 140 + // 12、处理资源位和店铺分页
  141 + private void dealPageData(Map<String, String> paramMap, List<Map<String, Object>> allFuzzySearchResource, CompletableFuture<JSONObject> shopsFuture, JSONObject dataMap) {
  142 + try {
  143 + // 1、源数据获取
  144 + JSONObject shopsResult = shopsFuture.get();
  145 + JSONArray shop_list_page = shopsResult.getJSONArray("shop_list_page");
  146 + JSONArray search_resource_list = JsonUtil.listToJsonArray(allFuzzySearchResource);
  147 +
  148 + // 2、数据整合,优先资源位数据,再店铺数据
  149 + List<JSONObject> dataList = new ArrayList<>();
  150 + for (int i = 0; i < search_resource_list.size(); i++) {
  151 + JSONObject resource = search_resource_list.getJSONObject(i);
  152 + resource.put("dataJoinType", "resource");
  153 + dataList.add(resource);
183 } 154 }
184 - }  
185 -  
186 - realFirstShops.addAll(tempFirstShopList);  
187 - // 4、处理realFirstShops: 将realFirstShops中,除了第一个节点的search_show_image的节点,全部清除  
188 - for(int i=0;i<realFirstShops.size();i++){  
189 - if(i==0){  
190 - continue; 155 + for (int i = 0; i < shop_list_page.size(); i++) {
  156 + JSONObject shop = shop_list_page.getJSONObject(i);
  157 + shop.put("dataJoinType", "shop");
  158 + dataList.add(shop);
191 } 159 }
192 - JSONObject shopInfo = realFirstShops.get(i);  
193 - JSONObject tbl_brand = shopInfo.getJSONObject("tbl_brand");  
194 - JSONObject ufo_brand = shopInfo.getJSONObject("ufo_brand");  
195 - JSONObject yoho_shop = shopInfo.getJSONObject("yoho_shop");  
196 - cleanSearchShowImage(tbl_brand);  
197 - cleanSearchShowImage(ufo_brand);  
198 - cleanSearchShowImage(yoho_shop);  
199 - }  
200 - dataMap.put("shop_list", realFirstShops);  
201 160
202 - // 5、处理分页节点,一页放两个  
203 - if (CollectionUtils.isEmpty(tempOtherShopList)) {  
204 - return;  
205 - }  
206 - int page = MapUtils.getIntValue(paramMap,"page",1);  
207 - int pageSize = 2;  
208 - List<JSONObject> subShopList = SearchCollectionUtils.safeSubList(tempOtherShopList, (page - 1) * pageSize, page * pageSize);  
209 - if (CollectionUtils.isEmpty(subShopList)) {  
210 - return;  
211 - }  
212 - int viewNum = MapUtils.getIntValue(paramMap,"viewNum",10);  
213 - if (viewNum > 100) {  
214 - viewNum = 100;  
215 - }  
216 - int[] index = new int[pageSize];  
217 - index[0] = viewNum / 4;  
218 - index[1] = viewNum * 3 / 4;  
219 - for (int i = 0; i < subShopList.size(); i++) {  
220 - subShopList.get(i).put("index", index[i]);  
221 - }  
222 - List productList = (List) dataMap.get("product_list");  
223 - int productListSize = productList == null ? 0 : productList.size();  
224 - subShopList = subShopList.stream().filter(e -> e.getIntValue("index") < productListSize).collect(Collectors.toList());  
225 - dataMap.put("shop_list_page", subShopList);  
226 - } 161 + // 3、得到分页数据
  162 + int countPerSize = 2;
  163 + int page = MapUtils.getIntValue(paramMap, "page", 1);
  164 + List<JSONObject> pageDataList = SearchCollectionUtils.safeSubList(dataList, (page - 1) * countPerSize, page * countPerSize);
227 165
228 - private boolean checkSearchShowImage(JSONObject data) {  
229 - if (data != null && StringUtils.isNotBlank(MapUtils.getString(data, "search_show_image", ""))) {  
230 - return true;  
231 - }  
232 - return false;  
233 - } 166 + // 4、处理数据索引
  167 + int viewNum = MapUtils.getIntValue(paramMap, "viewNum", 20);
  168 + if (viewNum > 100) {
  169 + viewNum = 100;
  170 + }
  171 + int[] index = new int[countPerSize];
  172 + index[0] = viewNum * 1 / 4;
  173 + index[1] = viewNum * 3 / 4;
  174 + for (int i = 0; i < pageDataList.size(); i++) {
  175 + pageDataList.get(i).put("index", index[i]);
  176 + }
  177 + List productList = (List) dataMap.get("product_list");
  178 + int productListSize = productList == null ? 0 : productList.size();
  179 + pageDataList = pageDataList.stream().filter(e -> e.getIntValue("index") < productListSize).collect(Collectors.toList());
  180 +
  181 + //5、处理结果
  182 + List<JSONObject> search_resource_list1 = new ArrayList<>();
  183 + List<JSONObject> shop_list_page1 = new ArrayList<>();
  184 + pageDataList.forEach(data -> {
  185 + String dataJoinType = MapUtils.getString(data, "dataJoinType", "");
  186 + if (dataJoinType.equalsIgnoreCase("resource")) {
  187 + search_resource_list1.add(data);
  188 + }
  189 + if (dataJoinType.equalsIgnoreCase("shop")) {
  190 + shop_list_page1.add(data);
  191 + }
  192 + data.remove("dataJoinType");
  193 + });
  194 +
  195 + //6、加入返回结果
  196 + dataMap.put("shop_list", shopsResult.getJSONArray("shop_list"));
  197 + dataMap.put("shop_list_page", shop_list_page1);
  198 + dataMap.put("search_resource_list", search_resource_list1);
234 199
235 - private void cleanSearchShowImage(JSONObject data) {  
236 - if (data != null) {  
237 - data.put("search_show_image",""); 200 + } catch (Exception e) {
  201 + logger.error(e.getMessage(), e);
  202 + dataMap.put("shop_list", Collections.emptyList());
  203 + dataMap.put("shop_list_page", Collections.emptyList());
  204 + dataMap.put("search_resource_list", Collections.emptyList());
238 } 205 }
239 } 206 }
240 207
241 -  
242 @Override 208 @Override
243 public SearchApiResult aggregations(Map<String, String> paramMap) { 209 public SearchApiResult aggregations(Map<String, String> paramMap) {
244 try { 210 try {
  1 +package com.yoho.search.service.scene.shopbrand;
  2 +
  3 +import com.alibaba.fastjson.JSONArray;
  4 +import com.alibaba.fastjson.JSONObject;
  5 +import com.yoho.search.common.SearchRequestParams;
  6 +import com.yoho.search.models.SearchApiResult;
  7 +import org.apache.commons.collections.MapUtils;
  8 +import org.apache.commons.lang.StringUtils;
  9 +import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.stereotype.Service;
  11 +import org.springframework.util.CollectionUtils;
  12 +
  13 +import java.util.*;
  14 +
  15 +@Service
  16 +public class ShopListFuzzyService {
  17 +
  18 + @Autowired
  19 + private ShopListService shopListService;
  20 +
  21 + /**
  22 + * 模糊搜索列表查询店铺信息,并处理好头结点
  23 + *
  24 + * @param paramMap
  25 + * @return
  26 + */
  27 + public JSONObject queryShopListForFuzzyScene(Map<String, String> paramMap) {
  28 +
  29 + //1、数据转换,调shopListService接口查询
  30 + String query = MapUtils.getString(paramMap, SearchRequestParams.PARAM_SEARCH_QUERY, "");
  31 + paramMap.put(SearchRequestParams.PARAM_SEARCH_SHOPS_KEYWORD, query);
  32 + SearchApiResult shops = shopListService.searchShopList(paramMap);
  33 +
  34 + //2、构造默认的返回结果
  35 + JSONObject result = new JSONObject();
  36 + result.put("shop_list", Collections.emptyList());
  37 + result.put("shop_list_page", Collections.emptyList());
  38 +
  39 + //3、参数检测
  40 + if (shops == null) {
  41 + return result;
  42 + }
  43 + JSONObject jsonObject = (JSONObject) shops.getData();
  44 + if (jsonObject == null) {
  45 + return result;
  46 + }
  47 + JSONArray jsonArray = jsonObject.getJSONArray("shop_list");
  48 + if (CollectionUtils.isEmpty(jsonArray)) {
  49 + return result;
  50 + }
  51 +
  52 + // 4、第一个数据或ufo数据放shop_list节点,即放在前面
  53 + List<JSONObject> topShopListTemp = new ArrayList<>();
  54 + List<JSONObject> pageShopList = new ArrayList<>();
  55 + for (int i = 0; i < jsonArray.size(); i++) {
  56 + JSONObject subJsonObject = jsonArray.getJSONObject(i);
  57 + if (i == 0 || MapUtils.getObject(subJsonObject, "ufo_brand") != null) {
  58 + topShopListTemp.add(subJsonObject);
  59 + } else {
  60 + pageShopList.add(subJsonObject);
  61 + }
  62 + }
  63 +
  64 + // 5、处理realFirstShops: 将tempFirstShopList中有search_show_image的节点放前面,如果都有,则顺序不变
  65 + List<JSONObject> topShopList = new ArrayList<>();
  66 + Iterator<JSONObject> iterator = topShopListTemp.iterator();
  67 + while (iterator.hasNext()) {
  68 + JSONObject shopInfo = iterator.next();
  69 + JSONObject tbl_brand = shopInfo.getJSONObject("tbl_brand");
  70 + JSONObject ufo_brand = shopInfo.getJSONObject("ufo_brand");
  71 + JSONObject yoho_shop = shopInfo.getJSONObject("yoho_shop");
  72 + if (checkSearchShowImage(tbl_brand) || checkSearchShowImage(ufo_brand) || checkSearchShowImage(yoho_shop)) {
  73 + topShopList.add(shopInfo);
  74 + iterator.remove();
  75 + }
  76 + }
  77 + topShopList.addAll(topShopListTemp);
  78 +
  79 + // 6、处理topShopList: 将topShopList中,除了第一个节点的search_show_image的节点,全部清除
  80 + for (int i = 0; i < topShopList.size(); i++) {
  81 + if (i == 0) {
  82 + continue;
  83 + }
  84 + JSONObject shopInfo = topShopList.get(i);
  85 + JSONObject tbl_brand = shopInfo.getJSONObject("tbl_brand");
  86 + JSONObject ufo_brand = shopInfo.getJSONObject("ufo_brand");
  87 + JSONObject yoho_shop = shopInfo.getJSONObject("yoho_shop");
  88 + cleanSearchShowImage(tbl_brand);
  89 + cleanSearchShowImage(ufo_brand);
  90 + cleanSearchShowImage(yoho_shop);
  91 + }
  92 +
  93 + // 7、返回结果
  94 + result.put("shop_list", topShopList);
  95 + result.put("shop_list_page", pageShopList);
  96 + return result;
  97 + }
  98 +
  99 + private boolean checkSearchShowImage(JSONObject data) {
  100 + if (data != null && StringUtils.isNotBlank(MapUtils.getString(data, "search_show_image", ""))) {
  101 + return true;
  102 + }
  103 + return false;
  104 + }
  105 +
  106 + private void cleanSearchShowImage(JSONObject data) {
  107 + if (data != null) {
  108 + data.put("search_show_image", "");
  109 + }
  110 + }
  111 +
  112 +}