es counts the number of highlight hits

Problem Description

`1.es Count the number of highlight hits.
When using es, only the number of highlighted results will be returned, but the number of
times this field has been hit will not be returned, for example.
insert image description here
The first message in Baidu was hit 3 times.
es highlights the return body, searches for two words of information, and hits two words and tags them separately.
1.1 The first case

<em></em><em></em>

At this time, the hit information can only be counted as a hit once, and there is another situation that is
1.2 The second situation

<em></em>

Only hit a single word. At this time, you can use regular statistics to count how many hits.

solution:

The solution is as follows:

    /**
     * 统计高亮次数,<em></em><em></em>算一次
     *
     * @return
     */
    private int countHits(String temp) {
    
    
        int num = 0;
        if (StringUtils.isEmpty(temp)) {
    
    
            return num;
        }
        String regex = "((<em>[^</]*</em>)+)";
        Pattern p1 = Pattern.compile(regex);
// 指定要匹配的内容
        Matcher m = p1.matcher(temp);
        while (m.find()) {
    
    
            num++;
        }
        return num;
    }

Here is an explanation of the regular meaning
(( )+) meaning to match the tag one or more times, which also covers the above two cases 1.1 and 1.2. "[^</]*" means that the content in the tag is not </, Otherwise anything is only matched once.

Summarize

If it can help you, you can give it a thumbs up, it is feasible after testing,
and

DisMaxQueryBuilder disMaxQueryBuilder = QueryBuilders.disMaxQuery();
disMaxQueryBuilder.add(QueryBuilders.matchQuery("a", search));
disMaxQueryBuilder.add(QueryBuilders.matchQuery("b", search));
disMaxQueryBuilder.add(QueryBuilders.matchQuery("c", search));

DisMaxQueryBuilder can use dismaxquery to query multiple fields. There is a competitive relationship between multiple fields. If the returned scores are high and hit at the same time, it is more suitable for most scenarios.

Guess you like

Origin blog.csdn.net/qq_34526237/article/details/127329394