|
@@ -5,11 +5,16 @@ import co.elastic.clients.elasticsearch._types.SortOptions;
|
|
|
import co.elastic.clients.elasticsearch._types.SortOrder;
|
|
|
import co.elastic.clients.elasticsearch._types.query_dsl.MultiMatchQuery;
|
|
|
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
|
|
|
+import co.elastic.clients.elasticsearch._types.query_dsl.RangeQuery;
|
|
|
import co.elastic.clients.elasticsearch.core.SearchRequest;
|
|
|
import co.elastic.clients.elasticsearch.core.SearchResponse;
|
|
|
+import co.elastic.clients.json.JsonData;
|
|
|
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.YoumeiData;
|
|
|
+import com.sxtvs.open.api.news.entity.YoumeiEsData;
|
|
|
import com.sxtvs.open.api.news.mapper.YoumeiDataMapper;
|
|
|
import com.sxtvs.open.api.news.service.IYoumeiDataService;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
@@ -17,9 +22,11 @@ import jakarta.annotation.Resource;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -34,13 +41,24 @@ public class YoumeiDataServiceImpl extends ServiceImpl<YoumeiDataMapper, YoumeiD
|
|
|
@Resource
|
|
|
private ElasticsearchClient elasticsearchClient;
|
|
|
|
|
|
- public Page<YoumeiData> search(DataRequestDTO dataRequestDTO){
|
|
|
+ @Resource
|
|
|
+ private ObjectMapper objectMapper;
|
|
|
+
|
|
|
+ public Page<YoumeiEsData> search(DataRequestDTO dataRequestDTO){
|
|
|
SearchResponse<YoumeiData> response = null;
|
|
|
- Page<YoumeiData> page = new Page<>(dataRequestDTO.getPage(), dataRequestDTO.getPageSize());
|
|
|
+ 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");
|
|
|
+
|
|
|
try {
|
|
|
SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
builder.index("news_data").from(offset).size((int) page.getSize());
|
|
|
+ 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())){
|
|
|
builder.sort(SortOptions.of(
|
|
|
so -> so.field(fs->fs.field("offset").order(SortOrder.Desc))
|
|
@@ -56,10 +74,13 @@ public class YoumeiDataServiceImpl extends ServiceImpl<YoumeiDataMapper, YoumeiD
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
- List<YoumeiData> data = new ArrayList<>();
|
|
|
+ List<YoumeiEsData> data = new ArrayList<>();
|
|
|
long total = 0;
|
|
|
if (response != null) {
|
|
|
- response.hits().hits().forEach(x -> data.add(x.source()));
|
|
|
+ response.hits().hits().forEach(x -> {
|
|
|
+ assert x.source() != null;
|
|
|
+ data.add(YoumeiEsData.of(x.source()));
|
|
|
+ });
|
|
|
if (response.hits().total() != null) {
|
|
|
total = response.hits().total().value();
|
|
|
}
|