[lucene] 创建索引 Directory inderWriter

来源:互联网 发布:c语言培训费用 编辑:程序博客网 时间:2024/06/09 00:13
Lucene主要的类:
Directory 索引的位置 Document 的集合组成 是由 一般用来打开索引库 (结构倒排序的结构) 
子类FSDirectory :
 //创建索引目录 INDEX_PATH: 索引目录
Directory  directory = FSDirectory.open(new File(INDEX_PATH));


Document 文档,lucene存储索引文件的形式,类似于DB中行,field 相当于DB中的列,
* 要写公共转化的方法:file转化成doc,doc 转化成file,类似于orm映射。
/**
     * 转换内容对象为Document对象
     * @param ct
     * @return
     */
    public static Document convertContentToDoc(ContentObject ct){
        Document doc = new Document();
           //Field.Store.YES 表示存储内容
        doc.add(new StringField("title", ct.getTitle(), Field.Store.YES));
        doc.add(new TextField("content", ct.getContent(),Field.Store.YES));
        doc.add(new StringField("link", ct.getLink(), Field.Store.YES));
  doc.add(new LongField("createDate",new Date().getTime(),Field.Store.YES));
        return doc;
    }


inderWriter 操作索引库的增删改查,添加doc 删除doc 查找doc 更改doc
构造方法:
IndexWriter(Directory?d,?IndexWriterConfig?conf) Constructs a new IndexWriter per the settings given in?conf.
IndexWriterConfig(Version?matchVersion,?Analyzer?analyzer)
Creates a new config that with defaults that match the specified?Version?as well as the default?Analyzer.


IndexWriterConfig indexWriterConfig = new IndexWriterConfig(version, analyzer);
indexWriterConfig.setMaxBufferedDocs(10000);
indexWriterConfig.setMergePolicy(mergePolicy);
indexWriterConfig.setRAMBufferSizeMB(50);
///设置索引的打开模式 创建或者添加索引
indexWriterConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
//如果索引文件被锁,解锁索引文件
if(IndexWriter.isLocked(directory)){  
       IndexWriter.unlock(directory);  
   }
0 0
原创粉丝点击