Authored by 胡古飞

找相似功能最多返回300个SKN

1 package com.yoho.search.service.servicenew.impl; 1 package com.yoho.search.service.servicenew.impl;
2 2
  3 +import java.util.ArrayList;
3 import java.util.List; 4 import java.util.List;
4 import java.util.Map; 5 import java.util.Map;
5 6
@@ -9,6 +10,9 @@ import org.elasticsearch.index.query.MultiMatchQueryBuilder; @@ -9,6 +10,9 @@ import org.elasticsearch.index.query.MultiMatchQueryBuilder;
9 import org.elasticsearch.index.query.QueryBuilders; 10 import org.elasticsearch.index.query.QueryBuilders;
10 import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; 11 import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
11 import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders; 12 import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
  13 +import org.elasticsearch.search.sort.SortBuilder;
  14 +import org.elasticsearch.search.sort.SortBuilders;
  15 +import org.elasticsearch.search.sort.SortOrder;
12 import org.springframework.beans.factory.annotation.Autowired; 16 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.stereotype.Service; 17 import org.springframework.stereotype.Service;
14 18
@@ -59,7 +63,7 @@ public class SearchLikeServiceImpl implements ISearchLikeService { @@ -59,7 +63,7 @@ public class SearchLikeServiceImpl implements ISearchLikeService {
59 return new SearchApiResult().setCode(400).setMessage("分页参数不合法"); 63 return new SearchApiResult().setCode(400).setMessage("分页参数不合法");
60 } 64 }
61 if (pageSize > 50) { 65 if (pageSize > 50) {
62 - pageSize = 100; 66 + pageSize = 50;
63 } 67 }
64 // 用来做加分 68 // 用来做加分
65 int brand_id = (int) productInfo.get("brand_id"); 69 int brand_id = (int) productInfo.get("brand_id");
@@ -114,8 +118,10 @@ public class SearchLikeServiceImpl implements ISearchLikeService { @@ -114,8 +118,10 @@ public class SearchLikeServiceImpl implements ISearchLikeService {
114 boolFilter.mustNot(QueryBuilders.termsQuery("isGlobal", "Y")); 118 boolFilter.mustNot(QueryBuilders.termsQuery("isGlobal", "Y"));
115 searchParam.setFiter(boolFilter); 119 searchParam.setFiter(boolFilter);
116 120
117 - // 4、设置排序规则  
118 - searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap)); 121 + // 4、设置排序规则[支持持按打分排序]
  122 + List<SortBuilder>sortBuilders = new ArrayList<SortBuilder>();
  123 + sortBuilders.add(SortBuilders.fieldSort("_score").order(SortOrder.DESC));
  124 + searchParam.setSortBuilders(sortBuilders);
119 125
120 // 5、设置分页 126 // 5、设置分页
121 searchParam.setPage(page); 127 searchParam.setPage(page);
@@ -133,14 +139,31 @@ public class SearchLikeServiceImpl implements ISearchLikeService { @@ -133,14 +139,31 @@ public class SearchLikeServiceImpl implements ISearchLikeService {
133 SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam); 139 SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
134 SearchApiResult searchApiResult = new SearchApiResult(); 140 SearchApiResult searchApiResult = new SearchApiResult();
135 dataMap = new JSONObject(); 141 dataMap = new JSONObject();
136 - dataMap.put("total", searchResult.getTotal());  
137 - dataMap.put("page", searchResult.getPage());  
138 - dataMap.put("page_size", searchParam.getSize());  
139 - dataMap.put("page_total", searchResult.getTotalPage()); 142 +
  143 + long total = searchResult.getTotal();
  144 + if(total>300){
  145 + total = 300;
  146 + }
  147 + dataMap.put("total", total);
  148 + dataMap.put("page", page);
  149 + dataMap.put("page_size", pageSize);
  150 + dataMap.put("page_total", this.getPageTotal(total, pageSize));
  151 + dataMap.put("product_info", productInfo);
140 dataMap.put("product_list", searchServiceHelper.getProductMapList(searchResult.getResultList())); 152 dataMap.put("product_list", searchServiceHelper.getProductMapList(searchResult.getResultList()));
141 searchCacheService.addJSONObjectToCache(cacheEnum, productIndexName, searchParam, dataMap); 153 searchCacheService.addJSONObjectToCache(cacheEnum, productIndexName, searchParam, dataMap);
142 return searchApiResult.setData(dataMap); 154 return searchApiResult.setData(dataMap);
143 } 155 }
  156 +
  157 + private long getPageTotal(long total,int pageSize){
  158 + if(pageSize<=0){
  159 + return total;
  160 + }
  161 + if(total % pageSize==0){
  162 + return total / pageSize;
  163 + }else{
  164 + return total / pageSize + 1;
  165 + }
  166 + }
144 167
145 private JSONObject getProductSknInfo(String productSkn) { 168 private JSONObject getProductSknInfo(String productSkn) {
146 SearchParam searchParam = new SearchParam(); 169 SearchParam searchParam = new SearchParam();