Lucene创建索引,删除索引

来源:互联网 发布:移动身份证读卡器软件 编辑:程序博客网 时间:2024/05/17 05:10

Lucene创建索引

package testindex;import java.io.IOException;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.index.IndexWriter;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;public class TestIndex {    /**     * user(id,name,address)     * insert into user(id,name,address) values(1,'zhangsan','shanghaichangningqv');     * @throws IOException      */    public static void main(String[] args) throws IOException {        String [] ids={"1","2","3"};        String [] names={"zhangsan","lisi","wangwu"};        String [] addresses={"shanghai","beijing","guangzhou"};        Analyzer analyzer=new StandardAnalyzer();        String indexDir="C:/Users/yihong/Desktop/luceneindex";        Directory dir=FSDirectory.getDirectory(indexDir);        //true 表示创建或覆盖当前索引;false表示对当前索引进行追加        //Default value is 128        IndexWriter writer=new IndexWriter(dir,analyzer,true,IndexWriter.MaxFieldLength.LIMITED);        for(int i=0;i<ids.length;i++){            Document document=new Document();            document.add(new Field("id",ids[i],Field.Store.YES,Field.Index.ANALYZED));            document.add(new Field("name",names[i],Field.Store.YES,Field.Index.NO));            document.add(new Field("address",addresses[i],Field.Store.YES,Field.Index.ANALYZED));            writer.addDocument(document);        }        writer.optimize();        writer.close();    }}

删除索引

package testindex;import java.io.IOException;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.Term;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;public class TestDelDocument {    public static void main(String[] args) throws IOException {        String indexDir="C:/Users/yihong/Desktop/luceneindex";        Directory dir=FSDirectory.getDirectory(indexDir);        IndexReader reader=IndexReader.open(dir);//      System.out.println(reader.maxDoc());        reader.deleteDocument(0);        System.out.println(reader.deleteDocuments(new Term("id","2")));        System.out.println(reader.numDocs());        reader.close();        dir.close();    }}

现在的Field构造函数原型是如下样子的:
public Field(String name, String value, Store store, Index index) <

Lucene 的核心索引类 IndexWriter :建立索引的核心组件。

Directory:代表一个 lucene 索引项的位置。

Analyzer :对文本内容进行分析的抽象类,具体实现中有停用词切除、词干分析、大小写切换等功能。
Document :可以视作文本经过处理后所对应的对象,由多个字段组成,如路径、标题、摘要、修改日期等等。
Field :字段,对应于文本的某一部分数据,便于检索时根据结果提取。早期版本分为四个类型: Keyword 、 UnIndexed 、 UnStored 和 Text ,其主要区别归结于三个方面:是否被分析,是否被索引,是否存储于索引中。但是在最新版本的Lucene中,使用了一种更为统一的形式,也即只有Field一个类,然后使用一些参数来描述这个字段的属性,通过参数组合,可以组合出各种类别,甚至那四种不存在的类别理论上也是可以组合出来。

原创粉丝点击