Apache Lucene open-source search software
SOLR-1298: enable returning float functions via fl, fix bug with default name of function
Yonik Seeley22a945609f31
diff --git a/solr/src/java/org/apache/solr/response/transform/ValueSourceAugmenter.java b/solr/src/java/org/apache/solr/response/transform/ValueSourceAugmenter.javaindex f3e8a31bd87..3d1f24b2c82 100644--- a/solr/src/java/org/apache/solr/response/transform/ValueSourceAugmenter.java+++ b/solr/src/java/org/apache/solr/response/transform/ValueSourceAugmenter.java@@ -16,10 +16,19 @@*/package org.apache.solr.response.transform;+import org.apache.lucene.index.IndexReader;+import org.apache.lucene.util.ReaderUtil;import org.apache.solr.common.SolrDocument;+import org.apache.solr.common.SolrException;+import org.apache.solr.core.SolrCore;import org.apache.solr.search.QParser;+import org.apache.solr.search.SolrIndexSearcher;+import org.apache.solr.search.function.DocValues;import org.apache.solr.search.function.ValueSource;+import java.io.IOException;+import java.util.Map;+/*** Add values from a ValueSource (function query etc)*@@ -32,13 +41,15 @@ public class ValueSourceAugmenter extends DocTransformer{public final String name;public final QParser qparser;- public final ValueSource values;+ public final ValueSource valueSource;++- public ValueSourceAugmenter( String name, QParser qparser, ValueSource values )+ public ValueSourceAugmenter( String name, QParser qparser, ValueSource valueSource ){this.name = name;this.qparser = qparser;- this.values = values;+ this.valueSource = valueSource;}@Override@@ -49,14 +60,41 @@ public class ValueSourceAugmenter extends DocTransformer@Overridepublic void setContext( TransformContext context ) {- // maybe we do something here?+ IndexReader reader = qparser.getReq().getSearcher().getIndexReader();+ readerContexts = reader.getTopReaderContext().leaves();+ docValuesArr = new DocValues[readerContexts.length];++ searcher = qparser.getReq().getSearcher();+ this.fcontext = valueSource.newContext(searcher);}++ Map fcontext;+ SolrIndexSearcher searcher;+ IndexReader.AtomicReaderContext[] readerContexts;+ DocValues docValuesArr[];++@Overridepublic void transform(SolrDocument doc, int docid) {- // TODO, should know what the real type is -- not always string- // how do we get to docvalues?- Object v = "now what..."; //values.g.strVal( docid );- doc.setField( name, v );+ // This is only good for random-access functions++ try {++ // TODO: calculate this stuff just once across diff functions+ int idx = ReaderUtil.subIndex(docid, readerContexts);+ IndexReader.AtomicReaderContext rcontext = readerContexts[idx];+ DocValues values = docValuesArr[idx];+ if (values == null) {+ docValuesArr[idx] = values = valueSource.getValues(fcontext, rcontext);+ }++ int localId = docid - rcontext.docBase;+ float val = values.floatVal(localId); // TODO: handle all types++ doc.setField( name, val );+ } catch (IOException e) {+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "exception at docid " + docid + " for valuesource " + valueSource, e, false);+ }}}diff --git a/solr/src/java/org/apache/solr/search/ReturnFields.java b/solr/src/java/org/apache/solr/search/ReturnFields.javaindex d50b417754c..dffcccd8d65 100644--- a/solr/src/java/org/apache/solr/search/ReturnFields.java+++ b/solr/src/java/org/apache/solr/search/ReturnFields.java@@ -27,6 +27,7 @@ import org.apache.lucene.queryParser.ParseException;import org.apache.lucene.search.Query;import org.apache.solr.common.SolrException;import org.apache.solr.common.params.CommonParams;+import org.apache.solr.common.params.SolrParams;import org.apache.solr.common.util.NamedList;import org.apache.solr.core.SolrCore;import org.apache.solr.request.SolrQueryRequest;@@ -194,7 +195,7 @@ public class ReturnFieldsif (field == null) {// We didn't find a simple name, so let's see if it's a globbed field name.- // Globbing only works with recommended field names.+ // Globbing only works with field names of the recommended form (roughly like java identifiers)field = sp.getGlobbedId(null);ch = sp.ch();@@ -258,6 +259,18 @@ public class ReturnFieldsvs = new QueryValueSource(q, 0.0f);}+ if (key==null) {+ SolrParams localParams = parser.getLocalParams();+ if (localParams != null) {+ key = localParams.get("key");+ }+ if (key == null) {+ // use the function name itself as the field name+ key = sp.val.substring(start, sp.pos);+ }+ }++if (key==null) {key = funcStr;}@@ -292,7 +305,7 @@ public class ReturnFieldsprivate void addField( String field, String key, DocTransformers augmenters, SolrQueryRequest req ){String disp = (key==null) ? field : key;- fields.add( field ); // need to put in the map to maintain order for things like CSVResponseWriter+ fields.add(field); // need to put in the map to maintain order for things like CSVResponseWriterokFieldNames.add( field );okFieldNames.add( key );// a valid field name