|
@@ -3,9 +3,7 @@ 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.MatchQuery;
|
|
|
-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.*;
|
|
|
import co.elastic.clients.elasticsearch.core.BulkRequest;
|
|
|
import co.elastic.clients.elasticsearch.core.SearchRequest;
|
|
|
import co.elastic.clients.elasticsearch.core.SearchResponse;
|
|
@@ -32,6 +30,7 @@ import java.time.format.DateTimeFormatter;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -68,40 +67,40 @@ public class YoumeiDataServiceImpl extends ServiceImpl<YoumeiDataMapper, YoumeiD
|
|
|
try {
|
|
|
SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
builder.index("news_data").from(offset).size((int) page.getSize());
|
|
|
+ Query query = Query.of(q -> q.bool(b -> {
|
|
|
+ // 日期
|
|
|
+ if (dataRequestDTO.getStart() != null) {
|
|
|
+ String start = dataRequestDTO.getStart().format(dateTimeFormatter);
|
|
|
+ String end = dataRequestDTO.getEnd().format(dateTimeFormatter);
|
|
|
+ Query publishTime = Query.of(x -> x.range(y -> y.field("publishTime").gte(JsonData.of(start)).lte(JsonData.of(end))));
|
|
|
|
|
|
- // 日期
|
|
|
- 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)))));
|
|
|
- }
|
|
|
+ b.must(publishTime);
|
|
|
|
|
|
- // 关键词
|
|
|
- if (TextUtils.isEmpty(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);
|
|
|
- }
|
|
|
+ }
|
|
|
+ // 关键词
|
|
|
+ if (!TextUtils.isEmpty(dataRequestDTO.getKeywords())) {
|
|
|
+ b.must(Query.of(y -> y.multiMatch(MultiMatchQuery.of(z -> z.fields(Arrays.asList("title", "content")).query(dataRequestDTO.getKeywords())))));
|
|
|
+ }
|
|
|
|
|
|
- // 类型
|
|
|
- if (!TextUtils.isEmpty(dataRequestDTO.getCategory())) {
|
|
|
- builder.query(
|
|
|
- Query.of(x -> x.term(y -> y.field("category").value(dataRequestDTO.getCategory())))
|
|
|
- );
|
|
|
- }
|
|
|
+ // 类型
|
|
|
+ if (!TextUtils.isEmpty(dataRequestDTO.getCategory())) {
|
|
|
+ b.must(Query.of(x -> x.term(y -> y.field("category").value(dataRequestDTO.getCategory()))));
|
|
|
+ }
|
|
|
|
|
|
- // 城市
|
|
|
- if(!TextUtils.isEmpty(dataRequestDTO.getCity())){
|
|
|
- Arrays.stream(dataRequestDTO.getCity().split(",")).filter(x -> !"其他".equals(x) && !"全部".equals(x)).forEach(city -> {
|
|
|
- builder.query(
|
|
|
- Query.of(y -> y.match(MatchQuery.of(z -> z.field("city").query(city))))
|
|
|
- );
|
|
|
- });
|
|
|
- }
|
|
|
+ // 城市
|
|
|
+ if (!TextUtils.isEmpty(dataRequestDTO.getCity())) {
|
|
|
+ List<String> cits = Arrays.stream(dataRequestDTO.getCity().split(",")).filter(x -> !"其他".equals(x) && !"全部".equals(x)).collect(Collectors.toList());
|
|
|
+ for (String ct : cits) {
|
|
|
+ b.must(Query.of(y -> y.match(MatchQuery.of(z -> z.field("city").query(ct)))));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return b;
|
|
|
+ }));
|
|
|
+
|
|
|
+ builder.query(query);
|
|
|
+ builder.sort(SortOptions.of(so -> so.score(s -> s.order(SortOrder.Desc))), SortOptions.of(
|
|
|
+ so -> so.field(fs -> fs.field("offset").order(SortOrder.Desc))
|
|
|
+ )).minScore(10D);
|
|
|
|
|
|
SearchRequest searchRequest = builder.build();
|
|
|
|