|
|
@@ -32,8 +32,8 @@ import java.util.stream.Collectors;
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class VideoTextQuery {
|
|
|
- private String indexName = "video_text";
|
|
|
- private int pageSize = 12;
|
|
|
+ private final String indexName = "video_text";
|
|
|
+ private final int pageSize = 12;
|
|
|
private final ElasticsearchClient esClient;
|
|
|
|
|
|
public VideoTextQuery(ElasticService elasticService) {
|
|
|
@@ -84,6 +84,7 @@ public class VideoTextQuery {
|
|
|
.requireFieldMatch(false)
|
|
|
.build();
|
|
|
|
|
|
+ String sortField = "publishTime";
|
|
|
// 3.搜索请求
|
|
|
int start = (pageNumber-1)*pageSize;
|
|
|
SearchRequest searchRequest = new SearchRequest.Builder()
|
|
|
@@ -92,21 +93,24 @@ public class VideoTextQuery {
|
|
|
.size(pageSize)
|
|
|
.query(query)
|
|
|
.highlight(highlight)
|
|
|
+ //.sort(f -> f.field(o -> o.field(sortField).order(SortOrder.Desc)))
|
|
|
.build();
|
|
|
try {
|
|
|
SearchResponse<VideoText> searchResponse = esClient.search(searchRequest, VideoText.class);
|
|
|
HitsMetadata<VideoText> hitsMetadata = searchResponse.hits();
|
|
|
long total = hitsMetadata.total().value();
|
|
|
- List<VideoText> list = hitsMetadata.hits().stream().map(mapper -> {
|
|
|
- Map<String, List<String>> highlightMap = mapper.highlight();
|
|
|
+ List<VideoText> list = hitsMetadata.hits().stream().map(hit -> {
|
|
|
+ double score = hit.score() != null ? hit.score() : 0.0;
|
|
|
+ log.info("score -> {}", score);
|
|
|
+ Map<String, List<String>> highlightMap = hit.highlight();
|
|
|
String highlightStr = "";
|
|
|
if (!highlightMap.isEmpty()) {
|
|
|
- highlightStr = mapper.highlight()
|
|
|
+ highlightStr = hit.highlight()
|
|
|
.get(highlightFieldName)
|
|
|
.get(0);
|
|
|
}
|
|
|
|
|
|
- VideoText videoText = mapper.source();
|
|
|
+ VideoText videoText = hit.source();
|
|
|
try {
|
|
|
Class<?> clazz = videoText.getClass();
|
|
|
Field field = clazz.getDeclaredField(highlightFieldName);
|
|
|
@@ -115,7 +119,7 @@ public class VideoTextQuery {
|
|
|
} catch (Exception e) {
|
|
|
log.error(e.getMessage());
|
|
|
}
|
|
|
- return mapper.source();
|
|
|
+ return hit.source();
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
if (total > pageSize*100L) {
|
|
|
@@ -131,17 +135,15 @@ public class VideoTextQuery {
|
|
|
}
|
|
|
|
|
|
public List<VideoText> queryByPage(int pageNumber) {
|
|
|
- String sortField = "publishTime";
|
|
|
- Query query = RangeQuery.of(r -> r.field("age").gte(JsonData.of(8)))._toQuery();
|
|
|
+ Query rangeQuery = 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();
|
|
|
|
|
|
+ String sortField = "publishTime";
|
|
|
int start = (pageNumber-1)*pageSize;
|
|
|
SearchRequest searchRequest = SearchRequest.of(s -> s
|
|
|
.index(indexName)
|
|
|
@@ -154,11 +156,6 @@ public class VideoTextQuery {
|
|
|
|
|
|
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());
|