lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Uwe Schindler" <>
Subject RE: Bug in TopFieldCollector?
Date Mon, 30 Mar 2009 08:39:32 GMT
Why not call IndexSearcher.getIndexReader().getSequentialSubReaders() (see
e/lucene/index/IndexReader.html). Its public and documented as this:


he/lucene/index/IndexReader.html> IndexReader[] getSequentialSubReaders()


Expert: returns the sequential sub readers that this reader is logically
composed of. For example, IndexSearcher uses this API to drive searching by
one sub reader at a time. If this reader is not composed of sequential child
readers, it should return null. If this method returns an empty array, that
means this reader is a null reader (for example a MultiReader that has no
sub readers). 

NOTE: for a MultiSegmentReader, which is obtained by
open(java.lang.String) when the index has more than one segment, you should
not use the sub-readers returned by this method to make any changes
(setNorm, deleteDocument, etc.). Doing so will likely lead to index
corruption. Use the parent reader instead. 


You only have the problem to replicate the code that gathers the subreaders
of the subreaders itself recursively.



Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen


From: Shai Erera [] 
Sent: Monday, March 30, 2009 10:20 AM
Subject: Re: Bug in TopFieldCollector?


Already did !

Another question - I think we somehow broke TopFieldCollector ...
Previously, in TopFieldDocCollector, it accepted an IndexReader as a
parameter, and now it requires IndexReader[], which is called subReaders.
Calling the 'fast' search methods with Sort has no problem obtaining that
IndexReader[] (and sort it), but how would someone use TopFieldCollector w/o
calling the appropriate Searcher methods?

For example, since all the Searcher methods pass in fillFields = true, I
wanted to use the method, TopFieldCollector) in the
test case I wrote, which BTW looks like this:

  public void testSortWithoutFillFields() throws Exception {
    // There was previously a bug in TopFieldCollector when fillFields was
    // to false - the same doc and score was set in ScoreDoc[] array. This
    // asserts that if fillFields is false, the documents are set properly.
    // does not use Searcher's default search methods (with Sort) since all
    // fillFields to true.
    Sort sort = new Sort();
    int nDocs=10;
    TopDocsCollector tdc = new TopFieldCollector(sort, nDocs,
        new IndexReader[] { ((IndexSearcher) full).getIndexReader() },
false); MatchAllDocsQuery(), tdc);

    ScoreDoc[] sd = tdc.topDocs().scoreDocs;
    for (int i = 1; i < sd.length; i++) {
      assertTrue(sd[i].doc != sd[i - 1].doc);

You'll notice that creating a TopFieldCollector now is much more complicated
and *ugly*. As a user of IndexSearcher, I can only call getIndexReader()
which returns a single IndexReader. I don't have access to gatherSubReaders
and sortSubReaders. I don't see why I should have access to them. So it
forces me to create a dummy array with a single IndexReader.

There are two ways I see to solve it:
1. Introduce a getIndexReaders() method on IndexSearcher, which will return
an array of (sorted?) IndexReader.
2. Introduce a new constructor in TopFieldCollector which accepts a single
IndexReader and make the other one package-private (for use by IndexSearcher
only). That constructor can internally create a dummy array of readers, but
at least it's private to the constructor and not exposed to the rest of the

Otherwise, I think it ruins TopFieldCollector and will make it a lot less
intuitive to use. At least, people who'd want to move from
TopFieldDocCollector to TopFieldCollector, will find it very inconvenient
and strange.

What do you think? I can do that (2) as part of 1575. If (1) is better, then
I think a different issue should be opened, because we might want to return
such an array as sorted or something, which makes it less trivial.


On Mon, Mar 30, 2009 at 11:07 AM, Michael McCandless
<> wrote:

Looks like quite a bug, Shai!  Thanks.  It came in with LUCENE-1483.
I would say add test case & fix it under 1575.


On Mon, Mar 30, 2009 at 3:50 AM, Shai Erera <> wrote:
> Hi
> As I prepared the patch for 1575, I noticed a strange implementation in
> TopFieldCollector's topDocs():
>     ScoreDoc[] scoreDocs = new ScoreDoc[queue.size()];
>     if (fillFields) {
>       for (int i = queue.size() - 1; i >= 0; i--) {
>         scoreDocs[i] = queue.fillFields((FieldValueHitQueue.Entry)
> queue.pop());
>       }
>     } else {
>       Entry entry = (FieldValueHitQueue.Entry) queue.pop();
>       for (int i = queue.size() - 1; i >= 0; i--) {
>         scoreDocs[i] = new FieldDoc(entry.docID,
>                                     entry.score);
>       }
>     }
>     return new TopFieldDocs(totalHits, scoreDocs, queue.getFields(),
> maxScore);
> Notice that if fillFields is true, then documents are popped from the
> However if it's false, then the first document is popped out of the queue
> and used to populate the entire ScoreDoc[]? I believe that's wrong, right?
> Otherwise, the returned TopFieldDocs.scoreDocs array will include the same
> document and score?
> I noticed there's no test case for that ... TopFieldCollector's
> is called only from, Filter, int, Sort,
> /* fillFields */) which is called from,
> int, sort) with fillFields always set to true. So perhaps that's why this
> hasn't showed up?
> BTW, the now deprecated TopFieldDocCollector's topDocs() implementation
> looks like this:
>     FieldSortedHitQueue fshq = (FieldSortedHitQueue)hq;
>     ScoreDoc[] scoreDocs = new ScoreDoc[fshq.size()];
>     for (int i = fshq.size()-1; i >= 0; i--)      // put docs in array
>       scoreDocs[i] = fshq.fillFields ((FieldDoc) fshq.pop());
>     return new TopFieldDocs(totalHits, scoreDocs,
>                             fshq.getFields(), fshq.getMaxScore());
> It assumes fillFields is always true and always pops elements out of the
> queue.
> If this is a bug, I can fix it as part of 1575, as I'm touching that class
> anyway. I can also add a test case ... The fix is very simple BTW, just
> the line "Entry entry = (FieldValueHitQueue.Entry) queue.pop();" inside
> for loop.
> Shai

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


View raw message