SpringBoot+elastisearch7.9高亮搜索信息

            SpringBoot+elastisearch7.9高亮搜索信息
            //分页参数
	        Pageable pageable = PageRequest.of(pageNum, pageSize);
	        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
	        //限制返回数据信息
	        nativeSearchQueryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{"id", "filetype","fileName","attachment.content_type"}, null));
	        //分页
	        nativeSearchQueryBuilder.withPageable(pageable);
	        //搜索
	        if (StringUtils.isEmpty(keyword)) {   //全部查询  
	                 nativeSearchQueryBuilder.withQuery(QueryBuilders.matchAllQuery());
	        } else {
	        	// 构建高亮查询
	        	HighlightBuilder.Field field = new HighlightBuilder.Field("fileName").preTags("<font style='color:red'>").postTags("</font>");
	        	nativeSearchQueryBuilder.withHighlightFields(field);  // 名字高亮
	        	 // 构建布尔查询
	        	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	        	boolQueryBuilder.should(QueryBuilders.matchQuery("fileName", keyword));
	        	// or
	        	boolQueryBuilder.should(QueryBuilders.matchQuery("attachment.content", keyword));
	        	nativeSearchQueryBuilder.withQuery(boolQueryBuilder);
	        }
            //排序	        
            nativeSearchQueryBuilder.withSort(SortBuilders.scoreSort().order(SortOrder.DESC));
	        NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
	        //执行查询
	        SearchHits<FileWord> kilst=elasticsearchRestTemplate.search(searchQuery, FileWord.class);
	        //创建list对象
	        List<FileWord> filewordlist=new ArrayList<FileWord>();
	        kilst.forEach(item -> {
	        	FileWord content = item.getContent();
	        	//获取高亮信息
	        	List<String> filename=item.getHighlightField("fileName");
	        	if(filename.size()>0) {
	        	  //替换原来字段内容
	        		content.setFileName(filename.get(0));
	        	}
	        	filewordlist.add(content);
	        });
	        // 组装分页对象
	        Page<FileWord> pagelist = new PageImpl<>(filewordlist,pageable,kilst.getTotalHits());
	        return pagelist;