|
|
@@ -3,14 +3,15 @@ package cn.reghao.tnb.search.app.es;
|
|
|
import cn.reghao.tnb.search.app.model.po.VideoText;
|
|
|
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
|
|
import co.elastic.clients.elasticsearch._types.FieldValue;
|
|
|
-import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
|
|
|
-import co.elastic.clients.elasticsearch._types.query_dsl.Query;
|
|
|
-import co.elastic.clients.elasticsearch._types.query_dsl.QueryStringQuery;
|
|
|
+import co.elastic.clients.elasticsearch._types.SortOrder;
|
|
|
+import co.elastic.clients.elasticsearch._types.query_dsl.*;
|
|
|
import co.elastic.clients.elasticsearch.core.SearchRequest;
|
|
|
import co.elastic.clients.elasticsearch.core.SearchResponse;
|
|
|
import co.elastic.clients.elasticsearch.core.search.Highlight;
|
|
|
import co.elastic.clients.elasticsearch.core.search.HighlightField;
|
|
|
+import co.elastic.clients.elasticsearch.core.search.Hit;
|
|
|
import co.elastic.clients.elasticsearch.core.search.HitsMetadata;
|
|
|
+import co.elastic.clients.json.JsonData;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageImpl;
|
|
|
@@ -19,6 +20,7 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.lang.reflect.Field;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -30,14 +32,19 @@ import java.util.stream.Collectors;
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class VideoTextQuery {
|
|
|
- private String indexName = "videotext";
|
|
|
+ private String indexName = "video_text";
|
|
|
+ private int pageSize = 12;
|
|
|
private final ElasticsearchClient esClient;
|
|
|
|
|
|
public VideoTextQuery(ElasticService elasticService) {
|
|
|
this.esClient = elasticService.getElasticsearchClient();
|
|
|
}
|
|
|
|
|
|
- public Page<VideoText> queryWithHighlight(String keyword, List<Integer> scopes, int pn, int ps) {
|
|
|
+ public Page<VideoText> queryWithHighlight(String keyword, List<Integer> scopes, int pageNumber) {
|
|
|
+ if (pageNumber > 100) {
|
|
|
+ pageNumber = 100;
|
|
|
+ }
|
|
|
+
|
|
|
String fieldName = "title";
|
|
|
String fieldValue = keyword;
|
|
|
QueryStringQuery stringQuery = new QueryStringQuery.Builder()
|
|
|
@@ -48,8 +55,11 @@ public class VideoTextQuery {
|
|
|
|
|
|
BoolQuery.Builder builder = new BoolQuery.Builder();
|
|
|
for (int scope : scopes) {
|
|
|
+ // filter 表示 list 中的值全部都要满足
|
|
|
+ //builder.filter(m -> m.term(t -> t.field("scope").value(FieldValue.of(scope))));
|
|
|
builder.should(m -> m.term(t -> t.field("scope").value(FieldValue.of(scope))));
|
|
|
}
|
|
|
+ builder.minimumShouldMatch("1");
|
|
|
builder.must(m -> m.queryString(stringQuery));
|
|
|
BoolQuery boolQuery = builder.build();
|
|
|
Query query = new Query.Builder()
|
|
|
@@ -75,11 +85,11 @@ public class VideoTextQuery {
|
|
|
.build();
|
|
|
|
|
|
// 3.搜索请求
|
|
|
- int start = (pn-1)*ps;
|
|
|
+ int start = (pageNumber-1)*pageSize;
|
|
|
SearchRequest searchRequest = new SearchRequest.Builder()
|
|
|
.index(indexName)
|
|
|
.from(start)
|
|
|
- .size(ps)
|
|
|
+ .size(pageSize)
|
|
|
.query(query)
|
|
|
.highlight(highlight)
|
|
|
.build();
|
|
|
@@ -107,11 +117,52 @@ public class VideoTextQuery {
|
|
|
}
|
|
|
return mapper.source();
|
|
|
}).collect(Collectors.toList());
|
|
|
- return new PageImpl<>(list, PageRequest.of(pn-1, ps), total);
|
|
|
+
|
|
|
+ if (total > pageSize*100L) {
|
|
|
+ total = pageSize*100L;
|
|
|
+ }
|
|
|
+
|
|
|
+ return new PageImpl<>(list, PageRequest.of(pageNumber-1, pageSize), total);
|
|
|
} catch (IOException e) {
|
|
|
log.error("search By Query Highlight error: {}", e.getMessage());
|
|
|
}
|
|
|
|
|
|
return Page.empty();
|
|
|
}
|
|
|
+
|
|
|
+ public List<VideoText> queryByPage(int pageNumber) {
|
|
|
+ String sortField = "publishTime";
|
|
|
+ Query query = RangeQuery.of(r -> r.field("age").gte(JsonData.of(8)))._toQuery();
|
|
|
+ String fieldName = "host";
|
|
|
+ String searchText = "api.iquizoo.com";
|
|
|
+ Query matchQuery = MatchQuery.of(m -> m.field(fieldName).query(searchText))._toQuery();
|
|
|
+ Query matchQuery1 = MatchQuery.of(m -> m.field(fieldName).query(searchText))._toQuery();
|
|
|
+
|
|
|
+ Query matchAllQuery = new Query.Builder()
|
|
|
+ .matchAll(new MatchAllQuery.Builder().build())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ int start = (pageNumber-1)*pageSize;
|
|
|
+ SearchRequest searchRequest = SearchRequest.of(s -> s
|
|
|
+ .index(indexName)
|
|
|
+ .query(matchAllQuery)
|
|
|
+ .from(start)
|
|
|
+ .size(pageSize)
|
|
|
+ // 按指定字段降序排列
|
|
|
+ .sort(f -> f.field(o -> o.field(sortField).order(SortOrder.Desc)))
|
|
|
+ );
|
|
|
+
|
|
|
+ try {
|
|
|
+ SearchResponse<VideoText> searchResponse = esClient.search(searchRequest, VideoText.class);
|
|
|
+ /*List<Hit<NginxLog>> hits = searchResponse.hits().hits();
|
|
|
+ for (Hit<NginxLog> hit : hits) {
|
|
|
+ NginxLog product = hit.source();
|
|
|
+ log.info("search page result: {}", product);
|
|
|
+ }*/
|
|
|
+ return searchResponse.hits().hits().stream().map(Hit::source).collect(Collectors.toList());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("{}", e.getMessage());
|
|
|
+ }
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
}
|