全文检索(二)-基于lucene4.10的增删改查(三)
ssWarnings("deprecation") public static void query() { // 1.创建Directory 在硬盘上的F:/luence/index下建立索引 try { IndexReader indexReader = IndexReader.open(FSDirectory .open(new File(indexPath))); System.out.println("存储的文档数:" + indexReader.numDocs()); System.out.println("总存储量:" + indexReader.maxDoc()); System.out.println("被删除的文档:" + indexReader.numDeletedDocs()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 回滚回收站 * * @throws IOException */ public void recoveryIndexByIsDelete() throws IOException { IndexWriter indexWriter = getIndexWriter(new IKAnalyzer()); indexWriter.rollback(); destroyWriter(indexWriter); } /** * 清空回收站 在版本3.6之后,已经没有了unDeleteAll()方法了 * * @throws IOException */ public static void forceDeleteIndex() throws IOException { IndexWriter indexWriter = getIndexWriter(new IKAnalyzer()); indexWriter.forceMergeDeletes(); destroyWriter(indexWriter); } /** * 更新索引 * * @throws IOException */ public void update() throws IOException { IndexWriter indexWriter = new IndexWriter(FSDirectory.open(new File( indexPath)), new IndexWriterConfig(Version.LATEST, new IKAnalyzer(true))); Document document = new Document(); document.add(new Field("id", "10", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); document.add(new Field("email", "9481629991", Field.Store.YES, Field.Index.NOT_ANALYZED)); document.add(new Field("name", "小米", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); document.add(new Field("content", "小米好", Field.Store.NO, Field.Index.ANALYZED)); // 这里的更新,从方法上可以看出,它实际上时将旧的删除,然后添加一个新文档的进去,将匹配到term的文档删除,然后就新的document添加进去 indexWriter.updateDocument(new Term("id", "1"), document); indexWriter.close(); } }