📅  最后修改于: 2020-11-12 04:46:24             🧑  作者: Mango
索引过程是Lucene提供的核心功能之一。下图说明了索引编制过程和类的使用。 IndexWriter是索引过程中最重要的核心组件。
我们将包含字段的文档添加到IndexWriter,后者使用分析器分析文档,然后根据需要创建/打开/编辑索引,并将其存储/更新到Directory中。 IndexWriter用于更新或创建索引。它不用于读取索引。
现在,我们将通过一个基本示例向您展示逐步的过程,以开始理解索引过程。
创建一种从文本文件中获取Lucene文档的方法。
创建各种类型的字段,这些字段是包含键(作为名称)和值(作为要索引的内容)的键值对。
设置是否要分析的字段。在我们的情况下,仅要分析内容,因为它可以包含搜索操作中不需要的数据,例如a,am,are等。
将新创建的字段添加到文档对象,并将其返回给调用者方法。
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-
步骤1-创建IndexWriter的对象。
步骤2-创建一个Lucene目录,该目录应指向要存储索引的位置。
步骤3-初始化使用索引目录创建的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应用程序测试。
Step | Description |
---|---|
1 |
Create a project with a name LuceneFirstApplication under a package com.tutorialspoint.lucene as explained in the Lucene – First Application chapter. You can also use the project created in Lucene – First Application chapter as such for this chapter to understand the indexing process. |
2 |
Create LuceneConstants.java,TextFileFilter.java and Indexer.java as explained in the Lucene – First Application chapter. Keep the rest of the files unchanged. |
3 |
Create LuceneTester.java as mentioned below. |
4 |
Clean and build the application to make sure the business logic is working as per the requirements. |
此类用于提供要在整个示例应用程序中使用的各种常量。
package com.tutorialspoint.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;
}
此类用作.txt文件过滤器。
package com.tutorialspoint.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");
}
}
此类用于对原始数据建立索引,以便我们可以使用Lucene库对其进行搜索。
package com.tutorialspoint.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();
}
}
此类用于测试Lucene库的索引编制能力。
package com.tutorialspoint.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:\ Lucene \ Data中。测试数据。索引目录路径应创建为E:\ Lucene \ Index 。运行该程序后,您可以看到在该文件夹中创建的索引文件的列表。
一旦完成了源,原始数据,数据目录和索引目录的创建,就可以编译并运行程序了。为此,请保持LuceneTester.Java文件选项卡处于活动状态,并使用Eclipse IDE中可用的“运行”选项,或者使用Ctrl + F11编译并运行您的LuceneTester应用程序。如果您的应用程序成功运行,它将在Eclipse IDE的控制台中显示以下消息-
Indexing E:\Lucene\Data\record1.txt
Indexing E:\Lucene\Data\record10.txt
Indexing E:\Lucene\Data\record2.txt
Indexing E:\Lucene\Data\record3.txt
Indexing E:\Lucene\Data\record4.txt
Indexing E:\Lucene\Data\record5.txt
Indexing E:\Lucene\Data\record6.txt
Indexing E:\Lucene\Data\record7.txt
Indexing E:\Lucene\Data\record8.txt
Indexing E:\Lucene\Data\record9.txt
10 File indexed, time taken: 109 ms
成功运行程序后,您的索引目录中将包含以下内容: