ITeye博客

RebuildLucene

xiaowei2002 · 2014-11-07 11:32 · luence · Web前端 · Markdown

package com.yulong.lucene.biz;

import java.io.IOException; import java.util.Iterator; import java.util.List; import java.util.Map;

import org.apache.log4j.Logger; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexWriter;

import com.yulong.lucene.core.LuceneContacts; import com.yulong.lucene.util.RequestUtil;

/**

  • LUCENE重新写入大批量索引

  • @author fyh */ public class RebuildLucene {

    private static final Logger LOG = Logger.getLogger(RebuildLucene.class);

    private String path = "";

    public RebuildLucene(String path) {

     this.path = RequestUtil.ChangePath(path);
    

    }

    /**

    • 写入索引数据

    • @param map */ public boolean write1(List<Map<String, String>> list) {

      IndexWriter iwriter = null; try { iwriter = LuceneContacts.iwpool.get(path + "rebuild/", this.toString()); for (Map<String, String> map : list) { Document doc = new Document(); for (Iterator it = map.keySet().iterator(); it.hasNext();) { String key = RequestUtil.VailStr(it.next(), ""); String value = RequestUtil.VailStr(map.get(key), ""); if (key != "") doc.add(new Field(key, value, Field.Store.YES, Field.Index.ANALYZED)); } if (map.size() > 0) { iwriter.addDocument(doc); } } // iwriter.close(); LOG.info("LUCENE重新写入大批量索引成功!"); } catch (Exception e) { e.printStackTrace(); return false; } finally { try { if (iwriter != null) { iwriter.close(); } } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return true; }

    public boolean write(List<Map<String, String>> list) {

     boolean res = this.write1(list);
     if (res)
     {
         res = new MoveLuceneRebuild().move(path);// 替换当前重编译后的索引
     }
     return res;
    

    } }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。