Lucene的基本使用之创建索引的流程

匿名 (未验证) 提交于 2019-12-02 23:35:02


@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、运行测试

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!