Lucene索引的初步建立

來源:互聯網
上載者:User

標籤:view   dir   director   baidu   代碼   gen   支援   建立   ima   

  從百度上知道的,Lucene是apache軟體基金會4 jakarta項目組的一個子項目,是一個開放原始碼的全文檢索索引引擎工具包,但它不是一個完整的全文檢索索引引擎,而是一個全文檢索索引引擎的架構,提供了完整的查詢引擎和索引引擎,部分文本分析引擎(英文與德文兩種西方語言)。Lucene的目的是為軟體開發人員提供一個簡單易用的工具包,以方便的在目標系統中實現全文檢索索引的功能,或者是以此為基礎建立起完整的全文檢索索引引擎。Lucene是一套用於全文檢索索引和搜尋的開源程式庫,由Apache軟體基金會支援和提供。Lucene提供了一個簡單卻強大的應用程式介面,能夠做全文索引和搜尋。在Java開發環境裡Lucene是一個成熟的免費開源工具。就其本身而言,Lucene是當前以及最近幾年最受歡迎的免費Java資訊檢索程式庫。人們經常提到資訊檢索程式庫,雖然與搜尋引擎有關,但不應該將資訊檢索程式庫與搜尋引擎相混淆。

 

用的是Lucene 5.5.0版本,http://archive.apache.org/dist/lucene/java/5.5.0/

一,下載後,需要找到Lucene需要的核心jar包

lucene-analyzers-common-5.5.0.jarlucene-core-5.5.0.jarlucene-queryparser-5.5.0.jar

找到核心jar包後,匯入到開發項目的lib下。

二、建立索引的步驟

1、建立Directory

2、建立IndexWriter

3、建立Document對象

4、往Document添加Field

5、通過IndexWriter添加文檔到索引中

三、執行個體開發

其中:FSDirectory.open(Paths.get("D://lucene//index"));  是將索引建立在自己電腦上硬碟上。

        如果建立在記憶體中,就可以使用Directory directory = new RAMDirectory();//建立在記憶體中

   /**     * 建立索引     */    public void createIndex(){        //1,建立Directory        Directory directory = null;        //2,建立IndexWriter        Analyzer analyzer = new StandardAnalyzer();        IndexWriterConfig config = new IndexWriterConfig(analyzer);        IndexWriter indexWriter = null;        try {            directory = FSDirectory.open(Paths.get("D://lucene//index"));              indexWriter = new IndexWriter(directory, config);            //3,建立Document對象            Document doc = null;            //4,位Document添加Field            File f = new File("D://text");            for(File file:f.listFiles()){                doc = new Document();                @SuppressWarnings("deprecation")                Field content = new Field("content", new FileReader(file));                @SuppressWarnings("deprecation")                Field filename = new Field("filename", file.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED);                @SuppressWarnings("deprecation")                Field pathFile = new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED);                doc.add(content);                doc.add(filename);                doc.add(pathFile);                //5,通過IndexWriter添加文檔到索引中                indexWriter.addDocument(doc);            }                    } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally{            if(indexWriter != null)                try {                    indexWriter.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }        }            }

執行後,可以看到索引檔案:

 

 OK,索引就建好了。

 

Lucene索引的初步建立

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.