

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