lucene4.x搜索高亮显示

public void light01() {
        String txt = "我爱北京天安门,伟大领袖毛主席,指引我们向前进";
        try {
            //创建查询的query
            org.apache.lucene.search.Query query = new QueryParser(Version.LUCENE_45, "f", new MMSegAnalyzer()).parse("北京 伟大");
            QueryScorer queryScorer = new QueryScorer(query);
            Fragmenter fragmenter = new SimpleSpanFragmenter(queryScorer);
            //自定义标签
            Formatter formatter = new SimpleHTMLFormatter("<font color=red>", "</font>");
            //高亮
            org.apache.lucene.search.highlight.Highlighter highlighter = new Highlighter(formatter, queryScorer);
            highlighter.setTextFragmenter(fragmenter);
            System.out.println(highlighter.getBestFragment(new MMSegAnalyzer(), "f", txt));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

junit测试:

@Test
    public void test7(){
        SearchUtil searchUtil = new SearchUtil();
        searchUtil.light01();
    }



测试结果:


你可能感兴趣的:(Lucene,高亮)