|
@@ -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;
|
|
|
+ }
|
|
|
}
|