Lucene中文分词以及关键字的高亮

1 篇文章 0 订阅
1 篇文章 0 订阅

    渲染在页面的关键字高亮, 以及适用于国内形式的中文分词器.

    需要引用的jar 看Lucene的第一章的pom.xml

package com.zero.lucene;

import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.search.highlight.SimpleSpanFragmenter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

/**
 * 搜索的时候 关键字红色
 * @author samuel
 *
 */
public class SearcherColor {

    private IndexReader indexReader;
    private IndexSearcher indexSearcher;

    /**
     * 搜索索引
     * @throws Exception
     */
    public void searcher() throws Exception {
        String uri = "";
        Directory directory = FSDirectory.open(Paths.get(uri));
        indexReader = DirectoryReader.open(directory);
        indexSearcher = new IndexSearcher(indexReader);
        // 中文分词
        Analyzer analyer = new SmartChineseAnalyzer();
        QueryParser parser = new QueryParser("title", analyer);
        Query query = parser.parse("pwd.txt");
        TopDocs topDocs = indexSearcher.search(query, 10);

        // 高亮部分

        QueryScorer queryScorer = new QueryScorer(query);
        // 设置关键的高亮度字体和颜色
        Formatter formatter = new SimpleHTMLFormatter("<b><font color='red'>", "</font></b>");
        Highlighter highligther = new Highlighter(formatter, queryScorer);
        Fragmenter fragmentScorer = new SimpleSpanFragmenter(queryScorer);
        highligther.setTextFragmenter(fragmentScorer);

        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
            Document doc = indexReader.document(scoreDoc.doc);
            String title = doc.get("title");
            if (null != title) {
                // 拿到最高分数的内容片断
                TokenStream tokenStream = analyer.tokenStream("title", "hahahah");
                String bestSpan = highligther.getBestFragment(tokenStream, title);
                System.out.println(bestSpan);
            }
        }


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值