lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yo...@apache.org
Subject svn commit: r1142481 - /lucene/dev/trunk/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/TotalTermFreqValueSource.java
Date Sun, 03 Jul 2011 19:02:58 GMT
Author: yonik
Date: Sun Jul  3 19:02:57 2011
New Revision: 1142481

URL: http://svn.apache.org/viewvc?rev=1142481&view=rev
Log:
SOLR-1932: add totaltermfreq to relevancy functions

Added:
    lucene/dev/trunk/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/TotalTermFreqValueSource.java
  (with props)

Added: 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=1142481&view=auto
==============================================================================
--- lucene/dev/trunk/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/TotalTermFreqValueSource.java
(added)
+++ lucene/dev/trunk/modules/queries/src/java/org/apache/lucene/queries/function/valuesource/TotalTermFreqValueSource.java
Sun Jul  3 19:02:57 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.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);
+  }
+}



Mime
View raw message