Lucene索引過程

索引過程是Lucene提供的核心功能之一。下圖說明了索引過程和使用的類。IndexWriter是索引過程中最重要的和核心組件。

Indexing

添加文檔包含字段IndexWriter,該分析用分析儀分析文件,然後創建/根據需要並在目錄存儲/更新/打開/編輯索引。IndexWriter用於更新或創建索引。它不是用來讀取索引。

現在,展示一個循序漸進的過程,以獲得在索引過程的理解,使用一個基本的例子。

創建一個文檔

  • 創建一個方法來獲取從文本文件中獲得 Lucene 的文檔。

  • 創建各種類型的是含有鍵作爲名稱和值作爲內容被編入索引鍵值對字段。

  • 設置字段中進行分析或不設置。在我們的實例中,只有內容被分析,因爲它可能包含數據,諸如 a, am, are, an,它不要求在搜索操作等等。

  • 新創建的字段添加到文檔對象並返回給調用者的方法。

private Document getDocument(File file) throws IOException{ Document document = new Document(); //index file contents Field contentField = new Field(LuceneConstants.CONTENTS, new FileReader(file)); //index file name Field fileNameField = new Field(LuceneConstants.FILE_NAME, file.getName(), Field.Store.YES,Field.Index.NOT_ANALYZED); //index file path Field filePathField = new Field(LuceneConstants.FILE_PATH, file.getCanonicalPath(), Field.Store.YES,Field.Index.NOT_ANALYZED); document.add(contentField); document.add(fileNameField); document.add(filePathField); return document; }

創建IndexWriter

  • IndexWriter 類作爲它創建/在索引過程中更新指標的核心組成部分

  • 創建一個 IndexWriter 對象

  • 創建其應指向位置,其中索引是存儲一個lucene的目錄

  • 初始化索引目錄,有標準的分析版本信息和其他所需/可選參數創建 IndexWriter 對象

private IndexWriter writer; public Indexer(String indexDirectoryPath) throws IOException{ //this directory will contain the indexes Directory indexDirectory = FSDirectory.open(new File(indexDirectoryPath)); //create the indexer writer = new IndexWriter(indexDirectory, new StandardAnalyzer(Version.LUCENE_36),true, IndexWriter.MaxFieldLength.UNLIMITED); }

開始索引過程

private void indexFile(File file) throws IOException{ System.out.println("Indexing "+file.getCanonicalPath()); Document document = getDocument(file); writer.addDocument(document); }

應用程序示例

讓我們創建一個測試 Lucene 應用程序來測試索引過程。

步驟

描述

1

在 packagecom.yiibai.lucene 包下創建一個名稱 LuceneFirstApplication 項目用於解釋 Lucene - First Application chapter, 也可以使用 Lucene 的創建項目 -  在 First Application 章這樣本章理解索引過程。

2

創建LuceneConstants.java,TextFileFilter.java和 Indexer.java,其它的文件保存不變。

3

創建LuceneTester.java如下所述

4

清理和構建應用程序,以確保業務邏輯按要求

LuceneConstants.java

這個類是用來提供跨示例應用程序中使用的各種常量

package com.yiibai.lucene; public class LuceneConstants { public static final String CONTENTS="contents"; public static final String FILE_NAME="filename"; public static final String FILE_PATH="filepath"; public static final int MAX_SEARCH = 10; }

TextFileFilter.java

此類用於爲 .txt 文件過濾器

package com.yiibai.lucene; import java.io.File; import java.io.FileFilter; public class TextFileFilter implements FileFilter { @Override public boolean accept(File pathname) { return pathname.getName().toLowerCase().endsWith(".txt"); } }

Indexer.java

這個類是用於索引的原始數據,這樣就可以使用Lucene庫,使其可搜索。

package com.yiibai.lucene; import java.io.File; import java.io.FileFilter; import java.io.FileReader; import java.io.IOException; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; public class Indexer { private IndexWriter writer; public Indexer(String indexDirectoryPath) throws IOException{ //this directory will contain the indexes Directory indexDirectory = FSDirectory.open(new File(indexDirectoryPath)); //create the indexer writer = new IndexWriter(indexDirectory, new StandardAnalyzer(Version.LUCENE_36),true, IndexWriter.MaxFieldLength.UNLIMITED); } public void close() throws CorruptIndexException, IOException{ writer.close(); } private Document getDocument(File file) throws IOException{ Document document = new Document(); //index file contents Field contentField = new Field(LuceneConstants.CONTENTS, new FileReader(file)); //index file name Field fileNameField = new Field(LuceneConstants.FILE_NAME, file.getName(), Field.Store.YES,Field.Index.NOT_ANALYZED); //index file path Field filePathField = new Field(LuceneConstants.FILE_PATH, file.getCanonicalPath(), Field.Store.YES,Field.Index.NOT_ANALYZED); document.add(contentField); document.add(fileNameField); document.add(filePathField); return document; } private void indexFile(File file) throws IOException{ System.out.println("Indexing "+file.getCanonicalPath()); Document document = getDocument(file); writer.addDocument(document); } public int createIndex(String dataDirPath, FileFilter filter) throws IOException{ //get all files in the data directory File[] files = new File(dataDirPath).listFiles(); for (File file : files) { if(!file.isDirectory() && !file.isHidden() && file.exists() && file.canRead() && filter.accept(file) ){ indexFile(file); } } return writer.numDocs(); } }

LuceneTester.java

這個類是用來測試 Lucene 庫的索引能力。

package com.yiibai.lucene; import java.io.IOException; public class LuceneTester { String indexDir = "E:\Lucene\Index"; String dataDir = "E:\Lucene\Data"; Indexer indexer; public static void main(String[] args) { LuceneTester tester; try { tester = new LuceneTester(); tester.createIndex(); } catch (IOException e) { e.printStackTrace(); } } private void createIndex() throws IOException{ indexer = new Indexer(indexDir); int numIndexed; long startTime = System.currentTimeMillis(); numIndexed = indexer.createIndex(dataDir, new TextFileFilter()); long endTime = System.currentTimeMillis(); indexer.close(); System.out.println(numIndexed+" File indexed, time taken: " +(endTime-startTime)+" ms"); } }

數據和索引目錄的創建

從 record1.txt 命名文件 record10.txt 包含簡單的名稱以及學生的其他細節,並把它們放在目錄 E:LuceneData.  索引目錄路徑應創建爲 E:LuceneIndex. 運行此程序後,就可以看到該文件夾中創建的索引文件的列表。

運行程序:

一旦使用創建源,創造了原始數據,數據目錄和索引目錄來完成,準備好這一步然後編譯和運行程序。要做到這一點,保存LuceneTester.Java文件選項卡中使用Eclipse IDE 運行 Run 選項,或使用Ctrl+ F11來編譯和運行應用程序LuceneTester。如果應用程序一切正常,這將打印在Eclipse IDE的控制檯以下消息:

Indexing E:LuceneData
ecord1.txt
Indexing E:LuceneData
ecord10.txt
Indexing E:LuceneData
ecord2.txt
Indexing E:LuceneData
ecord3.txt
Indexing E:LuceneData
ecord4.txt
Indexing E:LuceneData
ecord5.txt
Indexing E:LuceneData
ecord6.txt
Indexing E:LuceneData
ecord7.txt
Indexing E:LuceneData
ecord8.txt
Indexing E:LuceneData
ecord9.txt
10 File indexed, time taken: 109 ms

一旦成功地運行程序,將有以下的索引目錄中的內容:

Lucene