1. 程式人生 > >Lucene筆記35-Lucene的擴充套件-高亮索引檔案

Lucene筆記35-Lucene的擴充套件-高亮索引檔案

一、對索引檔案新增高亮

現在我們有一些索引檔案,怎麼對這些索引檔案新增高亮呢?首先需要根據搜尋域和值找到這篇文件,然後通過文件獲取域值,再將域值交給highlighter進行加工並返回,這個時候,返回的值就是新增過高亮的了。

二、程式碼展示

package com.wsy;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
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.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

public class HighLightTest {
    public void search(String name) {
        Analyzer analyzer = new IKAnalyzer();
        try {
            Directory directory = FSDirectory.open(new File("E:\\Lucene\\IndexLibrary"));
            IndexSearcher indexSearcher = new IndexSearcher(IndexReader.open(directory));
            // 這裡只能接受fileName這一個域,所以下面highLight2()之後,content是null
            // QueryParser queryParser = new QueryParser(Version.LUCENE_35, "fileName", analyzer);
            // 使用MultiFieldQueryParser可以接受多個域,表示既查詢fileName,又查詢content,這時候,經過highLight2()之後,content就有值了
            MultiFieldQueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_35, new String[]{"fileName", "content"}, analyzer);
            Query query = queryParser.parse(name);
            TopDocs topDocs = indexSearcher.search(query, 10);
            for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
                Document document = indexSearcher.doc(scoreDoc.doc);
                String fileName = document.get("fileName");
                fileName = highLight2(analyzer, query, fileName, "fileName");
                System.out.println(fileName);
                String content = new Tika().parseToString(new File(document.get("path")));
                content = highLight2(analyzer, query, content, "content");
                System.out.println(content);
            }
            indexSearcher.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (InvalidTokenOffsetsException e) {
            e.printStackTrace();
        } catch (TikaException e) {
            e.printStackTrace();
        }
    }

    public String highLight2(Analyzer analyzer, Query query, String text, String field) throws IOException, InvalidTokenOffsetsException {
        QueryScorer queryScorer = new QueryScorer(query);
        Fragmenter fragmenter = new SimpleSpanFragmenter(queryScorer);
        Formatter formatter = new SimpleHTMLFormatter("<span>", "</span>");
        Highlighter highlighter = new Highlighter(formatter, queryScorer);
        highlighter.setTextFragmenter(fragmenter);
        String highlightString = highlighter.getBestFragment(analyzer, field, text);
        // 如果查詢的highlightString為null,表示在field中沒有查到,就返回原來的內容
        return highlightString == null ? text : highlightString;
    }

    public static void main(String[] args) {
        HighLightTest highLightTest = new HighLightTest();
        highLightTest.search("必須");
    }
}