You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ry...@apache.org on 2010/08/23 23:56:16 UTC

svn commit: r988330 - in /lucene/dev/trunk/solr/src: java/org/apache/solr/search/ValueSourceParser.java java/org/apache/solr/search/function/JoinDocFreqValueSource.java test/org/apache/solr/search/function/SortByFunctionTest.java

Author: ryan
Date: Mon Aug 23 21:56:16 2010
New Revision: 988330

URL: http://svn.apache.org/viewvc?rev=988330&view=rev
Log:
SOLR-1694 -- use FieldCache value from one field to get the DocFreq for another field. 

Added:
    lucene/dev/trunk/solr/src/java/org/apache/solr/search/function/JoinDocFreqValueSource.java
Modified:
    lucene/dev/trunk/solr/src/java/org/apache/solr/search/ValueSourceParser.java
    lucene/dev/trunk/solr/src/test/org/apache/solr/search/function/SortByFunctionTest.java

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=988330&r1=988329&r2=988330&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 Mon Aug 23 21:56:16 2010
@@ -229,6 +229,14 @@ public abstract class ValueSourceParser 
         return new QueryValueSource(bq, 0.0f);
       }
     });
+    addParser("joindf", new ValueSourceParser() {
+      public ValueSource parse(FunctionQParser fp) throws ParseException {
+        String f0 = fp.parseArg();
+        String qf = fp.parseArg();
+        return new JoinDocFreqValueSource( f0, qf );
+      }
+    });
+    
     addParser("hsin", new ValueSourceParser() {
       public ValueSource parse(FunctionQParser fp) throws ParseException {
 

Added: lucene/dev/trunk/solr/src/java/org/apache/solr/search/function/JoinDocFreqValueSource.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/src/java/org/apache/solr/search/function/JoinDocFreqValueSource.java?rev=988330&view=auto
==============================================================================
--- lucene/dev/trunk/solr/src/java/org/apache/solr/search/function/JoinDocFreqValueSource.java (added)
+++ lucene/dev/trunk/solr/src/java/org/apache/solr/search/function/JoinDocFreqValueSource.java Mon Aug 23 21:56:16 2010
@@ -0,0 +1,100 @@
+/**
+ * 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.solr.search.function;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.search.FieldCache.DocTerms;
+import org.apache.lucene.util.BytesRef;
+
+/**
+ * Use a field value and find the Document Frequency within another field.
+ * 
+ * @since solr 4.0
+ */
+public class JoinDocFreqValueSource extends FieldCacheSource {
+
+  public static final String NAME = "joindf";
+  
+  protected final String qfield;
+  
+  public JoinDocFreqValueSource(String field, String qfield) {
+    super(field);
+    this.qfield = qfield;
+  }
+
+  public String description() {
+    return NAME + "(" + field +":("+qfield+"))";
+  }
+
+  public DocValues getValues(Map context, final IndexReader reader) throws IOException 
+  {
+    final DocTerms terms = cache.getTerms(reader, field, true );
+    
+    return new DocValues() {
+
+      public int intVal(int doc) 
+      {
+        try {
+          BytesRef ref = new BytesRef();
+          terms.getTerm(doc, ref);
+          int v = reader.docFreq( qfield, ref ); 
+          //System.out.println( NAME+"["+ref.utf8ToString()+"="+v+"]" );
+          return v;
+        } 
+        catch (IOException e) {
+          e.printStackTrace();
+        }
+        return 0;
+      }
+
+      public float floatVal(int doc) {
+        return (float)intVal(doc);
+      }
+
+      public long longVal(int doc) {
+        return (long)intVal(doc);
+      }
+
+      public double doubleVal(int doc) {
+        return (double)intVal(doc);
+      }
+
+      public String strVal(int doc) {
+        return intVal(doc) + "";
+      }
+
+      public String toString(int doc) {
+        return description() + '=' + intVal(doc);
+      }
+    };
+  }
+  
+  public boolean equals(Object o) {
+    if (o.getClass() !=  JoinDocFreqValueSource.class) return false;
+    JoinDocFreqValueSource other = (JoinDocFreqValueSource)o;
+    if( !qfield.equals( other.qfield ) ) return false;
+    return super.equals(other);
+  }
+
+  public int hashCode() {
+    return qfield.hashCode() + super.hashCode();
+  };
+}

Modified: lucene/dev/trunk/solr/src/test/org/apache/solr/search/function/SortByFunctionTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/src/test/org/apache/solr/search/function/SortByFunctionTest.java?rev=988330&r1=988329&r2=988330&view=diff
==============================================================================
--- lucene/dev/trunk/solr/src/test/org/apache/solr/search/function/SortByFunctionTest.java (original)
+++ lucene/dev/trunk/solr/src/test/org/apache/solr/search/function/SortByFunctionTest.java Mon Aug 23 21:56:16 2010
@@ -89,6 +89,37 @@ public class SortByFunctionTest extends 
             "//result/doc[4]/int[@name='id'][.='3']"
     );
   }
+  
+  public void testSortJoinDocFreq() throws Exception
+  {
+    assertU(adoc("id", "4", "id_s", "D", "links_mfacet", "A", "links_mfacet", "B", "links_mfacet", "C" ) );
+    assertU(adoc("id", "3", "id_s", "C", "links_mfacet", "A", "links_mfacet", "B" ) );
+    assertU(adoc("id", "2", "id_s", "B", "links_mfacet", "A" ) );
+    assertU(adoc("id", "1", "id_s", "A"  ) );
+    assertU(commit());
+
+    assertQ(req("q", "links_mfacet:B", "fl", "id", "sort", "id asc"),
+            "//*[@numFound='2']",
+            "//result/doc[1]/int[@name='id'][.='3']",
+            "//result/doc[2]/int[@name='id'][.='4']"
+    );
+    
+    assertQ(req("q", "*:*", "fl", "id", "sort", "joindf(id_s, links_mfacet) desc"),
+            "//*[@numFound='4']",
+            "//result/doc[1]/int[@name='id'][.='1']",
+            "//result/doc[2]/int[@name='id'][.='2']",
+            "//result/doc[3]/int[@name='id'][.='3']",
+            "//result/doc[4]/int[@name='id'][.='4']"
+    );
+
+    assertQ(req("q", "*:*", "fl", "id", "sort", "joindf(id_s, links_mfacet) asc"),
+            "//*[@numFound='4']",
+            "//result/doc[1]/int[@name='id'][.='4']",
+            "//result/doc[2]/int[@name='id'][.='3']",
+            "//result/doc[3]/int[@name='id'][.='2']",
+            "//result/doc[4]/int[@name='id'][.='1']"
+    );
+  }
 }
 
 /*