admin
2021-12-24 b5e19564dcbf1b7ec12946209d74313479f9dfe1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.yeshi.buwan.service.manager.search;
 
import com.yeshi.buwan.domain.HomeVideo;
import com.yeshi.buwan.domain.VideoResource;
import com.yeshi.buwan.domain.entity.PlayUrl;
import com.yeshi.buwan.domain.solr.SolrShortVideo;
import com.yeshi.buwan.dto.search.SolrResultDTO;
import com.yeshi.buwan.dto.search.SolrShortVideoSearchFilter;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.util.video.VideoUtil;
import com.yeshi.buwan.videos.iqiyi.util.IqiyiUtil2;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Sort;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleFilterQuery;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.List;
 
/**
 * 搜索引擎-短视频数据管理
 */
@Component
public class SolrShortVideoDataManager {
 
    private final String CORE_NAME = "buwan_short_video";
 
    private final Logger logger = LoggerFactory.getLogger(SolrShortVideoDataManager.class);
 
    @Resource
    private SolrTemplate solrTemplate;
 
 
    public void saveOrUpdate(SolrShortVideo solrShortVideo) {
        if (solrShortVideo.getLocalUpdateTime() == null) {
            solrShortVideo.setLocalUpdateTime(System.currentTimeMillis());
        }
        UpdateResponse updateResponse = solrTemplate.saveBean(CORE_NAME, solrShortVideo);
        if (updateResponse.getStatus() == 0) {
            solrTemplate.commit(CORE_NAME);
        } else {
            solrTemplate.rollback(CORE_NAME);
        }
    }
 
    public void saveOrUpdate(List<SolrShortVideo> shortVideoList) {
        UpdateResponse updateResponse = solrTemplate.saveBeans(CORE_NAME, shortVideoList);
        if (updateResponse.getStatus() == 0) {
            solrTemplate.commit(CORE_NAME);
        } else {
            solrTemplate.rollback(CORE_NAME);
        }
    }
 
    /**
     * 删除所有
     */
    public void clear() {
        Query query = new SimpleQuery("name:*");
        UpdateResponse updateResponse = solrTemplate.delete(query);
        if (updateResponse.getStatus() == 0) {
            solrTemplate.commit();
        } else {
            solrTemplate.rollback();
        }
    }
 
    public void deleteById(String id) {
        UpdateResponse updateResponse = solrTemplate.deleteById(CORE_NAME, id);
        if (updateResponse.getStatus() == 0) {
            solrTemplate.commit(CORE_NAME);
        } else {
            solrTemplate.rollback(CORE_NAME);
        }
    }
 
    public SolrShortVideo findOne(String id) {
        Query query = new SimpleQuery(new Criteria("id").is(id));
        ScoredPage<SolrShortVideo> result = solrTemplate.queryForPage(CORE_NAME, query, SolrShortVideo.class);
        if (result != null && result.getContent().size() > 0) {
            return result.getContent().get(0);
        }
        return null;
    }
 
    public SolrResultDTO find(SolrShortVideoSearchFilter filter, int page, int pageSize) {
        if (filter.getKey() == null) {
            filter.setKey("");
        }
        logger.info(filter.getKey() + "#" + page);
        Query query = null;
        if (!StringUtil.isNullOrEmpty(filter.getKey())) {
            //solr精准检索需要带引号
            if (filter.isFuzzy()) {
                query = new SimpleQuery(new Criteria("name").expression(filter.getKey()));
            } else {
                try {
                    query = new SimpleQuery(new Criteria("name_str").startsWith(filter.getKey()));
                } catch (InvalidDataAccessApiUsageException e) {
                    query = new SimpleQuery(new Criteria("name_str").expression("\"" + filter.getKey() + "\""));
                }
            }
        } else {
            query = new SimpleQuery("*:*");
        }
 
 
        if (filter.getRootVideoType() != null) {
            query.addFilterQuery(new SimpleFilterQuery(Criteria.where("root_video_type").is(filter.getRootVideoType())));
        }
 
        if (filter.getResourceIds() != null && filter.getResourceIds().size() > 0) {
            Criteria criteria = null;
            for (Integer resourceId : filter.getResourceIds()) {
                if (criteria == null)
                    criteria = Criteria.where("resource_id").is(resourceId);
                else
                    criteria = criteria.or("resource_id").is(resourceId);
            }
            query.addFilterQuery(new SimpleFilterQuery(criteria));
        }
 
        if (filter.getCategoryName() != null) {
            query.addFilterQuery(new SimpleFilterQuery(Criteria.where("category_names").contains(filter.getCategoryName())));
        }
 
        //区域
        if (filter.getAreas() != null && filter.getAreas().size() > 0) {
            Criteria criteria = null;
            for (String area : filter.getAreas()) {
                if (criteria == null)
                    criteria = Criteria.where("area").is(area);
                else
                    criteria = criteria.or("area").is(area);
            }
            query.addFilterQuery(new SimpleFilterQuery(criteria));
        }
 
        if (filter.getTag() != null) {
            query.addFilterQuery(new SimpleFilterQuery(Criteria.where("tag").is(filter.getTag())));
        }
 
 
        /** 设置分页开始记录数(第一页) 默认0 */
        query.setOffset((page - 1) * pageSize);
        /** 设置每页显示记录数,默认10 */
        query.setRows(pageSize);
        if (!StringUtil.isNullOrEmpty(filter.getSortKey())) {
            query.addSort(new Sort(Sort.Direction.DESC, filter.getSortKey()));
        }
        ScoredPage<SolrShortVideo> result = solrTemplate.queryForPage(CORE_NAME, query, SolrShortVideo.class);
        System.out.println("总记录数:" + result.getTotalElements());
        List<SolrShortVideo> list = result.getContent();
        return new SolrResultDTO(list, (int) result.getTotalElements());
    }
 
    public PlayUrl getPlayUrl(String id, VideoResource videoResource) {
        //短视频
        if (VideoUtil.getVideoFromType(id) == HomeVideo.FROM_TYPE_SHORT) {
            SolrShortVideo solrShortVideo = findOne(id);
            if (solrShortVideo == null)
                return null;
            PlayUrl pu = new PlayUrl();
            pu.setParams("");
            pu.setPlayType(IqiyiUtil2.PLAY_HTML);
            pu.setResource(videoResource);
            pu.setUrl(solrShortVideo.getPlayUrl());
            //韩剧
            pu.setUrl(pu.getUrl().replace("_bwap", "_hjvap"));
            return pu;
        }
        return null;
    }
 
}