cpoerschke commented on a change in pull request #123: URL: https://github.com/apache/solr/pull/123#discussion_r639920160
########## File path: solr/contrib/ltr/src/java/org/apache/solr/ltr/feature/FieldValueFeature.java ########## @@ -146,5 +201,137 @@ public float getMaxScore(int upTo) throws IOException { return Float.POSITIVE_INFINITY; } } + + /** + * A FeatureScorer that reads the numeric docValues for a field + */ + public final class NumericDocValuesFieldValueFeatureScorer extends FeatureScorer { + private final NumericDocValues docValues; + private final NumberType numberType; + + public NumericDocValuesFieldValueFeatureScorer(final FeatureWeight weight, final LeafReaderContext context, + final DocIdSetIterator itr, final NumberType numberType) { + super(weight, itr); + this.numberType = numberType; + + NumericDocValues docValues; + try { + docValues = DocValues.getNumeric(context.reader(), field); + } catch (IOException e) { + throw new IllegalArgumentException("Could not read numeric docValues for field " + field); + } + this.docValues = docValues; + } + + @Override + public float score() throws IOException { + if (docValues.advanceExact(itr.docID())) { + return readNumericDocValues(); + } + return FieldValueFeature.this.getDefaultValue(); + } + + /** + * Read the numeric value for a field and convert the different number types to float. + * + * @return The numeric value that the docValues contain for the current document + * @throws IOException if docValues cannot be read + */ + private float readNumericDocValues() throws IOException { + if (NumberType.FLOAT.equals(numberType)) { + // convert float value that was stored as long back to float + return Float.intBitsToFloat((int) docValues.longValue()); + } else if (NumberType.DOUBLE.equals(numberType)) { + // handle double value conversion + return (float) Double.longBitsToDouble(docValues.longValue()); + } + // just take the long value + return docValues.longValue(); + } Review comment: > ... I am not really fond of copying the switch-logic that you referenced, but sadly the method is private. ... Likewise, I would have preferred for there to be some existing reusable method and was surprised that there isn't one (or not an obviously findable one). The `SolrDocumentFetcher` one has the `sortableNumeric` flag (which we don't need here) and some special logic for `LatLonPointSpatialField` and `AbstractEnumField` scenarios (also not applicable here) ... otherwise it would have been a clear _"okay, let's factor out a method here and put it somewhere that both SolrDocumentFeature and FieldValueFeature can use it"_ kind of scenario. Oh well. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For additional commands, e-mail: issues-h...@solr.apache.org