lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Volodymyr Bychkoviak <vbychkov...@i-hypergrid.com>
Subject Re: Indexes auto creation
Date Mon, 13 Jun 2005 17:11:26 GMT
hello

I'm using following code in the startup of my program


    String indexDirectory = //some init
    try {
      if ( !IndexReader.indexExists(indexDirectory)) {
        // working index doesn't exist so try to create a dummy index.
        IndexWriter iw = new IndexWriter(indexDirectory, new 
StandardAnalyzer(), true);
        iw.close();
      } else {
        IndexReader.unlock(FSDirectory.getDirectory(indexDirectory, false));
      }
    } catch (IOException e) {
      // Exception happened when trying to unlock working index
    }

regards,
Volodymyr Bychkoviak


Stephane Bailliez wrote:

> 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
>
>

---------------------------------------------------------------------
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