2 讀取solr下的索引文件(lucene文件)


import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.queryparser.classic.ParseException;
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.store.Directory;
import org.apache.lucene.store.FSDirectory;


import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;

/**
 * 直接讀取lucene文件內容
 */
public class ReadLuceneFileTest {

    public static void main(String[] args) {
        test1();
    }

    public static void test1(){
        //得到讀取索引文件的路徑
        Directory dir= null;
        try {
            dir = FSDirectory.open(Paths.get("F:\\index"));
        } catch (IOException e) {
            System.out.println("IOException of open file,異常信息:"+e.getMessage());
            e.printStackTrace();
        }

        //通過dir得到的路徑下的所有的文件
        IndexReader reader= null;
        try {
            reader = DirectoryReader.open(dir);
        } catch (IOException e) {
            System.out.println("發生獲取文件目錄內容異常,異常信息:"+e.getMessage());
            e.printStackTrace();
        }

        //公共是多少文件,也就是最大文檔數
        System.out.println("最大文檔數:"+reader.maxDoc());

        //讀取的實際文檔數
        System.out.println("實際文檔數:"+reader.numDocs());
        //建立索引查詢器
        IndexSearcher is=new IndexSearcher(reader);
        //實例化分析器
        Analyzer analyzer=new StandardAnalyzer();
        //建立查詢解析器
        /**
         * 第一個參數是要查詢的字段;
         * 第二個參數是分析器Analyzer
         * */
        QueryParser parser=new QueryParser("DOC_ID", analyzer);

        //根據傳進來的p查找
        Query query= null;
        try {
            query = parser.parse("*:*");
        } catch (ParseException e) {
            System.out.println("解析異常,異常信息:"+e.getMessage());
            e.printStackTrace();
        }
        //計算索引開始時間
        long start=System.currentTimeMillis();
        //開始查詢
        /**
         * 第一個參數是通過傳過來的參數來查找得到的query;
         * 第二個參數是要出查詢的行數
         * */
        TopDocs hits= null;
        try {
            hits = is.search(query, 5);
        } catch (IOException e) {
            System.out.println("搜索異常,異常信息:"+e.getMessage());
            e.printStackTrace();
        }
        //計算索引結束時間
        long end=System.currentTimeMillis();

        System.out.println("匹配 "+"DOC_ID"+" ,總共花費"+(end-start)+"毫秒"+"查詢到"+hits.totalHits+"個記錄");

        //遍歷hits.scoreDocs,得到scoreDoc
        /**
         * ScoreDoc:得分文檔,即得到文檔
         * scoreDocs:代表的是topDocs這個文檔數組
         * @throws Exception
         * */
        for(ScoreDoc scoreDoc:hits.scoreDocs){
            Document doc= null;
            try {
                doc = is.doc(scoreDoc.doc);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(doc);
            List<IndexableField> list= doc.getFields();
            for (IndexableField indexableField : list) {
                System.out.println(indexableField.name()+" : "+doc.get(indexableField.name()));
            }
            System.out.println("****************");
        }
        //關閉reader
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM