lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stephane Bailliez <sbaill...@apache.org>
Subject Indexes auto creation
Date Mon, 13 Jun 2005 15:48:05 GMT
I have a very stupid question that puzzles me so far in the API. (I'm 
using Lucene 1.4.3)

There is a boolean flag over the creation of the Directory which is 
basically: use it as is or delete the storage area

Same for the index, the IndexWriter use a flag 'use the existing or 
create a new one'.

If you're creating an indexwriter with 'create' set to false. It could 
blow up with an IOException because the index does not exist.
But it could also blow up for other reasons with an IOException..which 
does not help much in identifying the source problem.


What I would like to is something like: if the index does not exist, 
then create one for me, otherwise use it.

I could do that with something like

try {
    writer = new IndexWriter(directory, analyzer, false)
} catch (IOException e){
     writer = new IndexWriter(directory, analyzer, true);
}

but this is not exactly true, and I could possibly delete an existing 
index if an IOException happens which is not due to a non-existing index.

Apparently a way to check there is an existing index would be (based on 
the Lucene source code) to do something like:

try {
    writer = new IndexWriter(directory, analyzer, false)
} catch (IOException e){
    if ( !directory.exists(IndexFileNames.SEGMENTS) ) {
        // the index really does not exists, so create it
        writer = new IndexWriter(directory, analyzer, true);
    } else {
        throw e;
    }
}

Is this correct or is there something even more simpler that I'm missing ?

Ideally I would have liked a subclassed IOException on the IndexWriter 
to differentiate the cases (like FileNotFoundException for example) but 
maybe I'm missing some trivial thing.


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message