可以通过Lucene实现全文中重要的组成部分——索引文件的创建。
创建索引基本流程
- 选择一个文件夹,作为索引输出的目录。
- 创建索引输出流对象
- 读取数据源,封装document对象,根据域属性定义文档对象的属性。
- 通过输出流对象,将讲过倒排索引计算的结果输出到指定目录下的目录中,完成索引的创建。
代码演示
注释写的比较详细,就不过多解释了:
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class CreateIndexTest {
@Test
public void createIndex() throws IOException {
//指定一个文件输出目录
Path path= Paths.get("d:/test/index"