lucene的CRUD操作Document(四)

IndexWriter writer = new IndexWriter(Directory, IndexWriterConfig);

增加文档:writer.addDocument();

读取文档:DirectoryReader.open(Directory);

删除在合并前(先删除后增加,结果为删除前的结果,即文档还保留在内存中):writer.deleteDocuments();

删除在合并后(先删除后增加,结果为删除后的结果,即文档不在内存中):writer.deleteDocuments();writer.forceMergeDeletes(); // 强制删除

更新:writer.updateDocument();

package com.wp.util;

import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Before;
import org.junit.Test;

public class IndexIngTest {

    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." };

    private Directory dir;// 目录

    /**
     * 获取IndexWriter实例
     * 
     * @return
     * @throws Exception
     */
    private IndexWriter getWriter() throws Exception {
        Analyzer analyzer = new StandardAnalyzer(); // 标准分词器
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
        IndexWriter writer = new IndexWriter(dir, iwc);
        return writer;
    }

    /**
     * 添加文档
     * 
     * @throws Exception
     */
    @Before
    public void setUp() throws Exception {
        dir = FSDirectory.open(Paths.get("D:\\lucene\\luceneIndex"));// 得到luceneIndex目录
        IndexWriter writer = getWriter();// 得到索引
        for (int i = 0; i < ids.length; i++) {
            Document doc = new Document();// 创建文档
            doc.add(new StringField("id", ids[i], Field.Store.YES));// 将id属性存入内存中
            doc.add(new StringField("city", citys[i], Field.Store.YES));
            doc.add(new TextField("desc", descs[i], Field.Store.NO));
            writer.addDocument(doc); // 添加文档
        }
        writer.close();
    }

    /**
     * 测试写了几个文档
     * 
     * @throws Exception
     */
    @Test
    public void testIndexWriter() throws Exception {
        IndexWriter writer = getWriter();
        System.out.println("写入了" + writer.numDocs() + "个文档");
        writer.close();
    }

    /**
     * 测试读取文档
     * 
     * @throws Exception
     */
    @Test
    public void testIndexReader() throws Exception {
        IndexReader reader = DirectoryReader.open(dir);
        System.out.println("最大文档数:" + reader.maxDoc());
        System.out.println("实际文档数:" + reader.numDocs());
        reader.close();
    }

    /**
     * 测试删除 在合并前(先删除后增加,结果为删除前的结果)
     * 
     * @throws Exception
     */
    @Test
    public void testDeleteBeforeMerge() throws Exception {
        IndexWriter writer = getWriter();
        System.out.println("删除前:" + writer.numDocs());
        writer.deleteDocuments(new Term("id", "1"));// term:根据id找到为1的
        writer.commit();
        System.out.println("writer.maxDoc():" + writer.maxDoc());
        System.out.println("writer.numDocs():" + writer.numDocs());
        writer.close();
    }

    /**
     * 测试删除 在合并后(先删除后增加,结果为删除后的结果)
     * 
     * @throws Exception
     */
    @Test
    public void testDeleteAfterMerge() throws Exception {
        IndexWriter writer = getWriter();
        System.out.println("删除前:" + writer.numDocs());
        writer.deleteDocuments(new Term("id", "1"));
        writer.forceMergeDeletes(); // 强制删除
        writer.commit();
        System.out.println("writer.maxDoc():" + writer.maxDoc());
        System.out.println("writer.numDocs():" + writer.numDocs());
        writer.close();
    }

    /**
     * 测试更新
     * 
     * @throws Exception
     */
    @Test
    public void testUpdate() throws Exception {
        IndexWriter writer = getWriter();
        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();
    }
}

 

Java小生店铺:

Pc端:http://shop125970977.taobao.com/index.htm

手机端:搜索 java小生店铺

希望店铺的资料能帮助到你!!!

 

posted @ 2016-09-27 20:18  一个java小生的奋斗史  阅读(1543)  评论(0编辑  收藏  举报