From 9e24bfacfce56d7b81dbb5a6e9b4957d59e35015 Mon Sep 17 00:00:00 2001 From: Tim Jones Date: Wed, 24 Mar 2004 19:23:11 +0000 Subject: [PATCH] avoid runtime exception if possible if index is empty added tests for this case git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150246 13f79535-47bb-0310-9956-ffa450edef68 --- .../lucene/search/FieldSortedHitQueue.java | 2 +- .../lucene/search/FloatSortedHitQueue.java | 70 +++---- .../lucene/search/IntegerSortedHitQueue.java | 70 +++---- .../lucene/search/StringSortedHitQueue.java | 181 +++++++++--------- .../org/apache/lucene/search/TestSort.java | 25 +++ 5 files changed, 191 insertions(+), 157 deletions(-) diff --git a/src/java/org/apache/lucene/search/FieldSortedHitQueue.java b/src/java/org/apache/lucene/search/FieldSortedHitQueue.java index 29f027a6dfa..d2c61767100 100644 --- a/src/java/org/apache/lucene/search/FieldSortedHitQueue.java +++ b/src/java/org/apache/lucene/search/FieldSortedHitQueue.java @@ -185,7 +185,7 @@ extends PriorityQueue { try { Term term = enumerator.term(); if (term == null) { - throw new RuntimeException ("no terms in field "+field); + throw new RuntimeException ("no terms in field "+field+" - cannot determine sort type"); } if (term.field() == field) { String termtext = term.text().trim(); diff --git a/src/java/org/apache/lucene/search/FloatSortedHitQueue.java b/src/java/org/apache/lucene/search/FloatSortedHitQueue.java index 1a476818cc7..ed756836642 100644 --- a/src/java/org/apache/lucene/search/FloatSortedHitQueue.java +++ b/src/java/org/apache/lucene/search/FloatSortedHitQueue.java @@ -81,28 +81,27 @@ extends FieldSortedHitQueue { throws IOException { float[] retArray = new float[reader.maxDoc()]; - - TermEnum enumerator = reader.terms (new Term (field, "")); - TermDocs termDocs = reader.termDocs (); - if (enumerator.term () == null) { - throw new RuntimeException ("no terms in field " + field); - } - - try { - do { - Term term = enumerator.term (); - if (term.field () != field) break; - float termval = Float.parseFloat (term.text()); - termDocs.seek (enumerator); - while (termDocs.next ()) { - retArray[termDocs.doc ()] = termval; + if (retArray.length > 0) { + TermEnum enumerator = reader.terms (new Term (field, "")); + TermDocs termDocs = reader.termDocs(); + try { + if (enumerator.term() == null) { + throw new RuntimeException ("no terms in field " + field); } - } while (enumerator.next ()); - } finally { - enumerator.close (); - termDocs.close (); + do { + Term term = enumerator.term(); + if (term.field() != field) break; + float termval = Float.parseFloat (term.text()); + termDocs.seek (enumerator); + while (termDocs.next()) { + retArray[termDocs.doc()] = termval; + } + } while (enumerator.next()); + } finally { + enumerator.close(); + termDocs.close(); + } } - return retArray; } @@ -156,22 +155,25 @@ extends FieldSortedHitQueue { throws IOException { float[] retArray = new float[reader.maxDoc()]; - - TermDocs termDocs = reader.termDocs (); - try { - do { - Term term = enumerator.term(); - if (term.field() != field) break; - float termval = Float.parseFloat (term.text()); - termDocs.seek (enumerator); - while (termDocs.next()) { - retArray[termDocs.doc()] = termval; + if (retArray.length > 0) { + TermDocs termDocs = reader.termDocs (); + try { + if (enumerator.term() == null) { + throw new RuntimeException ("no terms in field "+field); } - } while (enumerator.next()); - } finally { - termDocs.close(); + do { + Term term = enumerator.term(); + if (term.field() != field) break; + float termval = Float.parseFloat (term.text()); + termDocs.seek (enumerator); + while (termDocs.next()) { + retArray[termDocs.doc()] = termval; + } + } while (enumerator.next()); + } finally { + termDocs.close(); + } } - return retArray; } diff --git a/src/java/org/apache/lucene/search/IntegerSortedHitQueue.java b/src/java/org/apache/lucene/search/IntegerSortedHitQueue.java index 9f8c6f73ad2..79084a4df26 100644 --- a/src/java/org/apache/lucene/search/IntegerSortedHitQueue.java +++ b/src/java/org/apache/lucene/search/IntegerSortedHitQueue.java @@ -83,28 +83,27 @@ extends FieldSortedHitQueue { throws IOException { final int[] retArray = new int[reader.maxDoc()]; - - TermEnum enumerator = reader.terms (new Term (field, "")); - TermDocs termDocs = reader.termDocs(); - if (enumerator.term() == null) { - throw new RuntimeException ("no terms in field "+field); - } - - try { - do { - Term term = enumerator.term(); - if (term.field() != field) break; - int termval = Integer.parseInt (term.text()); - termDocs.seek (enumerator); - while (termDocs.next()) { - retArray[termDocs.doc()] = termval; + if (retArray.length > 0) { + TermEnum enumerator = reader.terms (new Term (field, "")); + TermDocs termDocs = reader.termDocs(); + try { + if (enumerator.term() == null) { + throw new RuntimeException ("no terms in field "+field); } - } while (enumerator.next()); - } finally { - enumerator.close(); - termDocs.close(); + do { + Term term = enumerator.term(); + if (term.field() != field) break; + int termval = Integer.parseInt (term.text()); + termDocs.seek (enumerator); + while (termDocs.next()) { + retArray[termDocs.doc()] = termval; + } + } while (enumerator.next()); + } finally { + enumerator.close(); + termDocs.close(); + } } - return retArray; } @@ -158,22 +157,25 @@ extends FieldSortedHitQueue { throws IOException { final int[] retArray = new int[reader.maxDoc()]; - - TermDocs termDocs = reader.termDocs(); - try { - do { - Term term = enumerator.term(); - if (term.field() != field) break; - int termval = Integer.parseInt (term.text()); - termDocs.seek (enumerator); - while (termDocs.next()) { - retArray[termDocs.doc()] = termval; + if (retArray.length > 0) { + TermDocs termDocs = reader.termDocs(); + try { + if (enumerator.term() == null) { + throw new RuntimeException ("no terms in field "+field); } - } while (enumerator.next()); - } finally { - termDocs.close(); + do { + Term term = enumerator.term(); + if (term.field() != field) break; + int termval = Integer.parseInt (term.text()); + termDocs.seek (enumerator); + while (termDocs.next()) { + retArray[termDocs.doc()] = termval; + } + } while (enumerator.next()); + } finally { + termDocs.close(); + } } - return retArray; } diff --git a/src/java/org/apache/lucene/search/StringSortedHitQueue.java b/src/java/org/apache/lucene/search/StringSortedHitQueue.java index 7fc5109374b..ce239be7b03 100644 --- a/src/java/org/apache/lucene/search/StringSortedHitQueue.java +++ b/src/java/org/apache/lucene/search/StringSortedHitQueue.java @@ -87,58 +87,59 @@ extends FieldSortedHitQueue { final int[] retArray = new int[reader.maxDoc()]; final String[] mterms = new String[reader.maxDoc()]; // guess length + if (retArray.length > 0) { + TermEnum enumerator = reader.terms (new Term (field, "")); + TermDocs termDocs = reader.termDocs(); - TermEnum enumerator = reader.terms (new Term (field, "")); - TermDocs termDocs = reader.termDocs(); - if (enumerator.term() == null) { - throw new RuntimeException ("no terms in field " + field); - } - - // NOTE: the contract for TermEnum says the - // terms will be in natural order (which is - // ordering by field name, term text). The - // contract for TermDocs says the docs will - // be ordered by document number. So the - // following loop will automatically sort the - // terms in the correct order. - - // if a given document has more than one term - // in the field, only the last one will be used. - - int t = 0; // current term number - try { - do { - Term term = enumerator.term(); - if (term.field() != field) break; - - // store term text - // we expect that there is at most one term per document - if (t >= mterms.length) throw new RuntimeException ("there are more terms than documents in field \""+field+"\""); - mterms[t] = term.text(); - - // store which documents use this term - termDocs.seek (enumerator); - while (termDocs.next()) { - retArray[termDocs.doc()] = t; + int t = 0; // current term number + try { + if (enumerator.term() == null) { + throw new RuntimeException ("no terms in field " + field); } - t++; - } while (enumerator.next()); + // NOTE: the contract for TermEnum says the + // terms will be in natural order (which is + // ordering by field name, term text). The + // contract for TermDocs says the docs will + // be ordered by document number. So the + // following loop will automatically sort the + // terms in the correct order. - } finally { - enumerator.close(); - termDocs.close(); + // if a given document has more than one term + // in the field, only the last one will be used. + + do { + Term term = enumerator.term(); + if (term.field() != field) break; + + // store term text + // we expect that there is at most one term per document + if (t >= mterms.length) throw new RuntimeException ("there are more terms than documents in field \""+field+"\""); + mterms[t] = term.text(); + + // store which documents use this term + termDocs.seek (enumerator); + while (termDocs.next()) { + retArray[termDocs.doc()] = t; + } + + t++; + } while (enumerator.next()); + + } finally { + enumerator.close(); + termDocs.close(); + } + + // if there are less terms than documents, + // trim off the dead array space + if (t < mterms.length) { + terms = new String[t]; + System.arraycopy (mterms, 0, terms, 0, t); + } else { + terms = mterms; + } } - - // if there are less terms than documents, - // trim off the dead array space - if (t < mterms.length) { - terms = new String[t]; - System.arraycopy (mterms, 0, terms, 0, t); - } else { - terms = mterms; - } - return retArray; } @@ -194,51 +195,55 @@ extends FieldSortedHitQueue { final int[] retArray = new int[reader.maxDoc()]; final String[] mterms = new String[reader.maxDoc()]; // guess length - - // NOTE: the contract for TermEnum says the - // terms will be in natural order (which is - // ordering by field name, term text). The - // contract for TermDocs says the docs will - // be ordered by document number. So the - // following loop will automatically sort the - // terms in the correct order. - - // if a given document has more than one term - // in the field, only the last one will be used. - - TermDocs termDocs = reader.termDocs(); - int t = 0; // current term number - try { - do { - Term term = enumerator.term(); - if (term.field() != field) break; - - // store term text - // we expect that there is at most one term per document - if (t >= mterms.length) throw new RuntimeException ("there are more terms than documents in field \""+field+"\""); - mterms[t] = term.text(); - - // store which documents use this term - termDocs.seek (enumerator); - while (termDocs.next()) { - retArray[termDocs.doc()] = t; + if (retArray.length > 0) { + TermDocs termDocs = reader.termDocs(); + int t = 0; // current term number + try { + if (enumerator.term() == null) { + throw new RuntimeException ("no terms in field " + field); } - t++; - } while (enumerator.next()); - } finally { - termDocs.close(); - } + // NOTE: the contract for TermEnum says the + // terms will be in natural order (which is + // ordering by field name, term text). The + // contract for TermDocs says the docs will + // be ordered by document number. So the + // following loop will automatically sort the + // terms in the correct order. - // if there are less terms than documents, - // trim off the dead array space - if (t < mterms.length) { - terms = new String[t]; - System.arraycopy (mterms, 0, terms, 0, t); - } else { - terms = mterms; - } + // if a given document has more than one term + // in the field, only the last one will be used. + do { + Term term = enumerator.term(); + if (term.field() != field) break; + + // store term text + // we expect that there is at most one term per document + if (t >= mterms.length) throw new RuntimeException ("there are more terms than documents in field \""+field+"\""); + mterms[t] = term.text(); + + // store which documents use this term + termDocs.seek (enumerator); + while (termDocs.next()) { + retArray[termDocs.doc()] = t; + } + + t++; + } while (enumerator.next()); + } finally { + termDocs.close(); + } + + // if there are less terms than documents, + // trim off the dead array space + if (t < mterms.length) { + terms = new String[t]; + System.arraycopy (mterms, 0, terms, 0, t); + } else { + terms = mterms; + } + } return retArray; } diff --git a/src/test/org/apache/lucene/search/TestSort.java b/src/test/org/apache/lucene/search/TestSort.java index a86c15a5a1a..8505b7defae 100644 --- a/src/test/org/apache/lucene/search/TestSort.java +++ b/src/test/org/apache/lucene/search/TestSort.java @@ -138,6 +138,11 @@ extends TestCase { return getIndex (false, true); } + private Searcher getEmptyIndex() + throws IOException { + return getIndex (false, false); + } + public void setUp() throws Exception { full = getFullIndex(); searchX = getXIndex(); @@ -174,6 +179,26 @@ extends TestCase { assertMatches (full, queryY, sort, "DJHFB"); } + // test sorts when there's nothing in the index + public void testEmptyIndex() throws Exception { + Searcher empty = getEmptyIndex(); + + sort = new Sort(); + assertMatches (empty, queryX, sort, ""); + + sort.setSort(SortField.FIELD_DOC); + assertMatches (empty, queryX, sort, ""); + + sort.setSort (new SortField[] { new SortField ("int", SortField.INT), SortField.FIELD_DOC }); + assertMatches (empty, queryX, sort, ""); + + sort.setSort (new SortField[] { new SortField ("string", SortField.STRING, true), SortField.FIELD_DOC }); + assertMatches (empty, queryX, sort, ""); + + sort.setSort (new SortField[] { new SortField ("float", SortField.FLOAT), new SortField ("string", SortField.STRING) }); + assertMatches (empty, queryX, sort, ""); + } + // test sorts where the type of field is determined dynamically public void testAutoSort() throws Exception { sort.setSort("int");