ElasticSearchg添加高亮后,文本显示不全问题

直接看代码的最后片段

public NativeSearchQuery getNativeSearchQuery(ProcessLog condition, PageParam pageParam) {
     
        Sort sortBy = Sort.by(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, pageParam.getSortCondition())).descending();
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        if (StringUtils.equalsIgnoreCase("asc", pageParam.getSortModel())) {
     
            sortBy = sortBy.ascending();
        }
        Pageable pageable = PageRequest.of(pageParam.getPageNum() - 1, pageParam.getPageSize(), sortBy);

        if (StringUtils.isEmpty(pageParam.getSortCondition())) {
     
            pageParam.setSortCondition("requestTime");
        }
        BoolQueryBuilder boolQueryBuilder = boolQuery();

        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();

        if (StringUtils.isNotEmpty(condition.getUser())) {
     
            boolQueryBuilder.must(matchPhraseQuery("user", condition.getUser()));
        }

        if (StringUtils.isNotEmpty(condition.getRequestUri())) {
     
            boolQueryBuilder.must(matchPhraseQuery("requestUri", condition.getRequestUri()));
        }

        if (StringUtils.isNotEmpty(condition.getParams())) {
     
            String params = condition.getParams();
            String[] paramArray = params.split(",");
            for (String param : paramArray) {
     
                boolQueryBuilder.must(matchPhraseQuery("params", param));
            }
        }

        if (StringUtils.isNotEmpty(condition.getEmail())) {
     
            boolQueryBuilder.must(matchPhraseQuery("email", condition.getEmail()));
        }

        if (StringUtils.isNotEmpty(condition.getPeerId())) {
     
            boolQueryBuilder.must(matchPhraseQuery("peerId", condition.getPeerId()));
        }

        if (StringUtils.isNotEmpty(condition.getServerName())) {
     
            boolQueryBuilder.must(matchPhraseQuery("serverName.keyword", condition.getServerName()));
        }

        if (condition.getStatus() != null) {
     
            boolQueryBuilder.must(matchPhraseQuery("status", condition.getStatus()));
        }

        if (StringUtils.isNotEmpty(condition.getMethod())) {
     
            boolQueryBuilder.must(matchPhraseQuery("method", condition.getMethod()));
        }

        if (condition.getStartTime() != null && condition.getEndTime() != null) {
     
            boolQueryBuilder.must(
                    rangeQuery("requestTime")
                            .gte(condition.getStartTime())
                            .lt(condition.getEndTime())
            );
        }
        nativeSearchQueryBuilder.withQuery(boolQueryBuilder);
        nativeSearchQueryBuilder.withHighlightBuilder(new HighlightBuilder()
                .field("id")
                .field("email")
                .field("serverName")
                .field("method")
                .field("requestUri")
                .field("params")
                .field("peerId")
                .field("status")
                .preTags("")
                .postTags("")
                 //注意加上高亮片数即可
                .fragmentSize(800000)
                .numOfFragments(0)
                .requireFieldMatch(false))
                .withPageable(pageable);
        return nativeSearchQueryBuilder.build();
    }

你可能感兴趣的:(ElasticSearch)