(三)Lucene之删除更新文档以及luke的基本使用

一、demo

  • 本例中采用单元测试,故在pom.xml中引入junit jar包
  • 1.1  前提:
public class IndexTest {

/**
 *数据准备
 */
private String ids[] = { "1", "2", "3" }; private String citys[] = { "qingdao", "nanjing", "shanghai" }; private String descs[] = { "Qingdao is a beautiful city.", "Nanjing is a city of culture.", "Shanghai is a bustling city." }; @Before public void setUp() throws IOException { IndexWriter indexWriter = getIndexWiter(); for (int i = 0; i < ids.length; i++) { Document document = new Document(); document.add(new StringField("id", ids[i], Field.Store.YES)); document.add(new StringField("city", citys[i], Field.Store.YES)); document.add(new StringField("desc", descs[i], Field.Store.NO)); indexWriter.addDocument(document); } indexWriter.close(); } /** * 实例化IndexWiter * * @return * @throws IOException */ private IndexWriter getIndexWiter() throws IOException { Directory dir = FSDirectory.open(Paths.get("E:\\lucene2")); Analyzer analyzer = new StandardAnalyzer(); IndexWriterConfig conf = new IndexWriterConfig(analyzer); IndexWriter indexWriter = new IndexWriter(dir, conf); return indexWriter; } }
  •      1.2      测试写入

    /**
     * 测试写了几个文档
     * 
     * @throws IOException
     */
    @Test
    public void tesWritert() throws IOException {
        IndexWriter indexWriter = getIndexWiter();

        System.out.println("一共写了" + indexWriter.numDocs() + "个文档");
        indexWriter.close();
    }
    

结果:

    •   使用luke查看索引文件 

 

 

   1.3  测试读取

  • 由于进行了1.2 测试写入,所以要把索引文件清理一下,因为此步骤也会产生索引文件

 

    /**
     * 测试读取了多少文档
     * @throws IOException 
     */
    @Test
    public void testReader() throws IOException {
        IndexWriter indexWriter=getIndexWiter();
        System.out.println("最大文档数为:"+indexWriter.maxDoc());
        System.out.println("当前文档数为:"+indexWriter.numDocs());
        indexWriter.close();
    }

结果:

 

   1.4  测试删除 在合并前

  •  由于进行了1.3 测试写入,所以要把索引文件清理一下,因为此步骤也会产生索引文件
    /**
     * 测试删除 在合并前
     * @throws IOException
     */
    @Test
    public void testDeleteBeforeMerge() throws IOException {
        IndexWriter indexWriter=getIndexWiter();
        indexWriter.deleteDocuments(new Term("id","1"));
        System.out.println("删除前。。。。。"+indexWriter.numDocs()+"个文件");
        indexWriter.commit();
        System.out.println("writer.maxDoc():"+indexWriter.maxDoc());
        System.out.println("writer.numDocs():"+indexWriter.numDocs());
        indexWriter.close();
    }

结果:

  • 上图可知,虽然indexWriter.deleteDocuments(new Term("id","1")); 删除了document,但是索引文件中不会立即删除。

 

  •    1.5  测试删除 在合并后

  •  由于进行了1.4 测试写入,所以要把索引文件清理一下,因为此步骤也会产生索引文件
  • 测试删除 在合并后,用强制删除的方法会立即在索引表删除文档,
    这种方法比较耗cpu,建议数据量不大的系统使用,数据量大的系统建议不写indexWriter.forceMergeDeletes(); 就不会立即删除文档
    /**
     * 测试删除 在合并后,用强制删除的方法会立即在索引表删除文档,
     * 这种方法比较耗cpu,建议数据量不大的系统使用,数据量大的系统建议不写indexWriter.forceMergeDeletes(); 就不会立即删除文档
     * @throws IOException
     */
    @Test
    public void testDeleteAfterMerge() throws IOException {
        IndexWriter indexWriter=getIndexWiter();
        indexWriter.deleteDocuments(new Term("id","1"));
        System.out.println("删除前。。。。。"+indexWriter.numDocs()+"个文件");
        indexWriter.forceMergeDeletes(); // 强制删除
        indexWriter.commit();
        System.out.println("writer.maxDoc():"+indexWriter.maxDoc());
        System.out.println("writer.numDocs():"+indexWriter.numDocs());
        indexWriter.close();
    }

结果:

  • 如图可知,立即在索引表删除文档,这种方式比较耗cpu,建议数据量不大的系统使用,数据量大的系统建议不写indexWriter.forceMergeDeletes(); 就不会立即删除文档。

 

  •    1.6  测试更新

  •  由于进行了1.5 测试写入,所以要把索引文件清理一下,因为此步骤也会产生索引文件
    /**
     * 测试更新
     * @throws Exception
     */
    @Test
    public void testUpdate()throws Exception{
        IndexWriter writer=getIndexWiter();
        Document doc=new Document();
        doc.add(new StringField("id", "1", Field.Store.YES));
        doc.add(new StringField("city","qingdao",Field.Store.YES));
        doc.add(new TextField("desc", "dsss is a city.", Field.Store.NO));
        writer.updateDocument(new Term("id","1"), doc);
        writer.close();
    }

结果:

 

posted @ 2017-11-27 21:04  shyroke、  阅读(1134)  评论(0编辑  收藏  举报
作者:shyroke 博客地址:http://www.cnblogs.com/shyroke/ 转载注明来源~