mirror of https://github.com/apache/lucene.git
streamlined by removing duplicated code
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150257 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
338499e86b
commit
3f54fbaaea
|
@ -72,67 +72,8 @@ extends FieldSortedHitQueue {
|
|||
*/
|
||||
static ScoreDocLookupComparator comparator (final IndexReader reader, final String fieldname)
|
||||
throws IOException {
|
||||
final String field = fieldname.intern();
|
||||
return new ScoreDocLookupComparator () {
|
||||
|
||||
protected final float[] fieldOrder = generateSortIndex();
|
||||
|
||||
protected final float[] generateSortIndex()
|
||||
throws IOException {
|
||||
|
||||
float[] retArray = new float[reader.maxDoc()];
|
||||
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);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
public final int compare (final ScoreDoc i, final ScoreDoc j) {
|
||||
final float fi = fieldOrder[i.doc];
|
||||
final float fj = fieldOrder[j.doc];
|
||||
if (fi < fj) return -1;
|
||||
if (fi > fj) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public final int compareReverse (final ScoreDoc i, final ScoreDoc j) {
|
||||
final float fi = fieldOrder[i.doc];
|
||||
final float fj = fieldOrder[j.doc];
|
||||
if (fi > fj) return -1;
|
||||
if (fi < fj) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public final boolean sizeMatches (final int n) {
|
||||
return fieldOrder.length == n;
|
||||
}
|
||||
|
||||
public Object sortValue (final ScoreDoc i) {
|
||||
return new Float (fieldOrder[i.doc]);
|
||||
}
|
||||
|
||||
public int sortType() {
|
||||
return SortField.FLOAT;
|
||||
}
|
||||
};
|
||||
TermEnum enumerator = reader.terms (new Term (fieldname, ""));
|
||||
return comparator (reader, enumerator, fieldname);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -73,68 +73,8 @@ extends FieldSortedHitQueue {
|
|||
*/
|
||||
static ScoreDocLookupComparator comparator (final IndexReader reader, final String fieldname)
|
||||
throws IOException {
|
||||
final String field = fieldname.intern();
|
||||
return new ScoreDocLookupComparator() {
|
||||
|
||||
/** The sort information being used by this instance */
|
||||
protected final int[] fieldOrder = generateSortIndex();
|
||||
|
||||
private final int[] generateSortIndex()
|
||||
throws IOException {
|
||||
|
||||
final int[] retArray = new int[reader.maxDoc()];
|
||||
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);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
public final int compare (final ScoreDoc i, final ScoreDoc j) {
|
||||
final int fi = fieldOrder[i.doc];
|
||||
final int fj = fieldOrder[j.doc];
|
||||
if (fi < fj) return -1;
|
||||
if (fi > fj) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public final int compareReverse (final ScoreDoc i, final ScoreDoc j) {
|
||||
final int fi = fieldOrder[i.doc];
|
||||
final int fj = fieldOrder[j.doc];
|
||||
if (fi > fj) return -1;
|
||||
if (fi < fj) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public final boolean sizeMatches (final int n) {
|
||||
return fieldOrder.length == n;
|
||||
}
|
||||
|
||||
public Object sortValue (final ScoreDoc i) {
|
||||
return new Integer (fieldOrder[i.doc]);
|
||||
}
|
||||
|
||||
public int sortType() {
|
||||
return SortField.INT;
|
||||
}
|
||||
};
|
||||
TermEnum enumerator = reader.terms (new Term (fieldname, ""));
|
||||
return comparator (reader, enumerator, fieldname);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -75,102 +75,8 @@ extends FieldSortedHitQueue {
|
|||
*/
|
||||
static ScoreDocLookupComparator comparator (final IndexReader reader, final String fieldname)
|
||||
throws IOException {
|
||||
final String field = fieldname.intern();
|
||||
return new ScoreDocLookupComparator() {
|
||||
|
||||
/** The sort information being used by this instance */
|
||||
protected final int[] fieldOrder = generateSortIndex();
|
||||
protected String[] terms;
|
||||
|
||||
private final int[] generateSortIndex()
|
||||
throws IOException {
|
||||
|
||||
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();
|
||||
|
||||
int t = 0; // current term number
|
||||
try {
|
||||
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.
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public final int compare (final ScoreDoc i, final ScoreDoc j) {
|
||||
final int fi = fieldOrder[i.doc];
|
||||
final int fj = fieldOrder[j.doc];
|
||||
if (fi < fj) return -1;
|
||||
if (fi > fj) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public final int compareReverse (final ScoreDoc i, final ScoreDoc j) {
|
||||
final int fi = fieldOrder[i.doc];
|
||||
final int fj = fieldOrder[j.doc];
|
||||
if (fi > fj) return -1;
|
||||
if (fi < fj) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public final boolean sizeMatches (final int n) {
|
||||
return fieldOrder.length == n;
|
||||
}
|
||||
|
||||
public Object sortValue (final ScoreDoc i) {
|
||||
return terms[fieldOrder[i.doc]];
|
||||
}
|
||||
|
||||
public int sortType() {
|
||||
return SortField.STRING;
|
||||
}
|
||||
};
|
||||
TermEnum enumerator = reader.terms (new Term (fieldname, ""));
|
||||
return comparator (reader, enumerator, fieldname);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue