You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by yo...@apache.org on 2011/07/05 17:06:25 UTC

svn commit: r1143091 - in /lucene/dev/trunk: modules/queries/src/java/org/apache/lucene/queries/function/valuesource/ solr/src/java/org/apache/solr/search/ solr/src/test/org/apache/solr/search/function/

Author: yonik
Date: Tue Jul  5 15:06:24 2011
New Revision: 1143091

URL: http://svn.apache.org/viewvc?rev=1143091&view=rev
Log:
SOLR-1932: add sumtotaltermfreq

Added:
    lucene/dev/trunk/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/SumTotalTermFreqValueSource.java   (with props)
Modified:
    lucene/dev/trunk/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/TotalTermFreqValueSource.java
    lucene/dev/trunk/solr/src/java/org/apache/solr/search/ValueSourceParser.java
    lucene/dev/trunk/solr/src/test/org/apache/solr/search/function/TestFunctionQuery.java

Added: lucene/dev/trunk/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/SumTotalTermFreqValueSource.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/SumTotalTermFreqValueSource.java?rev=1143091&view=auto
==============================================================================
--- lucene/dev/trunk/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/SumTotalTermFreqValueSource.java (added)
+++ lucene/dev/trunk/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/SumTotalTermFreqValueSource.java Tue Jul  5 15:06:24 2011
@@ -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);
+  }
+}

Modified: lucene/dev/trunk/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/TotalTermFreqValueSource.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/TotalTermFreqValueSource.java?rev=1143091&r1=1143090&r2=1143091&view=diff
==============================================================================
--- lucene/dev/trunk/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/TotalTermFreqValueSource.java (original)
+++ lucene/dev/trunk/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/TotalTermFreqValueSource.java Tue Jul  5 15:06:24 2011
@@ -64,7 +64,7 @@ public class TotalTermFreqValueSource ex
     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 supported
     context.put(this, new LongDocValues(this) {
       @Override
       public long longVal(int doc) {

Modified: lucene/dev/trunk/solr/src/java/org/apache/solr/search/ValueSourceParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/src/java/org/apache/solr/search/ValueSourceParser.java?rev=1143091&r1=1143090&r2=1143091&view=diff
==============================================================================
--- lucene/dev/trunk/solr/src/java/org/apache/solr/search/ValueSourceParser.java (original)
+++ lucene/dev/trunk/solr/src/java/org/apache/solr/search/ValueSourceParser.java Tue Jul  5 15:06:24 2011
@@ -547,6 +547,16 @@ public abstract class ValueSourceParser 
         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() {
       @Override

Modified: lucene/dev/trunk/solr/src/test/org/apache/solr/search/function/TestFunctionQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/src/test/org/apache/solr/search/function/TestFunctionQuery.java?rev=1143091&r1=1143090&r2=1143091&view=diff
==============================================================================
--- lucene/dev/trunk/solr/src/test/org/apache/solr/search/function/TestFunctionQuery.java (original)
+++ lucene/dev/trunk/solr/src/test/org/apache/solr/search/function/TestFunctionQuery.java Tue Jul  5 15:06:24 2011
@@ -415,6 +415,9 @@ public class TestFunctionQuery extends S
     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