Authored by wangnan9279

图库fix

1 package com.yoho.search.common; 1 package com.yoho.search.common;
2 2
3 -import java.util.Map;  
4 - 3 +import com.yoho.search.base.utils.ConvertUtils;
5 import org.apache.commons.lang.StringUtils; 4 import org.apache.commons.lang.StringUtils;
6 import org.elasticsearch.index.query.BoolQueryBuilder; 5 import org.elasticsearch.index.query.BoolQueryBuilder;
7 import org.elasticsearch.index.query.QueryBuilders; 6 import org.elasticsearch.index.query.QueryBuilders;
8 -import org.springframework.beans.factory.annotation.Autowired;  
9 7
10 -import com.yoho.search.base.utils.ConvertUtils;  
11 -import com.yoho.search.service.helper.SearchParamHelper;  
12 -import com.yoho.search.service.helper.SearchSortHelper; 8 +import java.util.List;
  9 +import java.util.Map;
13 10
14 /** 11 /**
15 * Created by wangnan on 2016/12/16. 12 * Created by wangnan on 2016/12/16.
16 */ 13 */
17 public class BaseService { 14 public class BaseService {
18 15
19 - /**  
20 - * 增加一个termsQuery  
21 - */  
22 - protected BoolQueryBuilder boolQueryTermsQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {  
23 - if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) {  
24 - int[] values = ConvertUtils.stringToIntArray(paramMap.get(fieldName), ",");  
25 - boolFilter.must(QueryBuilders.termsQuery(fieldName, values));  
26 - }  
27 - return boolFilter;  
28 - } 16 + /**
  17 + * 增加一个termsQuery
  18 + */
  19 + protected BoolQueryBuilder boolQueryTermsQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {
  20 + if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) {
  21 + List<Integer> values = ConvertUtils.stringToIntList(paramMap.get(fieldName), ",");
  22 + if (values == null || values.isEmpty()) {
  23 + return boolFilter;
  24 + }
  25 + boolFilter.must(QueryBuilders.termsQuery(fieldName, values));
  26 + }
  27 + return boolFilter;
  28 + }
29 29
30 - /**  
31 - * 增加一个termQuery  
32 - */  
33 - protected BoolQueryBuilder boolQueryTermQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {  
34 - if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) {  
35 - boolFilter.must(QueryBuilders.termQuery(fieldName, paramMap.get(fieldName)));  
36 - }  
37 - return boolFilter;  
38 - } 30 + /**
  31 + * 增加一个termQuery
  32 + */
  33 + protected BoolQueryBuilder boolQueryTermQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {
  34 + if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) {
  35 + boolFilter.must(QueryBuilders.termQuery(fieldName, paramMap.get(fieldName)));
  36 + }
  37 + return boolFilter;
  38 + }
39 39
40 - /**  
41 - * 通用的过滤请求  
42 - */  
43 - protected BoolQueryBuilder generalHandler(Map<String, String> paramMap, BoolQueryBuilder boolFilter) {  
44 - for (String key : paramMap.keySet()) {  
45 - if (key.startsWith("not_")) {  
46 - String[] values = paramMap.get(key).split(",");  
47 - boolFilter.mustNot(QueryBuilders.termsQuery(key.substring(4, key.length()), values));  
48 - }  
49 - if (key.startsWith("filter_")) {  
50 - String field = key.substring(7, key.length());  
51 - boolFilter.must(QueryBuilders.termsQuery(field, paramMap.get(key).split(",")));  
52 - }  
53 - if (key.startsWith("missing_")) {  
54 - String field = key.substring(8, key.length());  
55 - boolFilter.mustNot(QueryBuilders.existsQuery(field));  
56 - }  
57 - }  
58 - if (boolFilter.hasClauses()) {  
59 - return boolFilter;  
60 - } else {  
61 - return null;  
62 - }  
63 - } 40 + /**
  41 + * 通用的过滤请求
  42 + */
  43 + protected BoolQueryBuilder generalHandler(Map<String, String> paramMap, BoolQueryBuilder boolFilter) {
  44 + for (String key : paramMap.keySet()) {
  45 + if (key.startsWith("not_")) {
  46 + String[] values = paramMap.get(key).split(",");
  47 + boolFilter.mustNot(QueryBuilders.termsQuery(key.substring(4, key.length()), values));
  48 + }
  49 + if (key.startsWith("filter_")) {
  50 + String field = key.substring(7, key.length());
  51 + boolFilter.must(QueryBuilders.termsQuery(field, paramMap.get(key).split(",")));
  52 + }
  53 + if (key.startsWith("missing_")) {
  54 + String field = key.substring(8, key.length());
  55 + boolFilter.mustNot(QueryBuilders.existsQuery(field));
  56 + }
  57 + }
  58 + if (boolFilter.hasClauses()) {
  59 + return boolFilter;
  60 + } else {
  61 + return null;
  62 + }
  63 + }
64 64
65 - protected int getPage(Map<String, String> paramMap) {  
66 - int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));  
67 - return page;  
68 - } 65 + protected int getPage(Map<String, String> paramMap) {
  66 + int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
  67 + return page;
  68 + }
69 } 69 }
@@ -29,7 +29,7 @@ import java.util.Map; @@ -29,7 +29,7 @@ import java.util.Map;
29 29
30 /** 30 /**
31 * 图库服务 31 * 图库服务
32 - * @Author: wangnan 32 + * @Author wangnan
33 * @Date: Created in 2018/4/16 33 * @Date: Created in 2018/4/16
34 */ 34 */
35 @Service 35 @Service
@@ -53,16 +53,8 @@ public class ImageRepertoryService extends BaseService implements ApplicationEve @@ -53,16 +53,8 @@ public class ImageRepertoryService extends BaseService implements ApplicationEve
53 53
54 // 1.构造查询参数 54 // 1.构造查询参数
55 SearchParam searchParam = new SearchParam(); 55 SearchParam searchParam = new SearchParam();
56 - int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));  
57 - int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));  
58 - if (page < 1 || pageSize < 0) {  
59 - return new SearchApiResult().setCode(400).setMessage("分页参数错误");  
60 - }  
61 - if (pageSize > 100) {  
62 - pageSize = 100;  
63 - }  
64 - searchParam.setOffset((page - 1) * pageSize);  
65 - searchParam.setSize(pageSize); 56 + int size = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
  57 + searchParam.setSize(size);
66 58
67 //2.配置Filter 59 //2.配置Filter
68 BoolQueryBuilder boolFilter = constructHelperFilterBuilder(paramMap); 60 BoolQueryBuilder boolFilter = constructHelperFilterBuilder(paramMap);
@@ -80,7 +72,6 @@ public class ImageRepertoryService extends BaseService implements ApplicationEve @@ -80,7 +72,6 @@ public class ImageRepertoryService extends BaseService implements ApplicationEve
80 // 8.构造返回结果并加入缓存 72 // 8.构造返回结果并加入缓存
81 JSONObject dataMap = new JSONObject(); 73 JSONObject dataMap = new JSONObject();
82 dataMap.put("total", searchResult.getTotal()); 74 dataMap.put("total", searchResult.getTotal());
83 - dataMap.put("page", searchResult.getPage());  
84 dataMap.put("page_size", searchParam.getSize()); 75 dataMap.put("page_size", searchParam.getSize());
85 dataMap.put("page_total", searchResult.getTotalPage()); 76 dataMap.put("page_total", searchResult.getTotalPage());
86 dataMap.put("imageRepertory_list", getImageRepertoryMap(searchResult.getResultList())); 77 dataMap.put("imageRepertory_list", getImageRepertoryMap(searchResult.getResultList()));