Skip to content

Apache Lucene open-source search software

SOLR-2684: fix bug in BinaryResponseWriter introduced by SOLR-1566

Chris M. Hostetter47269b003fdc
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index a14b4393aa3..8967f9a34fe 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -98,7 +98,9 @@ New Features
* SOLR-1566: Transforming documents in the ResponseWriters. This will allow
for more complex results in responses and open the door for function queries
- as results. (ryan with patches from grant, noble, cmale, yonik, Jan Høydahl)
+ as results.
+ (ryan with patches from grant, noble, cmale, yonik, Jan Høydahl,
+ Arul Kalaipandian, hossman)
* SOLR-2396: Add CollationField, which is much more efficient than
the Solr 3.x CollationKeyFilterFactory, and also supports
diff --git a/solr/core/src/java/org/apache/solr/response/BinaryResponseWriter.java b/solr/core/src/java/org/apache/solr/response/BinaryResponseWriter.java
index 62dc8c01268..55db776cfe7 100755
--- a/solr/core/src/java/org/apache/solr/response/BinaryResponseWriter.java
+++ b/solr/core/src/java/org/apache/solr/response/BinaryResponseWriter.java
@@ -89,12 +89,15 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
}
if (o instanceof SolrDocument) {
- // Remove any fields that were not requested
- // This typically happens when distributed search adds extra fields to an internal request
+ // Remove any fields that were not requested.
+ // This typically happens when distributed search adds
+ // extra fields to an internal request
SolrDocument doc = (SolrDocument)o;
- for( String fname : doc.getFieldNames() ) {
- if( !returnFields.wantsField( fname ) ) {
- doc.removeFields( fname );
+ Iterator<Map.Entry<String, Object>> i = doc.iterator();
+ while ( i.hasNext() ) {
+ String fname = i.next().getKey();
+ if ( !returnFields.wantsField( fname ) ) {
+ i.remove();
}
}
return doc;
diff --git a/solr/core/src/test/org/apache/solr/request/TestBinaryResponseWriter.java b/solr/core/src/test/org/apache/solr/request/TestBinaryResponseWriter.java
index 2215501a2ca..b28e55a33a1 100644
--- a/solr/core/src/test/org/apache/solr/request/TestBinaryResponseWriter.java
+++ b/solr/core/src/test/org/apache/solr/request/TestBinaryResponseWriter.java
@@ -22,7 +22,10 @@ import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.JavaBinCodec;
import org.apache.solr.response.BinaryQueryResponseWriter;
+import org.apache.solr.response.BinaryResponseWriter.Resolver;
import org.apache.solr.response.SolrQueryResponse;
+import org.apache.solr.search.ReturnFields;
+
import org.apache.solr.util.AbstractSolrTestCase;
import java.io.ByteArrayInputStream;
@@ -70,4 +73,31 @@ public class TestBinaryResponseWriter extends AbstractSolrTestCase {
req.close();
}
+
+ public void testResolverSolrDocumentPartialFields() throws Exception {
+ LocalSolrQueryRequest req = lrf.makeRequest("q", "*:*",
+ "fl", "id,xxx,ddd_s");
+ SolrDocument in = new SolrDocument();
+ in.addField("id", 345);
+ in.addField("aaa_s", "aaa");
+ in.addField("bbb_s", "bbb");
+ in.addField("ccc_s", "ccc");
+ in.addField("ddd_s", "ddd");
+ in.addField("eee_s", "eee");
+
+ Resolver r = new Resolver(req, new ReturnFields(req));
+ Object o = r.resolve(in, new JavaBinCodec());
+
+ assertNotNull("obj is null", o);
+ assertTrue("obj is not doc", o instanceof SolrDocument);
+
+ SolrDocument out = (SolrDocument) o;
+ assertTrue("id not found", out.getFieldNames().contains("id"));
+ assertTrue("ddd_s not found", out.getFieldNames().contains("ddd_s"));
+ assertEquals("Wrong number of fields found",
+ 2, out.getFieldNames().size());
+
+
+ }
+
}
diff --git a/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java b/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java
index 54624d1fb03..f2f46e6d43a 100644
--- a/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java
+++ b/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java
@@ -50,10 +50,11 @@ public class SolrDocument implements Map<String,Object>, Iterable<Map.Entry<Stri
}
/**
- * @return a list of fields defined in this document
+ * @return a list of field names defined in this document - this Collection is directly backed by this SolrDocument.
+ * @see #keySet
*/
public Collection<String> getFieldNames() {
- return _fields.keySet();
+ return this.keySet();
}
///////////////////////////////////////////////////////////////////
@@ -73,7 +74,7 @@ public class SolrDocument implements Map<String,Object>, Iterable<Map.Entry<Stri
*/
public boolean removeFields(String name)
{
- return _fields.remove( name ) != null;
+ return this.remove( name ) != null;
}
/**