jpountz commented on a change in pull request #687: LUCENE-8815: Adds a 
DoubleValues implementation for feature fields
URL: https://github.com/apache/lucene-solr/pull/687#discussion_r290174093
 
 

 ##########
 File path: 
lucene/core/src/java/org/apache/lucene/document/FeatureDoubleValuesSource.java
 ##########
 @@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.document;
+
+import java.io.IOException;
+import java.util.Objects;
+
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.PostingsEnum;
+import org.apache.lucene.index.Terms;
+import org.apache.lucene.index.TermsEnum;
+import org.apache.lucene.search.DoubleValues;
+import org.apache.lucene.search.DoubleValuesSource;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.util.BytesRef;
+
+/**
+ * A {@link DoubleValuesSource} instance which can be used to read the values 
of a feature from the a 
+ * {@link FeatureField} for documents.
+ */
+public class FeatureDoubleValuesSource extends DoubleValuesSource {
+  
+  private BytesRef featureName;
+  private String field;
+
+  /**
+   * Creates a {@link DoubleValuesSource} instance which can be used to read 
the values of a feature from the a 
+   * {@link FeatureField} for documents.
+   * 
+   * @param field field name. Must not be null.
+   * @param featureName feature name. Must not be null.
+   * @throws NullPointerException if {@code field} or {@code featureName} is 
null.
+   */
+  public FeatureDoubleValuesSource(String field, BytesRef featureName) {
+    this.field = Objects.requireNonNull(field);
+    this.featureName = Objects.requireNonNull(featureName);
+  }
+
+  @Override
+  public boolean isCacheable(LeafReaderContext ctx) {
+    return false;
+  }
+
+  @Override
+  public DoubleValues getValues(LeafReaderContext ctx, DoubleValues scores) 
throws IOException {
+    Terms terms = ctx.reader().terms(field);
+    if (terms == null) {
+      return DoubleValues.EMPTY;
+    } else {
+      TermsEnum termsEnum = terms.iterator();
+      if (termsEnum.seekExact(featureName) == false) {
+        return DoubleValues.EMPTY;
+      } else {
+        PostingsEnum currentReaderPostingsValues = termsEnum.postings(null, 
PostingsEnum.FREQS);
+        return new FeatureDoubleValues(currentReaderPostingsValues);
+      }
+    }
+  }
+
+  @Override
+  public boolean needsScores() {
+    return false;
+  }
+
+  @Override
+  public DoubleValuesSource rewrite(IndexSearcher reader) throws IOException {
+    return this;
+  }
+
+  @Override
+  public int hashCode() {
+    return Objects.hash(field, featureName);
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (obj == null) {
+      return false;
+    }
+    if (obj.getClass() != getClass()) {
+      return false;
+    }
+    FeatureDoubleValuesSource other = (FeatureDoubleValuesSource) obj;
+    return Objects.equals(field, other.field) &&
+        Objects.equals(featureName, other.featureName);
+  }
+
+  @Override
+  public String toString() {
+    return "FeatureDoubleValuesSource("+field+", "+featureName+")";
 
 Review comment:
   The toString representation will not look nice due to how BytesRef#toString 
works. Maybe we should take the featureName as a String in the constructor to 
make sure it is a String and not random bytes, and either use the String here, 
or the BytesRef#utf8ToString representation of the BytesRef.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to