Lucene學習(1)

由於我們學校這期課程需要做一個基於源碼工具包搭建一個搜索引擎,上課的老師強力推薦使用lucene工具並且讀一讀lucene源碼。小編準備先把搜索引擎這個任務完成,學習lucene的使用,然後有時間的時候研究一下lucene這個優秀的源碼。

1.lucene學習框架

這裏寫圖片描述

2.基本的創建索引和搜索框架

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Paths;

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.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;

public class HelloLucene {

    /**
     * 建立索引
     * 
     * @throws IOException
     */

    public void index() throws IOException {
        // 1.創建directory(索引創建在哪呢?)
        // 2.創建IndexWriter(對象來寫索引)
        // 3.創建Document對象(索引一篇文檔?索引一個。。。?
        // 4.爲Document添加Field
        // 5.通過indexWriter添加文檔的索引
        //Directory directory = new RAMDirectory();// 在內存中創建索引
        Directory directory=FSDirectory.open(Paths.get("d:/lucene",new String[]{"index"}));
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(analyzer);// 對寫索引進行配置的對象
        IndexWriter writer = new IndexWriter(directory, config);// 寫入索引對象,用完之後需要關閉
        Document doc = null;
        File f = new File("d:/lucene/index_sample");
        File[] ff = f.listFiles();

        for (int i = 0; i < ff.length; i++) {
            doc = new Document();
            doc.add(new Field("filename", ff[i].getName(),
                    TextField.TYPE_STORED));
            doc.add(new Field("content", readFile(ff[i]), TextField.TYPE_STORED));

            doc.add(new Field("filepath", ff[i].getAbsolutePath(),
                    TextField.TYPE_STORED));
            writer.addDocument(doc);// 通過IndexWriter添加文檔到索引中

        }
        writer.close();//關閉IndexWriter之後才能看見索引創建效果

    }

    private static String readFile(File file) throws IOException {
        StringBuffer content = new StringBuffer();
        try {
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    new FileInputStream(file)));
            for (String line = null; (line = bf.readLine()) != null;) {
                content.append(line).append("\n");
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return content.toString();
    }

    public void searcher(String keyword) throws ParseException{
        //1.創建Directory
        //2.創建IndexReader
        //3.根據IndexReader創建InderSearcher
        //4.創建搜索的Query
        //5.根據searcher搜索並返回TopDocs
        //6.根據searcher和ScoreDoc對象獲取具體的Document對象
        //7.根據Document對象獲取需要的值
        try {
            Directory directory=FSDirectory.open(Paths.get("d:/lucene",new String[]{"index"}));
            DirectoryReader  reader=DirectoryReader.open(directory);
            IndexSearcher searcher=new IndexSearcher(reader);
            QueryParser parser=new QueryParser("content",new StandardAnalyzer());
            Query query=parser.parse(keyword);
            TopDocs topDocs=searcher.search(query,10);
            System.out.println("查找到的文檔總共有:"+topDocs.totalHits);
            ScoreDoc[] scoreDocs=topDocs.scoreDocs;
            for(int i=0;i<scoreDocs.length;i++){
                Document doc=new Document();
                doc=searcher.doc(scoreDocs[i].doc);
                System.out.println(doc.get("filename")+doc.get("filepath"));

            }




        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException, ParseException {
        HelloLucene lucene = new HelloLucene();
        lucene.index();
        lucene.searcher("設置");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章