On Apr 25, 2005, at 10:36 PM, Otis Gospodnetic wrote:
Would it be better to explicitly check for out of bounds hitNumber instead of catching ArrayIndexOutOfBoundsException?
if (hitNumber > hits.length()) { throw new NoSuchElementException(); }
Good eye. I've added a test case that identified this issue since the out of bound exception could never have occurred and then fixed the issue.
Also, is "a future for a hit" a typo, or does that actually mean something? This makes me think of Python's "future", but I'm not sure what this means in this context.
I've cleared this up.
Thanks,
Erik
Thanks, Otis
--- [EMAIL PROTECTED] wrote:
http://svn.apache.org/viewcvs/lucene/java/trunk/CHANGES.txt? rev=164695&r1=164694&r2=164695&view=diffAuthor: ehatcher Date: Mon Apr 25 17:21:53 2005 New Revision: 164695
URL: http://svn.apache.org/viewcvs?rev=164695&view=rev Log: Add Hits.iterator and corresponding HitIterator and Hit classes. Contributed by Jeremy Rayner
Added: lucene/java/trunk/src/java/org/apache/lucene/search/Hit.java
lucene/java/trunk/src/java/org/apache/lucene/search/HitIterator.java lucene/java/trunk/src/test/org/apache/lucene/TestHitIterator.java Modified: lucene/java/trunk/CHANGES.txt lucene/java/trunk/src/java/org/apache/lucene/search/Hits.java
Modified: lucene/java/trunk/CHANGES.txt URL:
======================================================================= =======
http://svn.apache.org/viewcvs/lucene/java/trunk/src/java/org/apache/ lucene/search/Hit.java?rev=164695&view=auto--- lucene/java/trunk/CHANGES.txt (original) +++ lucene/java/trunk/CHANGES.txt Mon Apr 25 17:21:53 2005 @@ -91,6 +91,11 @@ which lets the caller get the Lucene version information specified in the Lucene Jar. (Doug Cutting via Otis) + +15. Added Hits.iterator() method and corresponding HitIterator and Hit objects. + This provides standard java.util.Iterator iteration over Hits. + Each call to the iterator's next() method returns a Hit object. + (Jeremy Rayner via Erik)
API Changes
Added: lucene/java/trunk/src/java/org/apache/lucene/search/Hit.java URL:
======================================================================= =======
http://svn.apache.org/viewcvs/lucene/java/trunk/src/java/org/apache/ lucene/search/HitIterator.java?rev=164695&view=auto--- lucene/java/trunk/src/java/org/apache/lucene/search/Hit.java (added) +++ lucene/java/trunk/src/java/org/apache/lucene/search/Hit.java Mon Apr 25 17:21:53 2005 @@ -0,0 +1,125 @@ +/** + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.lucene.search; + +import java.io.IOException; + +import org.apache.lucene.document.Document; + +/** + * a lazy future for a hit, useful for iterators over instances of Hits + * + * @author Jeremy Rayner + */ +public class Hit implements java.io.Serializable { + + private float score; + private int id; + private Document doc = null; + + private boolean resolved = false; + + private Hits hits = null; + private int hitNumber; + + /** + * Constructed from [EMAIL PROTECTED] HitIterator} + * @param hits Hits returned from a search + * @param hitNumber Hit index in Hits + */ + Hit(Hits hits, int hitNumber) { + this.hits = hits; + this.hitNumber = hitNumber; + } + + /** + * Returns document for this hit. + * + * @see [EMAIL PROTECTED] Hits#doc(int)} + */ + public Document getDocument() throws IOException { + if (!resolved) fetchTheHit(); + return doc; + } + + /** + * Returns score for this hit. + * + * @see [EMAIL PROTECTED] Hits#score(int)} + */ + public float getScore() throws IOException { + if (!resolved) fetchTheHit(); + return score; + } + + /** + * Returns id for this hit. + * + * @see [EMAIL PROTECTED] Hits#id(int)} + */ + public int getId() throws IOException { + if (!resolved) fetchTheHit(); + return id; + } + + private void fetchTheHit() throws IOException { + doc = hits.doc(hitNumber); + score = hits.score(hitNumber); + id = hits.id(hitNumber); + resolved = true; + } + + // provide some of the Document style interface (the simple stuff) + + /** + * Returns the boost factor for this hit on any field of the underlying document. + * + * @see [EMAIL PROTECTED] Document#getBoost()} + */ + public float getBoost() throws IOException { + return getDocument().getBoost(); + } + + /** + * Returns the string value of the field with the given name if any exist in + * this document, or null. If multiple fields exist with this name, this + * method returns the first value added. If only binary fields with this name + * exist, returns null. + * + * @see [EMAIL PROTECTED] Document#get(String)} + */ + public String get(String name) throws IOException { + return getDocument().get(name); + } + + /** + * Prints the fields of the underlying document for human consumption. + * <p/> + * If an IOException occurs whilst getting the document, returns null + * + * @see [EMAIL PROTECTED] Document#toString()} + */ + public String toString() { + try { + return getDocument().toString(); + } catch (IOException e) { + return null; + } + } + + +}
Added: lucene/java/trunk/src/java/org/apache/lucene/search/HitIterator.java URL:
======================================================================= =======
http://svn.apache.org/viewcvs/lucene/java/trunk/src/java/org/apache/ lucene/search/Hits.java?rev=164695&r1=164694&r2=164695&view=diff--- lucene/java/trunk/src/java/org/apache/lucene/search/HitIterator.java (added) +++ lucene/java/trunk/src/java/org/apache/lucene/search/HitIterator.java Mon Apr 25 17:21:53 2005 @@ -0,0 +1,78 @@ +/** + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.lucene.search; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +/** + * An iterator over [EMAIL PROTECTED] Hits} that provides lazy fetching of each document. + * [EMAIL PROTECTED] Hits#iterator()} returns an instance of this class. Calls to [EMAIL PROTECTED] #next()} + * return a [EMAIL PROTECTED] Hit} instance. + * + * @author Jeremy Rayner + */ +public class HitIterator implements Iterator { + private Hits hits; + private int hitNumber = 0; + + /** + * Constructed from [EMAIL PROTECTED] Hits#iterator()}. + */ + HitIterator(Hits hits) { + this.hits = hits; + } + + /** + * @return true if current hit is less than the total number of [EMAIL PROTECTED] Hits}. + */ + public boolean hasNext() { + return hitNumber < hits.length(); + } + + /** + * Returns a [EMAIL PROTECTED] Hit} instance representing the next hit in [EMAIL PROTECTED] Hits}. + * + * @return Next [EMAIL PROTECTED] Hit}. + */ + public Object next() { + try { + Object next = new Hit(hits, hitNumber); + hitNumber++; + return next; + } catch (IndexOutOfBoundsException e) { + throw new NoSuchElementException(); + } + } + + /** + * Unsupported operation. + * + * @throws UnsupportedOperationException + */ + public void remove() { + throw new UnsupportedOperationException(); + } + + /** + * Returns the total number of hits. + */ + public int length() { + return hits.length(); + } +} +
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/Hits.java URL:
======================================================================= =======
http://svn.apache.org/viewcvs/lucene/java/trunk/src/test/org/apache/ lucene/TestHitIterator.java?rev=164695&view=auto--- lucene/java/trunk/src/java/org/apache/lucene/search/Hits.java (original) +++ lucene/java/trunk/src/java/org/apache/lucene/search/Hits.java Mon Apr 25 17:21:53 2005 @@ -18,6 +18,7 @@
import java.io.IOException; import java.util.Vector; +import java.util.Iterator;
import org.apache.lucene.document.Document;
@@ -114,6 +115,13 @@ return hitDoc(n).id; }
+ /** + * Returns an [EMAIL PROTECTED] Iterator} to navigate the Hits. Each item returned + * from [EMAIL PROTECTED] Iterator#next()} is a [EMAIL PROTECTED] Hit}. + */ + public Iterator iterator() { + return new HitIterator(this); + }
private final HitDoc hitDoc(int n) throws IOException { if (n >= length) {
Added: lucene/java/trunk/src/test/org/apache/lucene/TestHitIterator.java URL:
======================================================================= =======
--- lucene/java/trunk/src/test/org/apache/lucene/TestHitIterator.java (added) +++ lucene/java/trunk/src/test/org/apache/lucene/TestHitIterator.java Mon Apr 25 17:21:53 2005 @@ -0,0 +1,50 @@ +package org.apache.lucene; + +import junit.framework.TestCase; +import org.apache.lucene.store.RAMDirectory; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.Term; +import org.apache.lucene.analysis.WhitespaceAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.Hits; +import org.apache.lucene.search.Hit; +import org.apache.lucene.search.HitIterator; + +/** + * This test intentionally not put in the search package in order + * to test HitIterator and Hit package protection. + */ +public class TestHitIterator extends TestCase { + public void testIterator() throws Exception { + RAMDirectory directory = new RAMDirectory(); + + IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true); + Document doc = new Document(); + doc.add(new Field("field", "iterator test doc 1", Field.Store.YES, Field.Index.TOKENIZED)); + writer.addDocument(doc); + + doc = new Document(); + doc.add(new Field("field", "iterator test doc 2", Field.Store.YES, Field.Index.TOKENIZED)); + writer.addDocument(doc); + + writer.close(); + + IndexSearcher searcher = new IndexSearcher(directory); + Hits hits = searcher.search(new TermQuery(new Term("field", "iterator"))); + + HitIterator iterator = (HitIterator) hits.iterator(); + assertEquals(2, iterator.length()); + assertTrue(iterator.hasNext()); + Hit hit = (Hit) iterator.next(); + assertEquals("iterator test doc 1", hit.get("field")); + + assertTrue(iterator.hasNext()); + hit = (Hit) iterator.next(); + assertEquals("iterator test doc 2", hit.getDocument().get("field")); + + assertFalse(iterator.hasNext()); + } +}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
