You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by yo...@apache.org on 2011/04/01 23:25:14 UTC

svn commit: r1087902 - in /lucene/dev/trunk/solr/src/java/org/apache/solr: response/transform/ValueSourceAugmenter.java search/ReturnFields.java

Author: yonik
Date: Fri Apr  1 21:25:14 2011
New Revision: 1087902

URL: http://svn.apache.org/viewvc?rev=1087902&view=rev
Log:
SOLR-1298: enable returning float functions via fl, fix bug with default name of function

Modified:
    lucene/dev/trunk/solr/src/java/org/apache/solr/response/transform/ValueSourceAugmenter.java
    lucene/dev/trunk/solr/src/java/org/apache/solr/search/ReturnFields.java

Modified: lucene/dev/trunk/solr/src/java/org/apache/solr/response/transform/ValueSourceAugmenter.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/src/java/org/apache/solr/response/transform/ValueSourceAugmenter.java?rev=1087902&r1=1087901&r2=1087902&view=diff
==============================================================================
--- lucene/dev/trunk/solr/src/java/org/apache/solr/response/transform/ValueSourceAugmenter.java (original)
+++ lucene/dev/trunk/solr/src/java/org/apache/solr/response/transform/ValueSourceAugmenter.java Fri Apr  1 21:25:14 2011
@@ -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 extend
 {
   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 extend
 
   @Override
   public 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[];
+
+
   @Override
   public 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);
+    }
   }
 }

Modified: lucene/dev/trunk/solr/src/java/org/apache/solr/search/ReturnFields.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/src/java/org/apache/solr/search/ReturnFields.java?rev=1087902&r1=1087901&r2=1087902&view=diff
==============================================================================
--- lucene/dev/trunk/solr/src/java/org/apache/solr/search/ReturnFields.java (original)
+++ lucene/dev/trunk/solr/src/java/org/apache/solr/search/ReturnFields.java Fri Apr  1 21:25:14 2011
@@ -27,6 +27,7 @@ import org.apache.lucene.queryParser.Par
 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 ReturnFields
 
         if (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();
@@ -259,6 +260,18 @@ public class ReturnFields
           }
 
           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;
           }
           okFieldNames.add( key );
@@ -292,7 +305,7 @@ public class ReturnFields
   private 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 CSVResponseWriter
     okFieldNames.add( field );
     okFieldNames.add( key );
     // a valid field name