Return-Path: Delivered-To: apmail-lucene-java-user-archive@www.apache.org Received: (qmail 85332 invoked from network); 13 Jun 2005 17:11:50 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 13 Jun 2005 17:11:50 -0000 Received: (qmail 44735 invoked by uid 500); 13 Jun 2005 17:11:46 -0000 Delivered-To: apmail-lucene-java-user-archive@lucene.apache.org Received: (qmail 44681 invoked by uid 500); 13 Jun 2005 17:11:45 -0000 Mailing-List: contact java-user-help@lucene.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: java-user@lucene.apache.org Delivered-To: mailing list java-user@lucene.apache.org Received: (qmail 44657 invoked by uid 99); 13 Jun 2005 17:11:45 -0000 X-ASF-Spam-Status: No, hits=0.1 required=10.0 tests=FORGED_RCVD_HELO X-Spam-Check-By: apache.org Received-SPF: pass (hermes.apache.org: local policy) Received: from uar.spline-software.com (HELO uar.i-hypergrid.com) (194.44.193.196) by apache.org (qpsmtpd/0.28) with ESMTP; Mon, 13 Jun 2005 10:11:43 -0700 Received: from [192.168.0.86] (vbychkoviak.jforce [192.168.0.86]) by uar.i-hypergrid.com (Postfix) with ESMTP id 1C39A1BD30 for ; Mon, 13 Jun 2005 20:11:27 +0300 (EEST) Message-ID: <42ADBE3E.1090906@i-hypergrid.com> Date: Mon, 13 Jun 2005 20:11:26 +0300 From: Volodymyr Bychkoviak User-Agent: Mozilla Thunderbird 0.9 (Windows/20041103) X-Accept-Language: en-us, en MIME-Version: 1.0 To: java-user@lucene.apache.org Subject: Re: Indexes auto creation References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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