lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tjo...@apache.org
Subject cvs commit: jakarta-lucene/src/test/org/apache/lucene/search TestSort.java
Date Wed, 24 Mar 2004 19:23:11 GMT
tjones      2004/03/24 11:23:11

  Modified:    src/java/org/apache/lucene/search FieldSortedHitQueue.java
                        FloatSortedHitQueue.java IntegerSortedHitQueue.java
                        StringSortedHitQueue.java
               src/test/org/apache/lucene/search TestSort.java
  Log:
  avoid runtime exception if possible if index is empty
  added tests for this case
  
  Revision  Changes    Path
  1.6       +2 -2      jakarta-lucene/src/java/org/apache/lucene/search/FieldSortedHitQueue.java
  
  Index: FieldSortedHitQueue.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/FieldSortedHitQueue.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- FieldSortedHitQueue.java	17 Mar 2004 18:11:02 -0000	1.5
  +++ FieldSortedHitQueue.java	24 Mar 2004 19:23:11 -0000	1.6
  @@ -185,7 +185,7 @@
   		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();
  
  
  
  1.3       +37 -35    jakarta-lucene/src/java/org/apache/lucene/search/FloatSortedHitQueue.java
  
  Index: FloatSortedHitQueue.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/FloatSortedHitQueue.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- FloatSortedHitQueue.java	24 Feb 2004 19:34:58 -0000	1.2
  +++ FloatSortedHitQueue.java	24 Mar 2004 19:23:11 -0000	1.3
  @@ -81,28 +81,27 @@
   			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 @@
   			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;
   			}
   
  
  
  
  1.3       +37 -35    jakarta-lucene/src/java/org/apache/lucene/search/IntegerSortedHitQueue.java
  
  Index: IntegerSortedHitQueue.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/IntegerSortedHitQueue.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- IntegerSortedHitQueue.java	24 Feb 2004 19:34:58 -0000	1.2
  +++ IntegerSortedHitQueue.java	24 Mar 2004 19:23:11 -0000	1.3
  @@ -83,28 +83,27 @@
   			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 @@
   			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;
   			}
   
  
  
  
  1.4       +97 -92    jakarta-lucene/src/java/org/apache/lucene/search/StringSortedHitQueue.java
  
  Index: StringSortedHitQueue.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/StringSortedHitQueue.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- StringSortedHitQueue.java	24 Feb 2004 20:41:16 -0000	1.3
  +++ StringSortedHitQueue.java	24 Mar 2004 19:23:11 -0000	1.4
  @@ -87,58 +87,59 @@
   
   				final int[] retArray = new int[reader.maxDoc()];
   				final String[] mterms = new String[reader.maxDoc()];   // guess length
  -
  -				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;
  +				if (retArray.length > 0) {
  +					TermEnum enumerator = reader.terms (new Term (field, ""));
  +					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 {
  -					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;
  +						// 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.
  +
  +						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;
  +					}
   				}
  -
   				return retArray;
   			}
   
  @@ -194,51 +195,55 @@
   
   				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 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;
  +					}
   				}
  -
  -				// 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;
   			}
   
  
  
  
  1.4       +26 -1     jakarta-lucene/src/test/org/apache/lucene/search/TestSort.java
  
  Index: TestSort.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/test/org/apache/lucene/search/TestSort.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestSort.java	23 Mar 2004 16:49:56 -0000	1.3
  +++ TestSort.java	24 Mar 2004 19:23:11 -0000	1.4
  @@ -138,6 +138,11 @@
   		return getIndex (false, true);
   	}
   
  +	private Searcher getEmptyIndex()
  +	throws IOException {
  +		return getIndex (false, false);
  +	}
  +
   	public void setUp() throws Exception {
   		full = getFullIndex();
   		searchX = getXIndex();
  @@ -172,6 +177,26 @@
   		sort.setSort (new SortField[] { new SortField ("string", SortField.STRING), SortField.FIELD_DOC
});
   		assertMatches (full, queryX, sort, "AIGEC");
   		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
  
  
  

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


Mime
View raw message