Browse Source

data-service

孙永军 2 years ago
parent
commit
eb4578f215

+ 2 - 1
src/main/java/com/sxtvs/open/api/news/controller/HotRankController.java

@@ -1,5 +1,6 @@
 package com.sxtvs.open.api.news.controller;
 
+import com.sxtvs.open.api.news.dto.HotRankDTO;
 import com.sxtvs.open.api.news.entity.HotRank;
 import com.sxtvs.open.api.news.service.impl.HotRankServiceImpl;
 import jakarta.annotation.Resource;
@@ -24,7 +25,7 @@ public class HotRankController {
     private HotRankServiceImpl hotRankService;
 
     @RequestMapping("list")
-    public List<HotRank> rank(){
+    public List<HotRankDTO> rank(){
         return hotRankService.todayRank();
     }
 }

+ 16 - 1
src/main/java/com/sxtvs/open/api/news/controller/YoumeiDataController.java

@@ -1,8 +1,16 @@
 package com.sxtvs.open.api.news.controller;
 
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.sxtvs.open.api.news.dto.DataRequestDTO;
+import com.sxtvs.open.api.news.entity.YoumeiData;
+import com.sxtvs.open.api.news.service.impl.YoumeiDataServiceImpl;
+import jakarta.annotation.Resource;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.List;
+
 /**
  * <p>
  *  前端控制器
@@ -12,7 +20,14 @@ import org.springframework.web.bind.annotation.RestController;
  * @since 2023-02-20
  */
 @RestController
-@RequestMapping("/news/youmeiData")
+@RequestMapping("/news/data")
 public class YoumeiDataController {
 
+    @Resource
+    private YoumeiDataServiceImpl youmeiDataService;
+
+    @RequestMapping("search")
+    public Page<YoumeiData> search(@RequestBody DataRequestDTO dataRequestDTO){
+        return youmeiDataService.search(dataRequestDTO);
+    }
 }

+ 20 - 0
src/main/java/com/sxtvs/open/api/news/dto/DataRequestDTO.java

@@ -0,0 +1,20 @@
+package com.sxtvs.open.api.news.dto;
+
+import lombok.Data;
+
+import java.time.LocalDateTime;
+
+@Data
+public class DataRequestDTO {
+    private LocalDateTime dt;
+
+    private String category;
+
+    private String city;
+
+    private String keywords = "";
+
+    private Integer page = 1;
+
+    private Integer pageSize = 10;
+}

+ 27 - 0
src/main/java/com/sxtvs/open/api/news/dto/HotRankDTO.java

@@ -0,0 +1,27 @@
+package com.sxtvs.open.api.news.dto;
+
+import com.sxtvs.open.api.news.entity.HotRank;
+import lombok.Getter;
+import lombok.Setter;
+
+@Setter
+@Getter
+public class HotRankDTO {
+    private String rk;
+
+    private String content;
+
+    private String rsCount;
+
+    private String dt;
+
+    public HotRankDTO() {
+    }
+
+    public HotRankDTO(HotRank hotRank) {
+        this.rk = hotRank.getRk();
+        this.content = hotRank.getContent();
+        this.rsCount = hotRank.getRsCount();
+        this.dt = hotRank.getDt();
+    }
+}

+ 5 - 2
src/main/java/com/sxtvs/open/api/news/service/impl/HotRankServiceImpl.java

@@ -1,5 +1,6 @@
 package com.sxtvs.open.api.news.service.impl;
 
+import com.sxtvs.open.api.news.dto.HotRankDTO;
 import com.sxtvs.open.api.news.entity.HotRank;
 import com.sxtvs.open.api.news.mapper.HotRankMapper;
 import com.sxtvs.open.api.news.service.IHotRankService;
@@ -9,6 +10,7 @@ import org.springframework.stereotype.Service;
 import java.time.LocalDate;
 import java.time.format.DateTimeFormatter;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -21,8 +23,9 @@ import java.util.List;
 @Service
 public class HotRankServiceImpl extends ServiceImpl<HotRankMapper, HotRank> implements IHotRankService {
 
-    public List<HotRank> todayRank(){
+    public List<HotRankDTO> todayRank(){
         String dt = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
-        return lambdaQuery().eq(HotRank::getDt, dt).eq(HotRank::getRankName, "密度-头条热榜").orderByAsc(HotRank::getRk).list();
+        return lambdaQuery().eq(HotRank::getDt, dt).eq(HotRank::getRankName, "密度-头条热榜").orderByAsc(HotRank::getRk).list()
+                .stream().map(HotRankDTO::new).collect(Collectors.toList());
     }
 }

+ 52 - 0
src/main/java/com/sxtvs/open/api/news/service/impl/YoumeiDataServiceImpl.java

@@ -1,11 +1,26 @@
 package com.sxtvs.open.api.news.service.impl;
 
+import co.elastic.clients.elasticsearch.ElasticsearchClient;
+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.core.SearchRequest;
+import co.elastic.clients.elasticsearch.core.SearchResponse;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.sxtvs.open.api.news.dto.DataRequestDTO;
 import com.sxtvs.open.api.news.entity.YoumeiData;
 import com.sxtvs.open.api.news.mapper.YoumeiDataMapper;
 import com.sxtvs.open.api.news.service.IYoumeiDataService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import jakarta.annotation.Resource;
 import org.springframework.stereotype.Service;
 
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
 /**
  * <p>
  *  服务实现类
@@ -16,5 +31,42 @@ import org.springframework.stereotype.Service;
  */
 @Service
 public class YoumeiDataServiceImpl extends ServiceImpl<YoumeiDataMapper, YoumeiData> implements IYoumeiDataService {
+    @Resource
+    private ElasticsearchClient elasticsearchClient;
+
+    public Page<YoumeiData> search(DataRequestDTO dataRequestDTO){
+        SearchResponse<YoumeiData> response = null;
+        Page<YoumeiData> page = new Page<>(dataRequestDTO.getPage(), dataRequestDTO.getPageSize());
+        int offset = (int) ((page.getCurrent() - 1) * page.getSize());
+        try {
+            SearchRequest.Builder builder = new SearchRequest.Builder();
+            builder.index("news_data").from(offset).size((int) page.getSize());
+            if("".equals(dataRequestDTO.getKeywords())){
+                builder.sort(SortOptions.of(
+                        so -> so.field(fs->fs.field("offset").order(SortOrder.Desc))
+                ));
+            }else{
+                builder.query(
+                        Query.of(y -> y.multiMatch(MultiMatchQuery.of(z -> z.fields(Arrays.asList("title", "content")).query(dataRequestDTO.getKeywords()))))
+                ).minScore(10D);
+            }
+            SearchRequest searchRequest = builder.build();
+
+            response = elasticsearchClient.search(searchRequest, YoumeiData.class);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        List<YoumeiData> data = new ArrayList<>();
+        long total = 0;
+        if (response != null) {
+            response.hits().hits().forEach(x -> data.add(x.source()));
+            if (response.hits().total() != null) {
+                total = response.hits().total().value();
+            }
+        }
+        page.setTotal(total);
+        page.setRecords(data);
 
+        return page;
+    }
 }

+ 11 - 1
src/test/data-service.http

@@ -1,2 +1,12 @@
 ### 热榜
-GET http://localhost/news/hotRank/list
+GET http://localhost/news/hotRank/list
+
+### 搜索
+GET http://localhost/news/data/search
+Content-Type: application/json;charset=UTF-8
+
+{
+"keywords": "拜登为何此时秘密访问乌克兰",
+  "page": 1,
+  "pageSize": 10
+}

+ 22 - 0
src/test/java/com/sxtvs/open/EsClientTests.java

@@ -1,6 +1,9 @@
 package com.sxtvs.open;
 
 import co.elastic.clients.elasticsearch.ElasticsearchClient;
+import co.elastic.clients.elasticsearch._types.SortOptions;
+import co.elastic.clients.elasticsearch._types.SortOrder;
+import co.elastic.clients.elasticsearch._types.query_dsl.MatchAllQuery;
 import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
 import co.elastic.clients.elasticsearch._types.query_dsl.MultiMatchQuery;
 import co.elastic.clients.elasticsearch._types.query_dsl.Query;
@@ -87,4 +90,23 @@ public class EsClientTests {
         System.out.println(data.size());
         System.out.println(data.stream().limit(2));
     }
+
+    @Test
+    public void searchPage() throws IOException {
+
+        SearchResponse<YoumeiData> response = elasticsearchClient.search(SearchRequest.of(x -> x.index("news_data").sort(SortOptions.of(
+                so -> so.field(fs->fs.field("offset").order(SortOrder.Desc))
+        )).size(100).from(0)), YoumeiData.class);
+        List<YoumeiData> data = new ArrayList<>();
+        response.hits().hits().forEach(x -> {
+            System.out.println(x.score());
+            data.add(x.source());
+            System.out.println(x.source());
+        });
+
+        System.out.println(response.hits().total().value());
+
+        System.out.println(data.size());
+        System.out.println(data.stream().limit(2));
+    }
 }