diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index c106675e2c3..2d83e75819c 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -152,6 +152,11 @@ Bug Fixes * SOLR-2275: fix DisMax 'mm' parsing to be tolerant of whitespace (Erick Erickson via hossman) +* SOLR-2307: fix bug in PHPSerializedResponseWriter (wt=phps) when + dealing with SolrDocumentList objects -- ie: sharded queries. + (Antonio Verni via hossman) + + Other Changes ---------------------- diff --git a/solr/src/java/org/apache/solr/response/PHPSerializedResponseWriter.java b/solr/src/java/org/apache/solr/response/PHPSerializedResponseWriter.java index 4da3af5ad3e..286a9b1ae42 100755 --- a/solr/src/java/org/apache/solr/response/PHPSerializedResponseWriter.java +++ b/solr/src/java/org/apache/solr/response/PHPSerializedResponseWriter.java @@ -32,7 +32,8 @@ import org.apache.solr.schema.SchemaField; import org.apache.solr.search.DocIterator; import org.apache.solr.search.DocList; import org.apache.solr.search.SolrIndexSearcher; - +import org.apache.solr.common.SolrDocument; +import org.apache.solr.common.SolrDocumentList; /** * A description of the PHP serialization format can be found here: * http://www.hurring.com/scott/code/perl/serialize/ @@ -200,6 +201,96 @@ class PHPSerializedWriter extends JSONWriter { writeMapCloser(); } + @Override + public void writeSolrDocument(String name, SolrDocument doc, Set returnFields, Map pseudoFields) throws IOException { + HashMap single = new HashMap(); + HashMap multi = new HashMap(); + int pseudoSize = pseudoFields != null ? pseudoFields.size() : 0; + + for (String fname : doc.getFieldNames()) { + if(returnFields != null && !returnFields.contains(fname)){ + continue; + } + + Object val = doc.getFieldValue(fname); + SchemaField sf = schema.getFieldOrNull(fname); + if (sf != null && sf.multiValued()) { + multi.put(fname, val); + }else{ + single.put(fname, val); + } + } + + writeMapOpener(single.size() + multi.size() + pseudoSize); + for(String fname: single.keySet()){ + Object val = single.get(fname); + writeKey(fname, true); + writeVal(fname, val); + } + + for(String fname: multi.keySet()){ + writeKey(fname, true); + + Object val = multi.get(fname); + if (!(val instanceof Collection)) { + // should never be reached if multivalued fields are stored as a Collection + // so I'm assuming a size of 1 just to wrap the single value + writeArrayOpener(1); + writeVal(fname, val); + writeArrayCloser(); + }else{ + writeVal(fname, val); + } + } + + if (pseudoSize > 0) { + writeMap(null,pseudoFields,true, false); + } + writeMapCloser(); + } + + + @Override + public void writeSolrDocumentList(String name, SolrDocumentList docs, Set fields, Map otherFields) throws IOException { + boolean includeScore=false; + if (fields!=null) { + includeScore = fields.contains("score"); + if (fields.size()==0 || (fields.size()==1 && includeScore) || fields.contains("*")) { + fields=null; // null means return all stored fields + } + } + + int sz = docs.size(); + + writeMapOpener(includeScore ? 4 : 3); + + writeKey("numFound",false); + writeLong(null,docs.getNumFound()); + + writeKey("start",false); + writeLong(null,docs.getStart()); + + if (includeScore && docs.getMaxScore() != null) { + writeKey("maxScore",false); + writeFloat(null,docs.getMaxScore()); + } + + writeKey("docs",false); + + writeArrayOpener(sz); + for (int i=0; i nl = new HashMap(); + nl.put("data4.1", "hello"); + nl.put("data4.2", "hashmap"); + d.addField("data4",nl); + // array value + d.addField("data5",Arrays.asList("data5.1", "data5.2", "data5.3")); + + // adding one more document to test array indexes + d = new SolrDocument(); + SolrDocument d2 = d; + d.addField("id","2"); + + SolrDocumentList sdl = new SolrDocumentList(); + sdl.add(d1); + sdl.add(d2); + rsp.add("response", sdl); + + w.write(buf, req, rsp); + assertEquals(buf.toString(), "a:1:{s:8:\"response\";a:3:{s:8:\"numFound\";i:0;s:5:\"start\";i:0;s:4:\"docs\";a:2:{i:0;a:6:{s:2:\"id\";s:1:\"1\";s:5:\"data1\";s:5:\"hello\";s:5:\"data4\";a:2:{s:7:\"data4.2\";s:7:\"hashmap\";s:7:\"data4.1\";s:5:\"hello\";}s:5:\"data5\";a:3:{i:0;s:7:\"data5.1\";i:1;s:7:\"data5.2\";i:2;s:7:\"data5.3\";}s:5:\"data2\";i:42;s:5:\"data3\";b:1;}i:1;a:1:{s:2:\"id\";s:1:\"2\";}}}}"); + req.close(); + } + +}