| | |
| | | import org.springframework.data.solr.core.query.Criteria; |
| | | import org.springframework.data.solr.core.query.Query; |
| | | import org.springframework.data.solr.core.query.SimpleQuery; |
| | | import org.springframework.data.solr.core.query.SolrDataQuery; |
| | | import org.springframework.data.solr.core.query.result.ScoredPage; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * 搜索引擎-专辑数据管理 |
| | | */ |
| | | @Component |
| | | public class SolrAlbumDataManager { |
| | | |
| | | private final static String CORE_NAME = "buwan_album"; |
| | | |
| | | private final Logger logger = LoggerFactory.getLogger(SolrAlbumDataManager.class); |
| | | |
| | |
| | | |
| | | |
| | | public void saveOrUpdate(VideoInfo videoInfo) { |
| | | UpdateResponse updateResponse = solrTemplate.saveBean(SolrVideoFactory.create(videoInfo, videoInfo.getResourceList())); |
| | | UpdateResponse updateResponse = solrTemplate.saveBean(CORE_NAME, SolrVideoFactory.create(videoInfo, videoInfo.getResourceList())); |
| | | if (updateResponse.getStatus() == 0) { |
| | | solrTemplate.commit(); |
| | | solrTemplate.commit(CORE_NAME); |
| | | } else { |
| | | solrTemplate.rollback(); |
| | | solrTemplate.rollback(CORE_NAME); |
| | | } |
| | | } |
| | | |
| | |
| | | for (VideoInfo vi : videoInfoList) { |
| | | solrVideoList.add(SolrVideoFactory.create(vi, vi.getResourceList())); |
| | | } |
| | | UpdateResponse updateResponse = solrTemplate.saveBeans(solrVideoList); |
| | | UpdateResponse updateResponse = solrTemplate.saveBeans(CORE_NAME, solrVideoList); |
| | | if (updateResponse.getStatus() == 0) { |
| | | solrTemplate.commit(); |
| | | solrTemplate.commit(CORE_NAME); |
| | | } else { |
| | | solrTemplate.rollback(); |
| | | solrTemplate.rollback(CORE_NAME); |
| | | } |
| | | } |
| | | |
| | |
| | | // } |
| | | // } |
| | | public void deleteById(String id) { |
| | | UpdateResponse updateResponse = solrTemplate.deleteById(id); |
| | | UpdateResponse updateResponse = solrTemplate.deleteByIds(CORE_NAME, id); |
| | | if (updateResponse.getStatus() == 0) { |
| | | solrTemplate.commit(); |
| | | solrTemplate.commit(CORE_NAME); |
| | | } else { |
| | | solrTemplate.rollback(); |
| | | solrTemplate.rollback(CORE_NAME); |
| | | } |
| | | } |
| | | |
| | | public SolrVideo findOne(String id) { |
| | | SolrVideo solrVideo = solrTemplate.getById(id, SolrVideo.class); |
| | | return solrVideo; |
| | | Optional<SolrVideo> solrVideo = solrTemplate.getById(CORE_NAME, id, SolrVideo.class); |
| | | return solrVideo.get(); |
| | | } |
| | | |
| | | public SolrResultDTO find(SolrVideoSearchFilter filter, int page, int pageSize) { |
| | | if (filter.getKey() == null) { |
| | | filter.setKey(""); |
| | | } |
| | | logger.info(filter.getKey() + "#" + page); |
| | | Query query = new SimpleQuery("name:\"" + filter.getKey() + "\""); |
| | | |
| | | Query query = null; |
| | | if (!StringUtil.isNullOrEmpty(filter.getKey())) { |
| | | //solr精准检索需要带引号 |
| | | query = new SimpleQuery("name:\"" + filter.getKey() + "\""); |
| | | } else { |
| | | query = new SimpleQuery(); |
| | | } |
| | | Criteria criteria = new Criteria("contenttype").is(filter.getContentType()); |
| | | |
| | | if (filter.getVideoType() != null) { |
| | | criteria = criteria.and("root_video_type").is(filter.getVideoType()); |
| | | } |
| | | |
| | | if (filter.getActor() != null) { |
| | | criteria = criteria.and("mainactor").expression("\"" + filter.getActor() + "\""); |
| | | } |
| | | |
| | | if (filter.getDirector() != null) { |
| | | criteria = criteria.and("director").expression("\"" + filter.getDirector() + "\""); |
| | | } |
| | | |
| | | if (filter.getResourceIds() != null && filter.getResourceIds().length > 0) { |
| | | criteria = criteria.and("resourceIds").contains(filter.getResourceIds()); |
| | | } |
| | | |
| | | |
| | | /** 添加条件 */ |
| | | if (criteria != null) |
| | | query.addCriteria(criteria); |
| | | |
| | | /** 设置分页开始记录数(第一页) 默认0 */ |
| | | query.setOffset((page - 1) * pageSize); |
| | | query.setOffset((page - 1) * pageSize * 1L); |
| | | /** 设置每页显示记录数,默认10 */ |
| | | query.setRows(pageSize); |
| | | if (StringUtil.isNullOrEmpty(filter.getSortKey())) { |
| | |
| | | } else { |
| | | query.addSort(new Sort(Sort.Direction.DESC, filter.getSortKey())); |
| | | } |
| | | ScoredPage<SolrVideo> result = solrTemplate.queryForPage(query, SolrVideo.class); |
| | | ScoredPage<SolrVideo> result = solrTemplate.queryForPage(CORE_NAME, query, SolrVideo.class); |
| | | System.out.println("总记录数:" + result.getTotalElements()); |
| | | List<SolrVideo> list = result.getContent(); |
| | | return new SolrResultDTO(list, (int) result.getTotalElements()); |
| | |
| | | public List<String> getSuggestKeyList(String key) { |
| | | Query query = new SimpleQuery("name:\"" + key + "\""); |
| | | /** 设置分页开始记录数(第一页) 默认0 */ |
| | | query.setOffset(0); |
| | | query.setOffset(0L); |
| | | /** 设置每页显示记录数,默认10 */ |
| | | query.setRows(10); |
| | | ScoredPage<SolrVideo> result = solrTemplate.queryForPage(query, SolrVideo.class); |
| | | ScoredPage<SolrVideo> result = solrTemplate.queryForPage(CORE_NAME, query, SolrVideo.class); |
| | | System.out.println("总记录数:" + result.getTotalElements()); |
| | | List<SolrVideo> list = result.getContent(); |
| | | List<String> keyList = new ArrayList<>(); |