|
@@ -15,6 +15,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.sxtvs.open.api.news.dto.DataRequestDTO;
|
|
|
+import com.sxtvs.open.api.news.entity.CategoryData;
|
|
|
import com.sxtvs.open.api.news.entity.YoumeiData;
|
|
|
import com.sxtvs.open.api.news.entity.YoumeiEsData;
|
|
|
import com.sxtvs.open.api.news.entity.YoumeiOffset;
|
|
@@ -23,6 +24,8 @@ import com.sxtvs.open.api.news.service.IYoumeiDataService;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import jakarta.annotation.Resource;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.util.TextUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.IOException;
|
|
@@ -49,9 +52,13 @@ public class YoumeiDataServiceImpl extends ServiceImpl<YoumeiDataMapper, YoumeiD
|
|
|
@Resource
|
|
|
private YoumeiOffsetServiceImpl youmeiOffsetService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private ObjectMapper objectMapper;
|
|
|
+
|
|
|
|
|
|
public Page<YoumeiEsData> search(DataRequestDTO dataRequestDTO){
|
|
|
- SearchResponse<YoumeiData> response = null;
|
|
|
+
|
|
|
+ SearchResponse<YoumeiEsData> response = null;
|
|
|
Page<YoumeiEsData> page = new Page<>(dataRequestDTO.getPage(), dataRequestDTO.getPageSize());
|
|
|
int offset = (int) ((page.getCurrent() - 1) * page.getSize());
|
|
|
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
@@ -59,13 +66,16 @@ public class YoumeiDataServiceImpl extends ServiceImpl<YoumeiDataMapper, YoumeiD
|
|
|
try {
|
|
|
SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
builder.index("news_data").from(offset).size((int) page.getSize());
|
|
|
- if(dataRequestDTO.getStart() != null){
|
|
|
|
|
|
+ // 日期
|
|
|
+ if(dataRequestDTO.getStart() != null){
|
|
|
String start = dataRequestDTO.getStart().format(dateTimeFormatter);
|
|
|
String end = dataRequestDTO.getEnd().format(dateTimeFormatter);
|
|
|
builder.query(Query.of(x->x.range(y -> y.field("publishTime").gte(JsonData.of(start)).lte(JsonData.of(end)))));
|
|
|
}
|
|
|
- if("".equals(dataRequestDTO.getKeywords())){
|
|
|
+
|
|
|
+ // 关键词
|
|
|
+ if(TextUtils.isEmpty(dataRequestDTO.getKeywords())){
|
|
|
builder.sort(SortOptions.of(
|
|
|
so -> so.field(fs->fs.field("offset").order(SortOrder.Desc))
|
|
|
));
|
|
@@ -75,9 +85,16 @@ public class YoumeiDataServiceImpl extends ServiceImpl<YoumeiDataMapper, YoumeiD
|
|
|
).minScore(10D);
|
|
|
}
|
|
|
|
|
|
+ // 类型
|
|
|
+ if(!TextUtils.isEmpty(dataRequestDTO.getCategory())){
|
|
|
+ builder.query(
|
|
|
+ Query.of(x -> x.term(y -> y.field("category").value(dataRequestDTO.getCategory())))
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
SearchRequest searchRequest = builder.build();
|
|
|
|
|
|
- response = elasticsearchClient.search(searchRequest, YoumeiData.class);
|
|
|
+ response = elasticsearchClient.search(searchRequest, YoumeiEsData.class);
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -86,7 +103,7 @@ public class YoumeiDataServiceImpl extends ServiceImpl<YoumeiDataMapper, YoumeiD
|
|
|
if (response != null) {
|
|
|
response.hits().hits().forEach(x -> {
|
|
|
assert x.source() != null;
|
|
|
- data.add(YoumeiEsData.of(x.source()));
|
|
|
+ data.add(x.source());
|
|
|
});
|
|
|
if (response.hits().total() != null) {
|
|
|
total = response.hits().total().value();
|
|
@@ -108,7 +125,7 @@ public class YoumeiDataServiceImpl extends ServiceImpl<YoumeiDataMapper, YoumeiD
|
|
|
long offset = youmeiOffset.getOffset() + 1L;
|
|
|
long max = 0L;
|
|
|
while (true){
|
|
|
- List<YoumeiData> youmeiDataList = lambdaQuery().gt(YoumeiData::getOffset, offset).last("limit 1000").list();
|
|
|
+ List<CategoryData> youmeiDataList = baseMapper.getCategoryList(offset, 1000);
|
|
|
log.info("本次数据{}条,offset从{}开始",youmeiDataList.size(), offset);
|
|
|
if (youmeiDataList.size() == 0){
|
|
|
break;
|
|
@@ -117,7 +134,8 @@ public class YoumeiDataServiceImpl extends ServiceImpl<YoumeiDataMapper, YoumeiD
|
|
|
List<BulkOperation> bulkOperations = new ArrayList<>();
|
|
|
|
|
|
youmeiDataList.forEach(a -> {
|
|
|
- bulkOperations.add(BulkOperation.of(b -> b.index(c -> c.id(String.valueOf(a.getOffset())).document(a))));
|
|
|
+ YoumeiEsData youmeiEsData = YoumeiEsData.of(a);
|
|
|
+ bulkOperations.add(BulkOperation.of(b -> b.index(c -> c.id(String.valueOf(youmeiEsData.getOffset())).document(youmeiEsData))));
|
|
|
});
|
|
|
|
|
|
try {
|