parent
4834c66f04
commit
951dd819ff
12
pom.xml
12
pom.xml
|
@ -243,6 +243,18 @@
|
||||||
<version>1.1.1</version>
|
<version>1.1.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Elasticsearch -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.elasticsearch.client</groupId>
|
||||||
|
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
||||||
|
<version>7.17.9</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.elasticsearch</groupId>
|
||||||
|
<artifactId>elasticsearch</artifactId>
|
||||||
|
<version>7.17.9</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.guwan.backend.config;
|
||||||
|
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.elasticsearch.client.RestClient;
|
||||||
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.ConditionalOnProperty;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnProperty(name = "easy-es.enable", havingValue = "true")
|
||||||
|
public class ElasticsearchConfig {
|
||||||
|
|
||||||
|
@Value("${easy-es.address}")
|
||||||
|
private String address;
|
||||||
|
|
||||||
|
@Value("${easy-es.username:}")
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@Value("${easy-es.password:}")
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RestHighLevelClient restHighLevelClient() {
|
||||||
|
String[] parts = address.split(":");
|
||||||
|
String host = parts[0];
|
||||||
|
int port = Integer.parseInt(parts[1]);
|
||||||
|
|
||||||
|
return new RestHighLevelClient(
|
||||||
|
RestClient.builder(new HttpHost(host, port, "http"))
|
||||||
|
.setRequestConfigCallback(requestConfigBuilder -> {
|
||||||
|
requestConfigBuilder.setConnectTimeout(5000);
|
||||||
|
requestConfigBuilder.setSocketTimeout(60000);
|
||||||
|
return requestConfigBuilder;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,7 +9,7 @@ import lombok.Data;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@IndexName("videos")
|
@IndexName("video_index")
|
||||||
public class VideoDocument {
|
public class VideoDocument {
|
||||||
|
|
||||||
@IndexId
|
@IndexId
|
||||||
|
|
|
@ -10,12 +10,14 @@ import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
|
@ConditionalOnProperty(name = "easy-es.enable", havingValue = "true")
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class VideoSearchService {
|
public class VideoSearchService {
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ import com.guwan.backend.util.SecurityUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@ -30,7 +31,9 @@ public class VideoServiceImpl implements VideoService {
|
||||||
private final MinioUtil minioUtil;
|
private final MinioUtil minioUtil;
|
||||||
private final SecurityUtil securityUtil;
|
private final SecurityUtil securityUtil;
|
||||||
private final VideoLikeMapper videoLikeMapper;
|
private final VideoLikeMapper videoLikeMapper;
|
||||||
private final VideoSearchService videoSearchService;
|
|
||||||
|
@Autowired(required = false)
|
||||||
|
private VideoSearchService videoSearchService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@ -62,7 +65,7 @@ public class VideoServiceImpl implements VideoService {
|
||||||
videoMapper.insert(video);
|
videoMapper.insert(video);
|
||||||
|
|
||||||
// 保存到ES
|
// 保存到ES
|
||||||
videoSearchService.saveOrUpdate(video);
|
updateEs(video);
|
||||||
|
|
||||||
return convertToDTO(video);
|
return convertToDTO(video);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -115,7 +118,7 @@ public class VideoServiceImpl implements VideoService {
|
||||||
videoMapper.deleteById(id);
|
videoMapper.deleteById(id);
|
||||||
|
|
||||||
// 从ES中删除
|
// 从ES中删除
|
||||||
videoSearchService.delete(id);
|
deleteFromEs(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -152,7 +155,7 @@ public class VideoServiceImpl implements VideoService {
|
||||||
videoMapper.updateById(video);
|
videoMapper.updateById(video);
|
||||||
|
|
||||||
// 更新ES中的观看次数
|
// 更新ES中的观看次数
|
||||||
videoSearchService.updateViewCount(id, video.getViewCount());
|
updateEsViewCount(id, video.getViewCount());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,7 +202,7 @@ public class VideoServiceImpl implements VideoService {
|
||||||
videoMapper.updateById(video);
|
videoMapper.updateById(video);
|
||||||
|
|
||||||
// 更新ES中的点赞次数
|
// 更新ES中的点赞次数
|
||||||
videoSearchService.updateLikeCount(id, video.getLikeCount());
|
updateEsLikeCount(id, video.getLikeCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加新方法:检查用户是否已点赞
|
// 添加新方法:检查用户是否已点赞
|
||||||
|
@ -218,12 +221,20 @@ public class VideoServiceImpl implements VideoService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<VideoDTO> searchVideosByEs(String keyword) {
|
public List<VideoDTO> searchVideosByEs(String keyword) {
|
||||||
return videoSearchService.search(keyword);
|
if (videoSearchService != null) {
|
||||||
|
return videoSearchService.search(keyword);
|
||||||
|
}
|
||||||
|
// 降级到数据库搜索
|
||||||
|
return getVideoList(1, 10, keyword).getRecords();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<VideoDTO> getSimilarVideosByEs(Long id, int limit) {
|
public List<VideoDTO> getSimilarVideosByEs(Long id, int limit) {
|
||||||
return videoSearchService.findSimilar(id, limit);
|
if (videoSearchService != null) {
|
||||||
|
return videoSearchService.findSimilar(id, limit);
|
||||||
|
}
|
||||||
|
// 降级到随机推荐
|
||||||
|
return getVideoList(1, limit, null).getRecords();
|
||||||
}
|
}
|
||||||
|
|
||||||
private VideoDTO convertToDTO(Video video) {
|
private VideoDTO convertToDTO(Video video) {
|
||||||
|
@ -238,4 +249,28 @@ public class VideoServiceImpl implements VideoService {
|
||||||
|
|
||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateEs(Video video) {
|
||||||
|
if (videoSearchService != null) {
|
||||||
|
videoSearchService.saveOrUpdate(video);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteFromEs(Long id) {
|
||||||
|
if (videoSearchService != null) {
|
||||||
|
videoSearchService.delete(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateEsViewCount(Long id, Integer viewCount) {
|
||||||
|
if (videoSearchService != null) {
|
||||||
|
videoSearchService.updateViewCount(id, viewCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateEsLikeCount(Long id, Integer likeCount) {
|
||||||
|
if (videoSearchService != null) {
|
||||||
|
videoSearchService.updateLikeCount(id, likeCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -132,15 +132,18 @@ srs:
|
||||||
url: http://localhost:1985 # SRS HTTP API地址
|
url: http://localhost:1985 # SRS HTTP API地址
|
||||||
|
|
||||||
easy-es:
|
easy-es:
|
||||||
enable: true
|
enable: false # 改为false禁用Easy-Es
|
||||||
address: localhost:9200
|
address: localhost:9200 # ES地址
|
||||||
username: elastic # 如果有的话
|
username: ${ES_USERNAME:} # ES用户名,可选
|
||||||
password: elastic # 如果有的话
|
password: ${ES_PASSWORD:} # ES密码,可选
|
||||||
global-config:
|
global-config:
|
||||||
process-index-mode: manual
|
process-index-mode: manual
|
||||||
print-dsl: true
|
print-dsl: true
|
||||||
distributed: false
|
distributed: false
|
||||||
response-log: true
|
response-log: true
|
||||||
|
db-config:
|
||||||
|
map-underscore-to-camel-case: true
|
||||||
|
index-prefix: video_ # 索引前缀
|
||||||
|
|
||||||
netty:
|
netty:
|
||||||
danmaku:
|
danmaku:
|
||||||
|
|
Loading…
Reference in New Issue