lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael McCandless <>
Subject Re: Directory.list() deprecation
Date Tue, 10 Nov 2009 10:07:51 GMT
On Mon, Nov 9, 2009 at 7:53 PM, Daniel Noll <> wrote:
> On Tue, Nov 10, 2009 at 00:44, Michael McCandless
> <> wrote:
>> Stepping back, since presumably your app knows what it's storing in
>> the directory, can't you filter for files you know you've created?
>> What's the larger use case here?
> The exact use case where we were using list() is to determine whether
> the index had data in it, without having to open it and do a
> docCount() (well, there were also calls to it in the unit tests, but
> those were entirely replaceable with listAll()).
> This was previously a one-liner:
>    boolean containsData = directory.list().length > 1
> Maybe there is another newer API which will return this to being a
> one-liner -- at the time it was written this seemed to be the best
> option.
> By the way, when I mean "there is no data in it", I mean the index
> exists but has 0 documents.  Detecting that the index itself does not
> exist is somewhat simpler.

I see.

There's IndexReader.indexExists(), but it sounds like that's not what
you want because you want to check whether in fact it has > 0 docs in

Otherwise, I think something like this (requires 2.9, since prior to
that SegmentInfos isn't public) should work:

    SegmentInfos sis = new SegmentInfos();
    try {;
    } catch (IOException ioe) {
      // presumably no index exists
    int totDocCount = 0;
    for(SegmentInfo info : sis) {
      totDocCount += info.docCount;

It's not a one-liner, but it's fast to run since it just reads the
segments file.  But remember that SegmentInfos has forward rights to
break back-compat ("subject to change suddenly in the next release")!


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message