[lucene] 创建索引 Directory inderWriter

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值