lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Uwe Schindler" <...@thetaphi.de>
Subject RE: Extracting all documents for a given search
Date Mon, 19 Sep 2011 10:39:12 GMT
> Ahem, sorry. I quoted an old answer of mine, but HitCollector has been
gone
> for a while now...
> This is the modern version:
> 
> final ArrayList<Document> docs = new ArrayList<Document>();
> searcher.search( query, new Collector() {  private int docBase; *// ignore
> scorer*
> 
>    public void setScorer(Scorer scorer) {
>    }
> 
>    *// accept docs out of order (for a BitSet it doesn't matter)*
>    public boolean acceptsDocsOutOfOrder() {
>      return true;
>    }
> 
>    public void collect(int doc) {
>      doc.add(searcher.doc(doc + docBase));
>    }
> 
>    public void setNextReader(IndexReader reader, int docBase) {
>      this.docBase = docBase;
>    }
> 
> });

This code surely works, but looks a little bit strange as you are using the
docbase to calculate a top-level document id that then gets again be drilled
down to the indexreader by IndexSearcher.

The more native approach is to ignore the docBase and instead directly ask
IndexReader passed in by setNextReader for the document:

final ArrayList<Document> docs = new ArrayList<Document>();
searcher.search( query, new Collector() {
   private IndexReader reader;
 
   public void setScorer(Scorer scorer) {
   }

   public boolean acceptsDocsOutOfOrder() {
     return true;
   }

   public void collect(int doc) {
     docs.add(reader.document(doc));
   }

   public void setNextReader(IndexReader reader, int docBase) {
     this.reader = reader;
   }

});


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message