Authored by wangnan9279

图库fix

package com.yoho.search.common;
import java.util.Map;
import com.yoho.search.base.utils.ConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import com.yoho.search.base.utils.ConvertUtils;
import com.yoho.search.service.helper.SearchParamHelper;
import com.yoho.search.service.helper.SearchSortHelper;
import java.util.List;
import java.util.Map;
/**
* Created by wangnan on 2016/12/16.
*/
public class BaseService {
/**
* 增加一个termsQuery
*/
protected BoolQueryBuilder boolQueryTermsQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {
if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) {
int[] values = ConvertUtils.stringToIntArray(paramMap.get(fieldName), ",");
boolFilter.must(QueryBuilders.termsQuery(fieldName, values));
}
return boolFilter;
}
/**
* 增加一个termsQuery
*/
protected BoolQueryBuilder boolQueryTermsQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {
if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) {
List<Integer> values = ConvertUtils.stringToIntList(paramMap.get(fieldName), ",");
if (values == null || values.isEmpty()) {
return boolFilter;
}
boolFilter.must(QueryBuilders.termsQuery(fieldName, values));
}
return boolFilter;
}
/**
* 增加一个termQuery
*/
protected BoolQueryBuilder boolQueryTermQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {
if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) {
boolFilter.must(QueryBuilders.termQuery(fieldName, paramMap.get(fieldName)));
}
return boolFilter;
}
/**
* 增加一个termQuery
*/
protected BoolQueryBuilder boolQueryTermQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {
if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) {
boolFilter.must(QueryBuilders.termQuery(fieldName, paramMap.get(fieldName)));
}
return boolFilter;
}
/**
* 通用的过滤请求
*/
protected BoolQueryBuilder generalHandler(Map<String, String> paramMap, BoolQueryBuilder boolFilter) {
for (String key : paramMap.keySet()) {
if (key.startsWith("not_")) {
String[] values = paramMap.get(key).split(",");
boolFilter.mustNot(QueryBuilders.termsQuery(key.substring(4, key.length()), values));
}
if (key.startsWith("filter_")) {
String field = key.substring(7, key.length());
boolFilter.must(QueryBuilders.termsQuery(field, paramMap.get(key).split(",")));
}
if (key.startsWith("missing_")) {
String field = key.substring(8, key.length());
boolFilter.mustNot(QueryBuilders.existsQuery(field));
}
}
if (boolFilter.hasClauses()) {
return boolFilter;
} else {
return null;
}
}
/**
* 通用的过滤请求
*/
protected BoolQueryBuilder generalHandler(Map<String, String> paramMap, BoolQueryBuilder boolFilter) {
for (String key : paramMap.keySet()) {
if (key.startsWith("not_")) {
String[] values = paramMap.get(key).split(",");
boolFilter.mustNot(QueryBuilders.termsQuery(key.substring(4, key.length()), values));
}
if (key.startsWith("filter_")) {
String field = key.substring(7, key.length());
boolFilter.must(QueryBuilders.termsQuery(field, paramMap.get(key).split(",")));
}
if (key.startsWith("missing_")) {
String field = key.substring(8, key.length());
boolFilter.mustNot(QueryBuilders.existsQuery(field));
}
}
if (boolFilter.hasClauses()) {
return boolFilter;
} else {
return null;
}
}
protected int getPage(Map<String, String> paramMap) {
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
return page;
}
protected int getPage(Map<String, String> paramMap) {
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
return page;
}
}
... ...
... ... @@ -29,7 +29,7 @@ import java.util.Map;
/**
* 图库服务
* @Author: wangnan
* @Author wangnan
* @Date: Created in 2018/4/16
*/
@Service
... ... @@ -53,16 +53,8 @@ public class ImageRepertoryService extends BaseService implements ApplicationEve
// 1.构造查询参数
SearchParam searchParam = new SearchParam();
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
if (page < 1 || pageSize < 0) {
return new SearchApiResult().setCode(400).setMessage("分页参数错误");
}
if (pageSize > 100) {
pageSize = 100;
}
searchParam.setOffset((page - 1) * pageSize);
searchParam.setSize(pageSize);
int size = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
searchParam.setSize(size);
//2.配置Filter
BoolQueryBuilder boolFilter = constructHelperFilterBuilder(paramMap);
... ... @@ -80,7 +72,6 @@ public class ImageRepertoryService extends BaseService implements ApplicationEve
// 8.构造返回结果并加入缓存
JSONObject dataMap = new JSONObject();
dataMap.put("total", searchResult.getTotal());
dataMap.put("page", searchResult.getPage());
dataMap.put("page_size", searchParam.getSize());
dataMap.put("page_total", searchResult.getTotalPage());
dataMap.put("imageRepertory_list", getImageRepertoryMap(searchResult.getResultList()));
... ...