Apache Solr open-source search software
SOLR-1932: add sumtotaltermfreq
Yonik Seeley06a377890560
diff --git a/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/SumTotalTermFreqValueSource.java b/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/SumTotalTermFreqValueSource.javanew file mode 100644index 00000000000..8bc1ea5f314--- /dev/null+++ b/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/SumTotalTermFreqValueSource.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.Fields;+import org.apache.lucene.index.IndexReader;+import org.apache.lucene.index.Terms;+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 SumTotalTermFreqValueSource extends ValueSource {+ protected String indexedField;++ public SumTotalTermFreqValueSource(String indexedField) {+ this.indexedField = indexedField;+ }++ public String name() {+ return "sumtotaltermfreq";+ }++ @Override+ public String description() {+ return name() + '(' + indexedField + ')';+ }++ @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 sumTotalTermFreq = 0;+ for (IndexReader.AtomicReaderContext readerContext : searcher.getTopReaderContext().leaves()) {+ Fields fields = readerContext.reader.fields();+ if (fields == null) continue;+ Terms terms = fields.terms(indexedField);+ if (terms == null) continue;+ sumTotalTermFreq += terms.getSumTotalTermFreq();+ }+ final long ttf = Math.max(-1, sumTotalTermFreq); // we may have added up -1s if not supported+ context.put(this, new LongDocValues(this) {+ @Override+ public long longVal(int doc) {+ return ttf;+ }+ });+ }++ @Override+ public int hashCode() {+ return getClass().hashCode() + indexedField.hashCode();+ }++ @Override+ public boolean equals(Object o) {+ if (this.getClass() != o.getClass()) return false;+ SumTotalTermFreqValueSource other = (SumTotalTermFreqValueSource)o;+ return this.indexedField.equals(other.indexedField);+ }+}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.javaindex 2d4ae1fc1e8..21a0b2eff16 100644--- 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@@ -64,7 +64,7 @@ public class TotalTermFreqValueSource extends ValueSource {for (IndexReader.AtomicReaderContext readerContext : searcher.getTopReaderContext().leaves()) {totalTermFreq += readerContext.reader.totalTermFreq(indexedField, indexedBytes);}- final long ttf = totalTermFreq;+ final long ttf = Math.max(-1, totalTermFreq); // we may have added up -1s if not supportedcontext.put(this, new LongDocValues(this) {@Overridepublic long longVal(int doc) {diff --git a/solr/src/java/org/apache/solr/search/ValueSourceParser.java b/solr/src/java/org/apache/solr/search/ValueSourceParser.javaindex e49fc5d8bcc..8999ac8b7dc 100755--- a/solr/src/java/org/apache/solr/search/ValueSourceParser.java+++ b/solr/src/java/org/apache/solr/search/ValueSourceParser.java@@ -547,6 +547,16 @@ public abstract class ValueSourceParser implements NamedListInitializedPlugin {return new TotalTermFreqValueSource(tinfo.field, tinfo.val, tinfo.indexedField, tinfo.indexedBytes);}});+ alias("totaltermfreq","ttf");++ addParser("sumtotaltermfreq", new ValueSourceParser() {+ @Override+ public ValueSource parse(FunctionQParser fp) throws ParseException {+ String field = fp.parseArg();+ return new SumTotalTermFreqValueSource(field);+ }+ });+ alias("sumtotaltermfreq","sttf");addParser("idf", new ValueSourceParser() {@Overridediff --git a/solr/src/test/org/apache/solr/search/function/TestFunctionQuery.java b/solr/src/test/org/apache/solr/search/function/TestFunctionQuery.javaindex 5d5aece809b..ba86864ec8a 100755--- a/solr/src/test/org/apache/solr/search/function/TestFunctionQuery.java+++ b/solr/src/test/org/apache/solr/search/function/TestFunctionQuery.java@@ -415,6 +415,9 @@ public class TestFunctionQuery extends SolrTestCaseJ4 {assertU(adoc("id","6", "a_t","cow cow cow cow cow"));assertU(commit());assertQ(req("fl","*,score","q", "{!func}totaltermfreq('a_t','cow')", "fq","id:6"), "//float[@name='score']='7.0'");+ assertQ(req("fl","*,score","q", "{!func}ttf(a_t,'cow')", "fq","id:6"), "//float[@name='score']='7.0'");+ assertQ(req("fl","*,score","q", "{!func}sumtotaltermfreq('a_t')", "fq","id:6"), "//float[@name='score']='11.0'");+ assertQ(req("fl","*,score","q", "{!func}sttf(a_t)", "fq","id:6"), "//float[@name='score']='11.0'");}@Test