Lucene Paoding Demo

/**
 * 
 * @author Q.Wong [2010-12-17]
 * 
 */
public class IndexManager {

	private static Logger logger = LogManager.getLogger(IndexManager.class);

	/**
	 * return an IndexWriter with FSDirectory
	 * 
	 * @param indexDir
	 * @param analyzer
	 * @return
	 * @throws CorruptIndexException
	 * @throws LockObtainFailedException
	 * @throws IOException
	 */
	public static IndexWriter getFSDirectoryIndexWriter(String indexDir,
			Analyzer analyzer) throws CorruptIndexException,
			LockObtainFailedException, IOException {
		return new IndexWriter(FSDirectory.open(new File(indexDir)), analyzer,
				true, MaxFieldLength.LIMITED);
	}

	/**
	 * return an IndexWriter with RamDir
	 * 
	 * @param ramDir
	 * @param analyzer
	 * @return
	 * @throws CorruptIndexException
	 * @throws LockObtainFailedException
	 * @throws IOException
	 */
	public static IndexWriter getRamDirIndexWriter(Directory ramDir,
			Analyzer analyzer) throws CorruptIndexException,
			LockObtainFailedException, IOException {
		return new IndexWriter(ramDir, analyzer, MaxFieldLength.UNLIMITED);
	}

	/**
	 * return an IndexReader with FSDirectory
	 * 
	 * @param indexDir
	 * @return
	 * @throws CorruptIndexException
	 * @throws IOException
	 */
	public static IndexReader getFSDirectoryIndexReader(String indexDir)
			throws CorruptIndexException, IOException {
		return IndexReader.open(FSDirectory.open(new File(indexDir)), true);
	}

	/**
	 * return an IndexReader with RamDir
	 * 
	 * @param ramDir
	 * @return
	 * @throws CorruptIndexException
	 * @throws IOException
	 */
	public static IndexReader getRamDirIndexReader(Directory ramDir)
			throws CorruptIndexException, IOException {
		return IndexReader.open(ramDir);
	}

	/**
	 * 创建索引
	 * 
	 * @param f
	 * @param writer
	 * @throws CorruptIndexException
	 * @throws IOException
	 */
	public static void indexDocs(File f, IndexWriter writer,
			Boolean storeContents) throws CorruptIndexException, IOException {
		if (!f.canRead()) {
			return;
		}
		if (f.isDirectory()) {
			String[] files = f.list();
			if (null != files) {
				for (String file : files) {
					indexDocs(new File(f, file), writer, storeContents);
				}
			}
		} else {
			writer.addDocument(Document(f, storeContents));
			logger.info("Indexed: [" + f.getPath() + "]");
		}
	}

	/**
	 * 根据File生成Document对象
	 * 
	 * @param f
	 * @param storeContents
	 * @return
	 * @throws IOException
	 */
	private static Document Document(File f, Boolean storeContents)
			throws IOException {
		Document doc = new Document();
		doc.add(new Field("path", f.getPath(), Field.Store.YES,
				Field.Index.NOT_ANALYZED));

		doc.add(new Field("modified", DateTools.timeToString(f.lastModified(),
				DateTools.Resolution.MINUTE), Field.Store.YES,
				Field.Index.NOT_ANALYZED));

		if (storeContents) {
			doc.add(new Field("contents", readFile(f), Field.Store.YES,
					Field.Index.ANALYZED,
					Field.TermVector.WITH_POSITIONS_OFFSETS));
		} else {
			doc.add(new Field("contents", new FileReader(f)));
		}
		return doc;
	}

	/**
	 * 读取文件内容
	 * 
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public static String readFile(File file) throws IOException {
		Reader re = new FileReader(file);
		char[] chs = new char[1024];
		int count;
		String content = "";
		while ((count = re.read(chs)) != -1) {
			content = content + new String(chs, 0, count);
		}
		return content;
	}

}
 

你可能感兴趣的:(F#,Lucene)