Apache Lucene open-source search software
SOLR-1694 -- use FieldCache value from one field to get the DocFreq for another field.
Ryan McKinley4c3698858f4c
diff --git a/solr/src/java/org/apache/solr/search/ValueSourceParser.java b/solr/src/java/org/apache/solr/search/ValueSourceParser.javaindex a7f2d145fd7..92509f938df 100755--- a/solr/src/java/org/apache/solr/search/ValueSourceParser.java+++ b/solr/src/java/org/apache/solr/search/ValueSourceParser.java@@ -229,6 +229,14 @@ public abstract class ValueSourceParser implements NamedListInitializedPlugin {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 {diff --git a/solr/src/java/org/apache/solr/search/function/JoinDocFreqValueSource.java b/solr/src/java/org/apache/solr/search/function/JoinDocFreqValueSource.javanew file mode 100644index 00000000000..6f5101249af--- /dev/null+++ b/solr/src/java/org/apache/solr/search/function/JoinDocFreqValueSource.java@@ -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();+ };+}diff --git a/solr/src/test/org/apache/solr/search/function/SortByFunctionTest.java b/solr/src/test/org/apache/solr/search/function/SortByFunctionTest.javaindex 6ec278bac0c..e0fecfa1185 100644--- a/solr/src/test/org/apache/solr/search/function/SortByFunctionTest.java+++ b/solr/src/test/org/apache/solr/search/function/SortByFunctionTest.java@@ -89,6 +89,37 @@ public class SortByFunctionTest extends AbstractSolrTestCase {"//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']"+ );+ }}/*