RAMDirectory and FSDirectory

I have been working on a program that can do indexing first in RAMDirectory then flush it to FSDirectory in java using lucene framework. after I had been asking around on the internet. I finally solved it.

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;

public class SimpleFileIndexer {

	public static void main(String[] args) throws Exception   {

		int i=0;
		while(i<2) {
		File indexDir = new File("C:/Users/Raden/Documents/lucene/LuceneHibernate/adi");
		File dataDir = new File("C:/Users/Raden/Documents/lucene/LuceneHibernate/adi");
		String suffix = "txt";

		SimpleFileIndexer indexer = new SimpleFileIndexer();

		int numIndex = indexer.index(indexDir, dataDir, suffix);

		System.out.println("Total files indexed " + numIndex);


	private int index(File indexDir, File dataDir, String suffix) throws Exception {
	    RAMDirectory ramDir = new RAMDirectory();          // 1
	    IndexWriter indexWriter = new IndexWriter(
	            ramDir,                                    // 2
	            new SimpleAnalyzer(),
	    indexDirectory(indexWriter, dataDir, suffix);
	    int numIndexed = indexWriter.maxDoc();

	    Directory.copy(ramDir, FSDirectory.open(indexDir), false); // 3

	    return numIndexed;

	private void indexDirectory(IndexWriter indexWriter, File dataDir, String suffix) throws IOException {
		File[] files = dataDir.listFiles();
		for (int i = 0; i < files.length; i++) {
			File f = files[i];
			if (f.isDirectory()) {
				indexDirectory(indexWriter, f, suffix);
			else {
				indexFileWithIndexWriter(indexWriter, f, suffix);

	private void indexFileWithIndexWriter(IndexWriter indexWriter, File f, String suffix) throws IOException {
		if (f.isHidden() || f.isDirectory() || !f.canRead() || !f.exists()) {
		if (suffix!=null && !f.getName().endsWith(suffix)) {
		System.out.println("Indexing file " + f.getCanonicalPath());

		Document doc = new Document();
		doc.add(new Field("contents", new FileReader(f)));
		doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.ANALYZED));
	} }

thanks god. now there’s still one more thing to do. that is how to do deletion and updation in lucene.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s