Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LUCENE-8949: Allow LeafFieldComparators to publish Feature Values #831

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,10 @@ public Float value(int slot) {
public int compareTop(int doc) throws IOException {
return Float.compare(topValue, getValueForDoc(doc));
}

@Override
public Float leafValue(int docID) throws IOException {
return getValueForDoc(docID);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ public int compareTop(int doc) throws IOException {
return minValue;
}

//TODO: Implement this
@Override
public Double leafValue(int docID) throws IOException {
throw new UnsupportedOperationException("This comparator does not support getting leaf values");
}

// second half of the haversin calculation, used to convert results from haversin1 (used internally
// for sorting) for display purposes.
static double haversin2(double partial) {
Expand Down
40 changes: 40 additions & 0 deletions lucene/core/src/java/org/apache/lucene/search/FieldComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ public Double value(int slot) {
public int compareTop(int doc) throws IOException {
return Double.compare(topValue, getValueForDoc(doc));
}

@Override
public Double leafValue(int docID) throws IOException {
return getValueForDoc(docID);
}
}

/** Parses field's values as float (using {@link
Expand Down Expand Up @@ -279,6 +284,11 @@ public Float value(int slot) {
public int compareTop(int doc) throws IOException {
return Float.compare(topValue, getValueForDoc(doc));
}

@Override
public Float leafValue(int docID) throws IOException {
return getValueForDoc(docID);
}
}

/** Parses field's values as int (using {@link
Expand Down Expand Up @@ -341,6 +351,11 @@ public Integer value(int slot) {
public int compareTop(int doc) throws IOException {
return Integer.compare(topValue, getValueForDoc(doc));
}

@Override
public Integer leafValue(int docID) throws IOException {
return getValueForDoc(docID);
}
}

/** Parses field's values as long (using {@link
Expand Down Expand Up @@ -401,6 +416,11 @@ public Long value(int slot) {
public int compareTop(int doc) throws IOException {
return Long.compare(topValue, getValueForDoc(doc));
}

@Override
public Long leafValue(int docID) throws IOException {
return getValueForDoc(docID);
}
}

/** Sorts by descending relevance. NOTE: if you are
Expand Down Expand Up @@ -484,6 +504,11 @@ public int compareTop(int doc) throws IOException {
assert !Float.isNaN(docValue);
return Float.compare(docValue, topValue);
}

@Override
public Float leafValue(int docID) throws IOException {
return scorer.score();
}
}

/** Sorts by ascending docID */
Expand Down Expand Up @@ -545,6 +570,11 @@ public int compareTop(int doc) {
return Integer.compare(topValue, docValue);
}

@Override
public Integer leafValue(int docID) throws IOException {
return docBase + docID;
}

@Override
public void setScorer(Scorable scorer) {}
}
Expand Down Expand Up @@ -686,6 +716,11 @@ public int compareBottom(int doc) throws IOException {
}
}

@Override
public Integer leafValue(int docID) throws IOException {
return getOrdForDoc(docID);
}

@Override
public void copy(int slot, int doc) throws IOException {
int ord = getOrdForDoc(doc);
Expand Down Expand Up @@ -927,5 +962,10 @@ public int compareTop(int doc) throws IOException {

@Override
public void setScorer(Scorable scorer) {}

@Override
public BytesRef leafValue(int docID) throws IOException {
return getValueForDoc(docID);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
* @see FieldComparator
* @lucene.experimental
*/
public interface LeafFieldComparator {
public interface LeafFieldComparator<T> {

/**
* Set the bottom slot, ie the "weakest" (sorted last)
Expand Down Expand Up @@ -116,4 +116,7 @@ public interface LeafFieldComparator {
* obtain the current hit's score, if necessary. */
void setScorer(Scorable scorer) throws IOException;

/** Publishes feature values for the given docID
*/
T leafValue(int doc) throws IOException;
atris marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,15 @@ public void setScorer(Scorable scorer) throws IOException {
}
}

@Override
public Object leafValue(int docID) throws IOException {
atris marked this conversation as resolved.
Show resolved Hide resolved
Object[] valuesArray = new Object[comparators.length];

for (int i = 0; i < comparators.length; i++) {
valuesArray[i] = comparators[i].leafValue(docID);
}

return valuesArray;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ public void copy(int slot, int doc) throws IOException {

@Override
public void setScorer(Scorable scorer) {}

@Override
public Integer leafValue(int docID) throws IOException {
return docVal(docID);
}
};
}

Expand All @@ -212,7 +217,6 @@ public Integer value(int slot) {
return Integer.valueOf(values[slot]);
}


};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -442,5 +442,10 @@ public int compareTop(int doc) throws IOException {
final double docValue = docVals.doubleVal(doc);
return Double.compare(topValue, docValue);
}

@Override
public Double leafValue(int docID) throws IOException {
return docVals.doubleVal(docID);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ public int compareTop(int doc) throws IOException {
return Double.compare(topValue, computeMinimumDistance(doc));
}

@Override
public Double leafValue(int docID) throws IOException {
return computeMinimumDistance(docID);
}

double computeMinimumDistance(final int doc) throws IOException {
if (doc > currentDocs.docID()) {
currentDocs.advance(doc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public int compareTop(int doc) throws IOException {
return Double.compare(topValue, computeMinimumDistance(doc));
}

@Override
public Double leafValue(int docID) throws IOException {
return computeMinimumDistance(docID);
}

double computeMinimumDistance(final int doc) throws IOException {
if (doc > currentDocs.docID()) {
currentDocs.advance(doc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,11 @@ public int compareTop(int doc) {
final int docValue = docVal(doc);
return topVal - docValue; // values will be small enough that there is no overflow concern
}

@Override
public Integer leafValue(int docID) throws IOException {
return docVal(docID);
}
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ public int compareTop(int doc) {
// values will be positive... no overflow possible.
return topVal - hash(doc+seed);
}

@Override
public Integer leafValue(int docID) throws IOException {
return hash(docID + seed);
}
};
}
};
Expand Down