Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-1932: add totaltermfreq to relevancy functions

Yonik Seeley3f5c0eca5bd3
diff --git a/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/TotalTermFreqValueSource.java b/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/TotalTermFreqValueSource.java
new file mode 100644
index 00000000000..2d4ae1fc1e8
--- /dev/null
+++ b/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/TotalTermFreqValueSource.java
@@ -0,0 +1,87 @@
+/**
+ * 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.queries.function.valuesource;
+
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.queries.function.DocValues;
+import org.apache.lucene.queries.function.ValueSource;
+import org.apache.lucene.queries.function.docvalues.LongDocValues;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.util.BytesRef;
+
+import java.io.IOException;
+import java.util.Map;
+
+/**
+ * <code>TotalTermFreqValueSource</code> returns the total term freq (sum of term freqs across all docuyments).
+ * @lucene.internal
+ */
+public class TotalTermFreqValueSource extends ValueSource {
+ protected String field;
+ protected String indexedField;
+ protected String val;
+ protected BytesRef indexedBytes;
+
+ public TotalTermFreqValueSource(String field, String val, String indexedField, BytesRef indexedBytes) {
+ this.field = field;
+ this.val = val;
+ this.indexedField = indexedField;
+ this.indexedBytes = indexedBytes;
+ }
+
+ public String name() {
+ return "totaltermfreq";
+ }
+
+ @Override
+ public String description() {
+ return name() + '(' + field + ',' + val + ')';
+ }
+
+ @Override
+ public DocValues getValues(Map context, IndexReader.AtomicReaderContext readerContext) throws IOException {
+ return (DocValues)context.get(this);
+ }
+
+ @Override
+ public void createWeight(Map context, IndexSearcher searcher) throws IOException {
+ long totalTermFreq = 0;
+ for (IndexReader.AtomicReaderContext readerContext : searcher.getTopReaderContext().leaves()) {
+ totalTermFreq += readerContext.reader.totalTermFreq(indexedField, indexedBytes);
+ }
+ final long ttf = totalTermFreq;
+ context.put(this, new LongDocValues(this) {
+ @Override
+ public long longVal(int doc) {
+ return ttf;
+ }
+ });
+ }
+
+ @Override
+ public int hashCode() {
+ return getClass().hashCode() + indexedField.hashCode()*29 + indexedBytes.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this.getClass() != o.getClass()) return false;
+ TotalTermFreqValueSource other = (TotalTermFreqValueSource)o;
+ return this.indexedField.equals(other.indexedField) && this.indexedBytes.equals(other.indexedBytes);
+ }
+}