Authored by all4you

add search content

... ... @@ -112,6 +112,21 @@ public class ISearchConstans {
public static final String SOCIAL_USER_T_FOLLOW_BACK = "t_follow_back";
/**
* content字段
*/
public static final String CONTENT_ID = "id";
public static final String CONTENT_CID = "cid";
public static final String CONTENT_TYPE = "type";
public static final String CONTENT_APP = "app";
public static final String CONTENT_TITLE = "title";
public static final String CONTENT_COVER = "cover";
public static final String CONTENT_SUMMARY = "summary";
public static final String CONTENT_RELATE_ACCOUNT = "relate_account";
public static final String CONTENT_TAG_IDS = "tag_ids";
public static final String CONTENT_TAG_NAMES = "tag_names";
//多字段匹配模式
public static final String SEARCH_MULTIMATCHQUERY_TYPE = Configuration.getString("search.multiMatchQuery.type", "BEST_FIELDS");
... ...
... ... @@ -14,7 +14,6 @@ public class SearchParamUtils {
public static SearchSourceBuilder genSearchSourceBuilderFromSearchParam(SearchParam searchParam) {
final SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();
// 排序字段
for (String sortField : searchParam.getSortFields().keySet()) {
... ... @@ -33,9 +32,10 @@ public class SearchParamUtils {
sourceBuilder.query(QueryBuilders.boolQuery().must(searchParam.getQuery()).must(searchParam.getGeoDistanceRangeQueryBuilder()).filter(searchParam.getFilter()));
sourceBuilder.sort(searchParam.getGeoDistanceSortBuilder());
}else{
//sourceBuilder.query(QueryBuilders.filteredQuery(searchParam.getQuery(), searchParam.getFiter()));
sourceBuilder.query(QueryBuilders.boolQuery().must(searchParam.getQuery()).filter(searchParam.getFilter()));
}
}else{
sourceBuilder.query(searchParam.getQuery());
}
// 每页记录数
sourceBuilder.size(searchParam.getSize());
... ... @@ -64,8 +64,9 @@ public class SearchParamUtils {
String[] excludes = new String[searchParam.getSourceExcludes().size()];
excludes = searchParam.getSourceExcludes().toArray(excludes);
sourceBuilder.fetchSource(includes,excludes);
if(includes.length>0 || excludes.length>0) {
sourceBuilder.fetchSource(includes, excludes);
}
return sourceBuilder;
}
... ...
package com.yohomars.search.model;
import com.yoho.tools.common.beans.BaseBean;
/**
* @author gris.wang
* @since 2018/3/12
**/
public class ContentRequest extends BaseBean{
private Integer page;
private Integer pageSize;
/**
* 文章的cid
*/
private Integer cid;
/**
* 逗号分隔的tagId
*/
private String tagIds;
/**
* 最小匹配中的tag的个数
*/
private Integer leastMatchTagsCount;
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getTagIds() {
return tagIds;
}
public void setTagIds(String tagIds) {
this.tagIds = tagIds;
}
public Integer getLeastMatchTagsCount() {
return leastMatchTagsCount;
}
public void setLeastMatchTagsCount(Integer leastMatchTagsCount) {
this.leastMatchTagsCount = leastMatchTagsCount;
}
public boolean valid(){
return cid!=null
&& tagIds!=null && tagIds.trim().length()>0 && tagIds.split(",").length>0
&& leastMatchTagsCount!=null && leastMatchTagsCount>0
&& tagIds.split(",").length>=leastMatchTagsCount;
}
}
... ...
package com.yohomars.search.model;
import com.yoho.tools.common.beans.BaseBean;
import org.apache.commons.collections.CollectionUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* @author gris.wang
* @since 2018/3/12
**/
public class ContentResponse extends BaseBean {
private long totalCount;
private long totalPage;
private int page;
private int cost;
private List<Content> contents;
public long getTotalCount() {
return totalCount;
}
public void setTotalCount(long totalCount) {
this.totalCount = totalCount;
}
public long getTotalPage() {
return totalPage;
}
public void setTotalPage(long totalPage) {
this.totalPage = totalPage;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
public List<Content> getContents() {
return contents;
}
public void setContents(List<Content> contents) {
this.contents = contents;
}
public void addContents(Collection<ContentResponse.Content> contents){
if(CollectionUtils.isEmpty(this.contents)){
this.contents = new ArrayList<>();
}
this.contents.addAll(contents);
}
public static class Content{
private Integer cid;
private Integer type;
private Integer app;
private String title;
private String cover;
private String summary;
private Integer relateAccount;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public Integer getApp() {
return app;
}
public void setApp(Integer app) {
this.app = app;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public Integer getRelateAccount() {
return relateAccount;
}
public void setRelateAccount(Integer relateAccount) {
this.relateAccount = relateAccount;
}
}
}
... ...
package com.yohomars.search.restapi;
import com.yoho.tools.common.beans.ApiResponse;
import com.yohomars.search.model.ContentRequest;
import com.yohomars.search.model.ContentResponse;
import com.yohomars.search.service.ContentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author gris.wang
* @since 2018/2/23
**/
@Controller
public class ContentController extends BaseController{
private static final Logger LOGGER = LoggerFactory.getLogger(ContentController.class);
@Autowired
private ContentService contentService;
/**
* 搜索相关文章
* @param request
* @return
*/
@RequestMapping("/content")
@ResponseBody
public ApiResponse<?> search(ContentRequest request) {
try {
ContentResponse response = contentService.search(request);
return new ApiResponse<>(200,"搜索相关文章",response);
} catch (Exception e) {
LOGGER.error("search content error,cause:",e);
return new ApiResponse(400, e.getMessage());
}
}
}
... ...
package com.yohomars.search.service;
import com.yoho.search.dal.model.SearchParam;
import com.yoho.search.dal.model.SearchResult;
import com.yohomars.search.model.ContentRequest;
import com.yohomars.search.model.ContentResponse;
import com.yohomars.search.utils.ISearchConstans;
import com.yohomars.search.utils.Index;
import org.apache.commons.collections.CollectionUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
/**
* @author gris.wang
* @since 2018/2/23
**/
@Service
public class ContentService extends BaseService {
private static final Logger LOGGER = LoggerFactory.getLogger(ContentService.class);
private static final Integer DEFAULT_PAGE = 1;
private static final Integer DEFAULT_PAGE_SIZE = 10;
private static final String CONTENT_INDEX_NAME = Index.content.getIndexName();
@Autowired
private SearchCommonService searchCommonService;
private SearchResult getSearchResult(ContentRequest request,SearchParam searchParam){
String[] tagIds = request.getTagIds().split(",");
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.minimumShouldMatch(String.valueOf(request.getLeastMatchTagsCount()));
for (String tagId : tagIds) {
boolQuery.should(QueryBuilders.termQuery(ISearchConstans.CONTENT_TAG_IDS, tagId));
}
boolQuery.mustNot(QueryBuilders.termQuery(ISearchConstans.CONTENT_CID, request.getCid()));
// 构造query
searchParam.setQuery(boolQuery);
return searchCommonService.doSearch(CONTENT_INDEX_NAME, searchParam);
}
private List<ContentResponse.Content> transfer(List<Map<String, Object>> resultList){
List<ContentResponse.Content> list = new ArrayList<>();
if(CollectionUtils.isNotEmpty(resultList)) {
for (Map<String, Object> map : resultList) {
ContentResponse.Content content = new ContentResponse.Content();
content.setCid(Integer.parseInt(map.get(ISearchConstans.CONTENT_CID).toString()));
content.setType(Integer.parseInt(map.get(ISearchConstans.CONTENT_TYPE).toString()));
content.setApp(Integer.parseInt(map.get(ISearchConstans.CONTENT_APP).toString()));
Object title = map.get(ISearchConstans.CONTENT_TITLE);
content.setTitle(title == null ? null : title.toString());
Object cover = map.get(ISearchConstans.CONTENT_COVER);
content.setCover(cover == null ? null : cover.toString());
Object summary = map.get(ISearchConstans.CONTENT_SUMMARY);
content.setSummary(summary == null ? null : summary.toString());
content.setRelateAccount(Integer.parseInt(map.get(ISearchConstans.CONTENT_RELATE_ACCOUNT).toString()));
list.add(content);
}
}
return list;
}
/**
* 搜索
* @param request
* @return
* @throws Exception
*/
public ContentResponse search(ContentRequest request) throws Exception {
long begin = System.currentTimeMillis();
LOGGER.info("[model=ContentService][func=search][request={}][begin={}]", request, begin);
if(request==null || !request.valid()){
throw new IllegalArgumentException("参数错误");
}
//检测分页参数
int page = request.getPage()==null ? DEFAULT_PAGE : request.getPage();
int pageSize = request.getPageSize()==null ? DEFAULT_PAGE_SIZE : request.getPageSize();
if (page < 1 || pageSize < 0) {
throw new IllegalArgumentException("分页参数错误");
}
// 解析参数转化为检索条件SearchParam
SearchParam searchParam = new SearchParam();
// 设置查询条数
searchParam.setPage(page);
searchParam.setOffset((page - 1) * pageSize);
searchParam.setSize(pageSize);
ContentResponse response = new ContentResponse();
// 查询规则rule下的数据
SearchResult searchResult = getSearchResult(request,searchParam);
long end = System.currentTimeMillis();
if(searchResult!=null) {
response.setTotalCount(searchResult.getTotal());
response.setPage(searchResult.getPage());
response.setTotalPage(searchResult.getTotalPage());
response.setCost((int)(end-begin));
response.addContents(transfer(searchResult.getResultList()));
}
LOGGER.info("[model=ContentService][func=search][request={}][page={}][end={}][cost={}ms]", request,page,end,end - begin);
return response;
}
}
... ...