首页 > 代码库 > 创建索引和搜索

创建索引和搜索

package cn.dyg.luence;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
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.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * Created by Administrator on 2016/11/2.
 */
public class IndexUtil {

    private static final Logger logger = LoggerFactory.getLogger(IndexUtil.class);

    private static final String indexPath = "E:\\javaweb\\luenceRepository\\index";

    private IndexUtil() { }

    public static IndexReader getIndexReader() {

        logger.info("this is the indexReader ...");
        return null;
    }

    public static void setIndexWriter() {
        //1 创建Directory
//        Directory directory = new RAMDirectory();   //索引建立在内存中
        Directory directory = null;   //索引建立在磁盘中
        try {
            directory = FSDirectory.open(new File(indexPath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //2 创建IndexWriter
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
        IndexWriter indexWriter = null;
        try {
            indexWriter = new IndexWriter(directory, indexWriterConfig);
            //3 创建文档对象
            Document document = null;
            //4 为文档添加filed
            File fileList = new File("E:\\javaweb\\luenceRepository\\example");
            for (File file : fileList.listFiles()) {
                document = new Document();
                document.add(new Field("content", new FileReader(file)));
                document.add(new Field("name", file.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                document.add(new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                //5 通过IndexWriter 添加文档到索引中
                indexWriter.addDocument(document);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (indexWriter != null) {
                try {
                    indexWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void search() {
        //1 创建Directory
//        Directory directory = new RAMDirectory();   //索引建立在内存中
        Directory directory = null;   //索引建立在磁盘中
        try {
            directory = FSDirectory.open(new File(indexPath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //2 创建IndexReader
        IndexReader indexReader = null;
        try {
            indexReader = IndexReader.open(directory);
            //3 创建search
            IndexSearcher indexSearcher = new IndexSearcher(indexReader);
            //4 创建搜索的Query
            QueryParser queryParser = new QueryParser(
                    Version.LUCENE_35,
                    "content",
                    new StandardAnalyzer(Version.LUCENE_35)
            );
            //5 创建Query, 表示搜索域为content中包含java的文档
            Query query = queryParser.parse("3350527960");
            //6 根据searcher搜索并返回TopDocs
            int queryLimit = 10;
            TopDocs topDocs = indexSearcher.search(query, queryLimit);
            //7 根据topDocs获得scoreDocs
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;
            int i = 0;
            for (ScoreDoc scoreDoc : scoreDocs) {
                //8 根据searcher 和ScoreDoc获取document对象
                Document document = indexSearcher.doc(scoreDoc.doc);
                logger.info(i + document.get("path") + document.get("name"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            if (indexReader != null) {
                try {
                    indexReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



}

  

创建索引和搜索