lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mossaab Bagdouri <bagdouri_moss...@yahoo.fr>
Subject How to properly refresh MultiReader IndexSearcher in Lucene 4.0-BETA
Date Mon, 27 Aug 2012 17:37:08 GMT
Hi,

The context is that I've migrated from Lucene 3.6 to Lucene 4.0-BETA.
Lucene 3.6 had the convenient method IndexSearcher.isCurrent() for any
underlying IndexReader, including MultiReader. This is no more the case for
Lucene 4.0-BETA. I've been suffering in the last 48h until I came up with
this solution. I just want to share, and get feedbacks if any. The idea is
to create a new instance of MultiReader, add the old current SubReaders and
the new changed ones, refresh the IndexSearcher, then close the old
out-of-date SubReaders.

private IndexSearcher getIndexSearcher() {
        try {
            if (is == null || is.getIndexReader().getRefCount() == 0) {
                DirectoryReader newReaders[] = new DirectoryReader[2];
                for (int i = 0; i < 2; i++) {
                    newReaders[i] =
DirectoryReader.open(MyFSDirectories.get(i));
                }
                is = new IndexSearcher(new MultiReader(newReaders));
            } else {
                MultiReader mr = (MultiReader) is.getIndexReader();
                List<DirectoryReader> oldReaders = (List<DirectoryReader>)
mr.getSequentialSubReaders();
                DirectoryReader newReaders[] = new
DirectoryReader[oldReaders.size()];
                Set<Integer> toClose = new HashSet<>();
                for (int i = 0; i < oldReaders.size(); i++) {
                    DirectoryReader oldDirectoryReader = oldReaders.get(i);
                    if (oldDirectoryReader.isCurrent()) {
                        newReaders[i] = oldDirectoryReader;
                    } else {
                        toClose.add(i);
                        newReaders[i] =
DirectoryReader.openIfChanged(oldReaders.get(i));
                    }
                }

                is = new IndexSearcher(new MultiReader(newReaders));

                for (int i : toClose) {
                    oldReaders.get(i).close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return is;
    }

Regards,
Mossaab

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