lucene的代碼示範

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.FileSystems;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
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.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;



public class IndexFile {

        String indexFile = null;
        String sourceDir = null;

        public IndexFile(String sourceDir, String indexFile){
            this.sourceDir = sourceDir;
            this.indexFile = indexFile;
        }

    public  void index() throws Exception {

    //  RAMDirectory ramdirectory = new RAMDirectory(); //創建內存索引對象
        Directory directory = FSDirectory.open(FileSystems.getDefault().getPath(this.indexFile)); //創建磁盤目錄對象
        Analyzer analyzer = new StandardAnalyzer();  //創建分詞器
        IndexWriterConfig writerConfig = new IndexWriterConfig(analyzer);  //創建索引寫入對象控制
        IndexWriter writer = new IndexWriter(directory, writerConfig); //創建寫入對象
        this.writeDocuments(writer); //將文檔寫入對象
//      directory.close();
    }

    public void writeDocuments(IndexWriter writer) throws Exception{
        File directory = new File(sourceDir);
        File[] fileList = directory.listFiles();
        for(int i = 0 ; i < fileList.length; i++){
            String title = fileList[i].getName();
            String content= this.readFile(fileList[i]);
            System.out.println(content);
            System.out.println(title);
            Document doc = new Document();
            doc.add(new Field("name", title, TextField.TYPE_STORED));
            doc.add(new Field("address", this.indexFile, TextField.TYPE_STORED));
            doc.add(new Field("thing", content, TextField.TYPE_STORED));
            writer.addDocument(doc);
            System.out.println("add one document");
        }
        writer.close();

    }
    public String readFile(File file) throws Exception{
        BufferedReader br = null;
        String content = null;
        try{
            br = new BufferedReader(new FileReader(file));
            String line ;
            while ( (line = br.readLine()) != null){
                content += line;
            }
        }
        finally{
            if(br != null ){
                br.close();
            }
        }
        return content;
    }

    public void query(String queryStr) throws IOException, ParseException{
        Directory directory = FSDirectory.open(FileSystems.getDefault().getPath(this.indexFile));
        DirectoryReader directoryReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
        Analyzer analyzer = new StandardAnalyzer();
        QueryParser qp = new QueryParser("thing",  analyzer);
        qp.setDefaultOperator(QueryParser.AND_OPERATOR);
        Query query = qp.parse(queryStr);
//      Term term = new Term("name", queryStr);
//      Query query = new TermQuery(term);
        TopDocs topDocs = indexSearcher.search(query, 10);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        System.out.println( "檢索到" + topDocs.totalHits + "條記錄。");
        for( ScoreDoc scoreDoc : scoreDocs){
            Document document = indexSearcher.doc(scoreDoc.doc);
            System.out.println("title:" + document.getField("name").stringValue());
            System.out.println("content:" + document.getField("thing").stringValue());
        }
        directory.close();
    }
    public static void main(String[] agrs) throws Exception{
        String indexFileName  =  "/home/quincy1994/QA/LuceneIndex/data";
        String sourceDir =  "./MovieList";
        IndexFile indexFile = new IndexFile(sourceDir, indexFileName);
//      indexFile.index();
        System.out.println("nice!");
        String queryStr = "大城小事講的是什麼";
        indexFile.query(queryStr);
    }
}
發佈了60 篇原創文章 · 獲贊 34 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章