Lucene多欄位查詢&高亮顯示

Bart_G發表於2017-04-08
  • 在百度搜尋的時候,查詢的關鍵詞會高亮顯示
  • 在搜尋一個關鍵詞的時候,有可能這個關鍵詞在title和content中,搜尋的時候要把結果全部顯示出來

例項說明

package com.bart.lucene.mutilseacher;

import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.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.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.Scorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.junit.Test;
import com.bart.lucene.entity.Article;
import com.bart.lucene.util.LuceneUtils;


/**
 * Article多條件搜尋加高亮顯示
 * @author hp
 */
public class ArticleDao {

    /**
     * 高亮顯示
     * 查詢索引庫中所有的記錄
     * @throws Exception 
     */
    @Test
    public void findAll() throws Exception{
        String keyword = "顯示卡";
        List<Article>articleList = new ArrayList<Article>(); 
        IndexSearcher indexSearcher = new  IndexSearcher(LuceneUtils.getDirectory());
        //預設之搜尋一個欄位,單條件搜尋
        //QueryParser queryParser = new QueryParser(LuceneUtils.getVersion(),"content",LuceneUtils.getAnalyzer());
        //搜尋多個欄位,多條件搜尋
        QueryParser queryParser2 = new MultiFieldQueryParser(LuceneUtils.getVersion(),new String[]{"title","content"},LuceneUtils.getAnalyzer());

        //Query query = queryParser.parse(keyword);
        Query query = queryParser2.parse(keyword);
        TopDocs topDocs = indexSearcher.search(query,100);

        //以下程式碼對查詢結果進行高亮顯示
        // 1.格式化物件,設定字首和字尾
        Formatter formatter = new SimpleHTMLFormatter("<font color='red'>","</font>");
        // 2.關鍵詞物件
        Scorer scorer = new QueryScorer(query);
        // 3. 高亮物件
        Highlighter highlighter = new Highlighter(formatter, scorer);


        for(int i=0;i<topDocs.scoreDocs.length;i++){
            ScoreDoc scoreDoc = topDocs.scoreDocs[i];
            int no = scoreDoc.doc;

            //關鍵詞沒有高亮
            Document document = indexSearcher.doc(no);

            // 1. 關鍵詞新增高亮
            String titleHighLight  = highlighter.getBestFragment(LuceneUtils.getAnalyzer(),"title",document.get("title"));
            String contentHighLight  = highlighter.getBestFragment(LuceneUtils.getAnalyzer(),"content",document.get("content"));
            // 2. 將高亮的結果再次封裝到document物件中
            document.getField("title").setValue(titleHighLight);
            document.getField("content").setValue(contentHighLight);

            Article article = LuceneUtils.document2JavaBean(document,Article.class);
            articleList.add(article);
        }

        //遍歷
        for(Article a : articleList){
            System.out.println(a);
        }
    }

    /**
     * 增加document物件到物件索引庫中
     * @throws Exception
     */
    @Test
    public void testCreate() throws Exception{
        Article article = new Article(1,"顯示卡","顯示卡是電腦的核心部件",10);
        Document document = LuceneUtils.javaBean2Document(article);
        IndexWriter indexWriter = new IndexWriter(LuceneUtils.getDirectory(), LuceneUtils.getAnalyzer(), LuceneUtils.getMaxFieldLength());
        indexWriter.addDocument(document);
        //設定當插入三條記錄的時候合併到同一條記錄,預設是10個快取就合併索引的cfs檔案
        indexWriter.setMergeFactor(3);
//        //合併記錄,不管插入多少條
//        indexWriter.optimize();
        indexWriter.close();
    }


}

輸出結果:
Article [id=1, title=, content=是電腦的核心部件, count=10]

相關文章