Lucene添加文檔操作

添加文件是核心的操作,索引進程的一部分中的一個。

添加包含字段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的目錄。

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

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); }

添加文件,並開始索引過程

下面的兩個是添加的文件的方式。

  • addDocument(Document) - 添加使用默認的分析文檔(在創建索引時,指定寫入器)

  • addDocument(Document,Analyzer) - 添加使用提供的分析文檔。

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  也可以使用 EJB 創建的項目 - 第一章申請這樣本章理解索引過程。

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"); } }

數據和索引目錄的創建

使用10個文件從 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