KristianMedK
KristianMedK

Reputation: 1841

Lucene indexwriter destination folder

I'm working on a small lucene project, where i have to index a bunch of text files. so far i've managed to create the index, i think. the code runs and i get a bunch of files called 0_.* fdt/fdx/fnm and more.

what i want to know is, can i pick a destination folder, for the index to be created in?

i'm following this Guide and i define a index folder, and a files to index folder, but i can't find any parameters in the indexwriter constructor, that could achieve this.

here's my code for creating the index

public static void createIndex() throws CorruptIndexException, LockObtainFailedException, IOException {
    File[] files = FILES_TO_INDEX_DIRECTORY.listFiles();
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_33);
    SimpleFSDirectory d = new SimpleFSDirectory(FILES_TO_INDEX_DIRECTORY);
    IndexWriter indexWriter = new IndexWriter(d, analyzer, IndexWriter.MaxFieldLength.LIMITED);

    for (File file : files) {
        Document document = new Document();

        String path = file.getCanonicalPath();
        byte[] bytes = path.getBytes();
        document.add(new Field(FIELD_PATH, bytes));

        Reader reader = new FileReader(file);
        document.add(new Field(FIELD_CONTENTS, reader));

        indexWriter.addDocument(document);
    }
    indexWriter.optimize();
    indexWriter.close();
}

and i'm using type File instead of string for the directories

public static File FILES_TO_INDEX_DIRECTORY = new File("C:\\Users\\k\\Dropbox\\Public\\afgansprojekt\\RouteLogger\\Lucene\\FilesToIndex");
public static final File INDEX_DIRECTORY = new File("C:\\Users\\k\\Dropbox\\Public\\afgansprojekt\\RouteLogger\\Lucene\\Index");

Upvotes: 0

Views: 4331

Answers (1)

hkn
hkn

Reputation: 1448

Actually you are setting the destination folder with SimpleFSDirectory d = new SimpleFSDirectory(FILES_TO_INDEX_DIRECTORY);

Just change SimpleFSDirectory(FILES_TO_INDEX_DIRECTORY); to SimpleFSDirectory(INDEX_DIRECTORY);.

Edit:

File[] files = FILES_TO_INDEX_DIRECTORY.listFiles(); //this is where you set the files to index

SimpleFSDirectory d = new SimpleFSDirectory(FILES_TO_INDEX_DIRECTORY); //here you are setting the index directory

You should change this line to SimpleFSDirectory d = new SimpleFSDirectory(INDEX_DIRECTORY);

Upvotes: 1

Related Questions