lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Seth Rosen <s...@architexa.com>
Subject Re: adding documents to an existing index
Date Wed, 27 Oct 2010 14:13:16 GMT
Yakob the boolean in the constructor should be true if you want to create a
NEW index in INDEX_DIR and false to append to an existing one as seen here
[1]

As for adding a directory to an index you will need to validate the
directory, then loop through it recursively and add each doc to the writer
you created.

static void indexDocs(IndexWriter writer, File file)
>>
>>     throws IOException {
>>
>>     // do not try to index files that cannot be read
>>
>>     if (file.canRead()) {
>>
>>       if (file.isDirectory()) {
>>
>>         String[] files = file.list();
>>
>>         // an IO error could occur
>>
>>         if (files != null) {
>>
>>           for (int i = 0; i < files.length; i++) {
>>
>>             indexDocs(writer, new File(file, files[i]));
>>
>>           }
>>
>>         }
>>
>>       } else {
>>
>>         System.out.println("adding " + file);
>>
>>         try {
>>
>>           writer.addDocument(FileDocument.Document(file));
>>
>>         }
>>
>>         // at least on windows, some temporary files raise this exception
>>> with an "access denied" message
>>
>>         // checking if the file can be read doesn't help
>>
>>         catch (FileNotFoundException fnfe) {
>>
>>           ;
>>
>>         }
>>
>>       }
>>
>>     }
>>
>>   }
>>
>>
[1] http://lucene.apache.org/java/3_0_0/api/core/org/apache/lucene/index/IndexWriter.html#IndexWriter(org.apache.lucene.store.Directory,
org.apache.lucene.analysis.Analyzer, boolean,
org.apache.lucene.index.IndexWriter.MaxFieldLength)

Seth Rosen
www.architexa.com
Understand & Document Code In Seconds
seth@architexa.com <vineet@architexa.com>



On Wed, Oct 27, 2010 at 9:51 AM, Yakob <jacobian@opensuse-id.org> wrote:

> On 10/27/10, Seth Rosen <seth@architexa.com> wrote:
> > Yakob,
> > Here is a snippet of an example of IndexWriter from the lucene source
> that
> > you might find helpful.
> >
> >
> >> IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_DIR), new
> >> StandardAnalyzer(Version.LUCENE_CURRENT), true,
> >> IndexWriter.MaxFieldLength.LIMITED);
>
> // the above code should change true into false so that the index will
> still be open right?
> // I mean surely the index shouldn't be close
> >
> > System.out.println("Indexing to directory '" +INDEX_DIR+ "'...");
> >
> // what confuse me is how can add a new directory path containing new
> Documents to a
> // lucene class. so that lucene will index those new documents and add
> it to an existing
> // index.
> > indexDocs(writer, docDir);
> >
> > System.out.println("Optimizing...");
> >
> > writer.optimize();
> >
> > writer.close();
> >
> >
> > Seth Rosen
> > www.architexa.com
> > Understand & Document Code In Seconds
> > seth@architexa.com <vineet@architexa.com>
> >
> >
> >
>
>
> --
> http://jacobian.web.id
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message