---
title: "MoveLuceneRebuild"
author: "xiaowei2002"
date: "2014-11-06T09:59:16Z"
tags: []
categories: ["Web前端"]
slug: "movelucenerebuild"
draft: false
lastmod: "2014-11-06T09:58:55Z"
id: 2152929
url: "https://xiaowei2002.iteye.com/blog/2152929"
---

package com.yulong.lucene.biz;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import com.yulong.lucene.core.LuceneContacts;
import com.yulong.lucene.util.RequestUtil;



/**
 * LUCENE小文件移动到大文件中的任务调度
 *
 * @author fyh
 */
public class MoveLuceneRebuild {
	private String path = "";

//	public MoveLuceneRebuild(String path) {
//		this.path = RequestUtil.ChangePath(path);
//		this.move();
//	}

	/**
	 * 开始转移
	 *
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public boolean move(String path) {
		this.path = RequestUtil.ChangePath(path);
		IndexWriter iw = null;
		IndexWriter iwcc = null;
		try {
			File file = new File(path+"rebuild/");
			if (file.exists()) {
				try{
					Directory directory = FSDirectory.open(file);
					iw = LuceneContacts.iwpool.get(path +"index/", this.toString());
					iw.deleteAll();
					if(iw != null){
						iw.addIndexes(new Directory{directory});
						iwcc = LuceneContacts.iwpool.get(path +"rebuild/", this.toString());
						iwcc.deleteAll();
//						iwcc.close();
					}
				}catch (Exception e) {
				}
			}
//			iw.close();
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			try {
				if(iwcc!=null){
					iwcc.close();					
				}
				if(iw!=null){
					iw.close();
				}
			} catch (CorruptIndexException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return true;
	}

}