A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 我是楠楠 于 2019-4-17 16:51 编辑

【郑州校区】Lucene的基本使用之创建索引的流程

1.1.1、创建索引的流程
图片16.png
图片17.png
1.1.2、代码实现
[AppleScript] 纯文本查看 复制代码
@Test

public void indexCreate() throws IOException {

// 创建文档对象

Document document = new Document();

// 添加字段,参数Field是一个接口,要new实现类的对象(StringField, TextField)

// StringField的实例化需要3个参数:1-字段名,2-字段值,3-是否保存到文档,Store.YES存储,NO不存储

document.add(new StringField("id", "1", Store.YES));

// TextField:创建索引并提供分词,StringField创建索引但不分词

document.add(new TextField("title", "谷歌地图之父跳槽FaceBook", Store.YES));


// 创建目录对象,指定索引库的存放位置;FSDirectory文件系统;RAMDirectory内存

Directory directory = FSDirectory.open(new File("C:\\tmp\\indexDir"));

// 创建分词器对象

Analyzer analyzer = new StandardAnalyzer();

// 创建索引写入器配置对象,第一个参数版本VerSion.LATEST,第一个参数分词器

IndexWriterConfig conf = new IndexWriterConfig(Version.LATEST, analyzer);

// 创建索引写入器

IndexWriter indexWriter = new IndexWriter(directory , conf);

// 向索引库写入文档对象

indexWriter.addDocument(document);

// 提交

indexWriter.commit();

// 关闭

indexWriter.close();

}
1.1.3、运行测试
图片18.png
传智播客·黑马程序员郑州校区地址
河南省郑州市 高新区长椿路11号大学科技园(西区)东门8号楼三层
联系电话 0371-56061160/61/62
来校路线  地铁一号线梧桐街站A口出

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马