You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by "Paul J. Lucas" <pa...@lucasmail.org> on 2009/06/10 03:04:59 UTC

Migrating from Hit/Hits to TopDocs/TopDocCollector

I have existing code that's like:

	final Term t = /* ... */;
         final Iterator i = searcher.search( new  
TermQuery( t ) ).iterator();
         while ( i.hasNext() ) {
             final Hit hit = (Hit)i.next();
	    // "FILE" is the field that recorded the original file indexed
             final File f = new File( hit.get( "FILE" ) );
	    // ...
         }

It's not clear to me how to rewrite the code using TopDocs/ 
TopDocCollector and how to iterate over the results.

A little help?  Thanks.  :-)

- Paul

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


Re: Migrating from Hit/Hits to TopDocs/TopDocCollector

Posted by Ian Lea <ia...@gmail.com>.
Hi


The code below might do the job.  Based on the example at
http://lucene.apache.org/java/2_4_1/api/core/org/apache/lucene/search/Hits.html

Completely uncompiled and untested of course.

TopDocCollector collector = new TopDocCollector(hitsPerPage);
final Term t = /* ... */;
Query query = new TermQuery( t )
searcher.search(query, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
for (int i = 0; i < hits.length; i++) {
     int docId = hits[i].doc;
     Document d = searcher.doc(docId);
     final File f = new File( d.get( "FILE" ) );
}


--
Ian.


On Wed, Jun 10, 2009 at 2:04 AM, Paul J. Lucas<pa...@lucasmail.org> wrote:
> I have existing code that's like:
>
>        final Term t = /* ... */;
>        final Iterator i = searcher.search( new TermQuery( t ) ).iterator();
>        while ( i.hasNext() ) {
>            final Hit hit = (Hit)i.next();
>            // "FILE" is the field that recorded the original file indexed
>            final File f = new File( hit.get( "FILE" ) );
>            // ...
>        }
>
> It's not clear to me how to rewrite the code using TopDocs/TopDocCollector
> and how to iterate over the results.
>
> A little help?  Thanks.  :-)
>
> - Paul
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>

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