|
|
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;
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|