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();
        }
    }

}
原文地址:https://www.cnblogs.com/yangh2016/p/5787632.html