Apache Lucene open-source search software
SOLR-1124: add top() function, fix ord/rord
Yonik Seeleyf1be0a4f2013
diff --git a/src/java/org/apache/solr/search/ValueSourceParser.java b/src/java/org/apache/solr/search/ValueSourceParser.javaindex 2d1aff40bee..05bbdc5b4d7 100755--- a/src/java/org/apache/solr/search/ValueSourceParser.java+++ b/src/java/org/apache/solr/search/ValueSourceParser.java@@ -23,22 +23,7 @@ import java.util.Map;import org.apache.lucene.queryParser.ParseException;import org.apache.lucene.search.Query;import org.apache.solr.common.util.NamedList;-import org.apache.solr.search.function.BoostedQuery;-import org.apache.solr.search.function.DivFloatFunction;-import org.apache.solr.search.function.DocValues;-import org.apache.solr.search.function.LinearFloatFunction;-import org.apache.solr.search.function.MaxFloatFunction;-import org.apache.solr.search.function.OrdFieldSource;-import org.apache.solr.search.function.PowFloatFunction;-import org.apache.solr.search.function.ProductFloatFunction;-import org.apache.solr.search.function.QueryValueSource;-import org.apache.solr.search.function.RangeMapFloatFunction;-import org.apache.solr.search.function.ReciprocalFloatFunction;-import org.apache.solr.search.function.ReverseOrdFieldSource;-import org.apache.solr.search.function.ScaleFloatFunction;-import org.apache.solr.search.function.SimpleFloatFunction;-import org.apache.solr.search.function.SumFloatFunction;-import org.apache.solr.search.function.ValueSource;+import org.apache.solr.search.function.*;import org.apache.solr.util.plugin.NamedListInitializedPlugin;/**@@ -67,7 +52,7 @@ public abstract class ValueSourceParser implements NamedListInitializedPluginstandardValueSourceParsers.put("ord", new ValueSourceParser() {public ValueSource parse(FunctionQParser fp) throws ParseException {String field = fp.parseId();- return new OrdFieldSource(field);+ return new TopValueSource(new OrdFieldSource(field));}public void init(NamedList args) {@@ -77,13 +62,23 @@ public abstract class ValueSourceParser implements NamedListInitializedPluginstandardValueSourceParsers.put("rord", new ValueSourceParser() {public ValueSource parse(FunctionQParser fp) throws ParseException {String field = fp.parseId();- return new ReverseOrdFieldSource(field);+ return new TopValueSource(new ReverseOrdFieldSource(field));}public void init(NamedList args) {}});+ standardValueSourceParsers.put("top", new ValueSourceParser() {+ public ValueSource parse(FunctionQParser fp) throws ParseException {+ ValueSource source = fp.parseValueSource();+ // nested top is redundant, and ord and rord get automatically wrapped+ if (source instanceof TopValueSource) return source;+ return new TopValueSource(source);+ }+ public void init(NamedList args) {+ }+ });standardValueSourceParsers.put("linear", new ValueSourceParser() {public ValueSource parse(FunctionQParser fp) throws ParseException {ValueSource source = fp.parseValueSource();diff --git a/src/java/org/apache/solr/search/function/TopValueSource.java b/src/java/org/apache/solr/search/function/TopValueSource.javanew file mode 100755index 00000000000..35aba07d854--- /dev/null+++ b/src/java/org/apache/solr/search/function/TopValueSource.java@@ -0,0 +1,84 @@+package org.apache.solr.search.function;++import org.apache.solr.search.SolrIndexReader;+import org.apache.lucene.index.IndexReader;++import java.io.IOException;++/**+ * A value source that wraps another and ensures that the top level reader+ * is used. This is useful for value sources like ord() who's value depend+ * on all those around it.+ */+public class TopValueSource extends ValueSource {+ private final ValueSource vs;++ public TopValueSource(ValueSource vs) {+ this.vs = vs;+ }++ public ValueSource getValueSource() {+ return vs;+ }++ public String description() {+ return "top(" + vs.description() + ')';+ }++ public DocValues getValues(IndexReader reader) throws IOException {+ int offset = 0;+ IndexReader topReader = reader;+ if (topReader instanceof SolrIndexReader) {+ SolrIndexReader r = (SolrIndexReader)topReader;+ while (r.getParent() != null) {+ offset += r.getBase();+ r = r.getParent();+ }+ topReader = r;+ }+ final int off = offset;+ final DocValues vals = vs.getValues(topReader);+ if (topReader == reader) return vals;++ return new DocValues() {+ public float floatVal(int doc) {+ return vals.floatVal(doc + off);+ }++ public int intVal(int doc) {+ return vals.intVal(doc + off);+ }++ public long longVal(int doc) {+ return vals.longVal(doc + off);+ }++ public double doubleVal(int doc) {+ return vals.doubleVal(doc + off);+ }++ public String strVal(int doc) {+ return vals.strVal(doc + off);+ }++ public String toString(int doc) {+ return vals.strVal(doc + off);+ }+ };+ }++ public boolean equals(Object o) {+ if (o.getClass() != TopValueSource.class) return false;+ TopValueSource other = (TopValueSource)o;+ return vs.equals(other.vs);+ }++ public int hashCode() {+ int h = vs.hashCode();+ return (h<<1) | (h>>>31);+ }++ public String toString() {+ return "top("+vs.toString()+')';+ }+}\ No newline at end of filediff --git a/src/test/org/apache/solr/search/function/TestFunctionQuery.java b/src/test/org/apache/solr/search/function/TestFunctionQuery.javaindex bcfc73f188b..f52da3b0277 100755--- a/src/test/org/apache/solr/search/function/TestFunctionQuery.java+++ b/src/test/org/apache/solr/search/function/TestFunctionQuery.java@@ -273,6 +273,24 @@ public class TestFunctionQuery extends AbstractSolrTestCase {singleTest(field, "\0", answers);System.out.println("Done test "+i);}+ }++ public void testGeneral() {+ assertU(adoc("id","1"));+ assertU(adoc("id","2"));+ assertU(commit()); // create more than one segment+ assertU(adoc("id","3"));+ assertU(adoc("id","4"));+ assertU(commit()); // create more than one segment+ assertU(adoc("id","5"));+ assertU(adoc("id","6"));+ assertU(commit());+ // test that ord and rord are working on a global index basis, not just+ // at the segment level (since Lucene 2.9 has switched to per-segment searching)+ assertQ(req("fl","*,score","q", "{!func}ord(id)", "fq","id:6"), "//float[@name='score']='6.0'");+ assertQ(req("fl","*,score","q", "{!func}top(ord(id))", "fq","id:6"), "//float[@name='score']='6.0'");+ assertQ(req("fl","*,score","q", "{!func}rord(id)", "fq","id:1"),"//float[@name='score']='6.0'");+ assertQ(req("fl","*,score","q", "{!func}top(rord(id))", "fq","id:1"),"//float[@name='score']='6.0'");}}\ No newline at end of filediff --git a/src/test/test-files/solr/conf/solrconfig.xml b/src/test/test-files/solr/conf/solrconfig.xmlindex f56d7fdeb4c..6edc0d73f22 100644--- a/src/test/test-files/solr/conf/solrconfig.xml+++ b/src/test/test-files/solr/conf/solrconfig.xml@@ -89,9 +89,13 @@<!-- lucene options specific to the main on-disk lucene index --><useCompoundFile>false</useCompoundFile><mergeFactor>10</mergeFactor>- <ramBufferSizeMB>32</ramBufferSizeMB>+ <!-- for better multi-segment testing, we are using slower+ indexing properties of maxBufferedDocs=10 and LogDocMergePolicy.+ -->+ <maxBufferedDocs>10</maxBufferedDocs><maxMergeDocs>2147483647</maxMergeDocs><maxFieldLength>10000</maxFieldLength>+ <mergePolicy>org.apache.lucene.index.LogDocMergePolicy</mergePolicy><unlockOnStartup>true</unlockOnStartup></mainIndex>