From 952fdc80fbe044b0ec7720ae4b0f445fc2e8164c Mon Sep 17 00:00:00 2001 From: Mikhail Khludnev Date: Mon, 4 Dec 2023 16:00:31 +0300 Subject: [PATCH 1/2] reproducer.. still in doubts. .. --- .../TestPHPSerializedResponseWriter.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/solr/core/src/test/org/apache/solr/response/TestPHPSerializedResponseWriter.java b/solr/core/src/test/org/apache/solr/response/TestPHPSerializedResponseWriter.java index de6339650a3..d92ac1b6ead 100644 --- a/solr/core/src/test/org/apache/solr/response/TestPHPSerializedResponseWriter.java +++ b/solr/core/src/test/org/apache/solr/response/TestPHPSerializedResponseWriter.java @@ -20,9 +20,12 @@ import java.io.StringWriter; import java.util.Arrays; import java.util.LinkedHashMap; + +import org.apache.lucene.document.*; import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; +import org.apache.solr.common.util.SimpleOrderedMap; import org.apache.solr.request.SolrQueryRequest; import org.junit.BeforeClass; import org.junit.Test; @@ -94,4 +97,28 @@ public void testSolrDocuments() throws IOException { buf.toString()); req.close(); } + + @Test + public void testLuceneDocument() throws IOException { + SolrQueryRequest req = req("q", "*:*"); + SolrQueryResponse rsp = new SolrQueryResponse(); + QueryResponseWriter w = new PHPSerializedResponseWriter(); + StringWriter buf = new StringWriter(); + + Document d = new Document(); + + d.add(new StringField("id", "2", Field.Store.YES)); + d.add(new TextField("name", "foo bar", Field.Store.YES)); + d.add(new IntField("cost", 5, Field.Store.YES)); + + SimpleOrderedMap val = new SimpleOrderedMap<>(); + val.add("lucene doc", d); + rsp.add("map", val); + + w.write(buf, req, rsp); + assertEquals( + "a:1:{s:3:\"map\";a:1:{s:10:\"lucene doc\";a:3:{s:2:\"id\";s:1:\"2\";s:4:\"cost\";s:1:\"5\";s:4:\"name\";a:1:{s:7:\"foo bar\";}}}}", + buf.toString()); + req.close(); + } } From 4bb877ba3bbe68f11656eb257ae31635bb49ebad Mon Sep 17 00:00:00 2001 From: Mikhail Khludnev Date: Mon, 4 Dec 2023 21:19:21 +0300 Subject: [PATCH 2/2] fix wt=phps /admin/luke --- .../response/PHPSerializedResponseWriter.java | 31 +++++++++++++++++++ .../TestPHPSerializedResponseWriter.java | 7 +++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/response/PHPSerializedResponseWriter.java b/solr/core/src/java/org/apache/solr/response/PHPSerializedResponseWriter.java index 0a6f828936f..a8676557e0a 100644 --- a/solr/core/src/java/org/apache/solr/response/PHPSerializedResponseWriter.java +++ b/solr/core/src/java/org/apache/solr/response/PHPSerializedResponseWriter.java @@ -23,12 +23,15 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; + +import org.apache.lucene.document.Document; import org.apache.lucene.util.ArrayUtil; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.UnicodeUtil; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.params.CommonParams; import org.apache.solr.common.util.NamedList; +import org.apache.solr.common.util.SimpleOrderedMap; import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.search.ReturnFields; @@ -84,9 +87,37 @@ public void writeResponse() throws IOException { @Override public void writeNamedList(String name, NamedList val) throws IOException { + /*SimpleOrderedMap copy = new SimpleOrderedMap<>(); + for (Map.Entry tuple : val) { + + Object entryVal = tuple.getValue(); + if (entryVal!=null && (entryVal instanceof SolrDocument || entryVal instanceof Document)) { + entryVal = new Object[]{entryVal}; + } + copy.add(tuple.getKey(), entryVal); + }*/ writeNamedListAsMapMangled(name, val); } + @Override + public void writeVal(String name, Object val) throws IOException { + if (val==null) { + super.writeVal(name, val); + } else { + if (val instanceof Document || val instanceof SolrDocument) { + writeMapOpener(1); // + //writeKey(1, false); // Docs never appears standalone, but always in docList, + // unless it's reponded by /admin/luke where Lucene Document + // appears in SimpleOrderedMap + // commening this line fixes PHPS format + super.writeVal(""+0, val); + writeMapCloser(); + } else { + super.writeVal(name, val); + } + } + } + @Override public void writeStartDocumentList( String name, long start, int size, long numFound, Float maxScore, Boolean numFoundExact) diff --git a/solr/core/src/test/org/apache/solr/response/TestPHPSerializedResponseWriter.java b/solr/core/src/test/org/apache/solr/response/TestPHPSerializedResponseWriter.java index d92ac1b6ead..accaa9a012d 100644 --- a/solr/core/src/test/org/apache/solr/response/TestPHPSerializedResponseWriter.java +++ b/solr/core/src/test/org/apache/solr/response/TestPHPSerializedResponseWriter.java @@ -112,12 +112,13 @@ public void testLuceneDocument() throws IOException { d.add(new IntField("cost", 5, Field.Store.YES)); SimpleOrderedMap val = new SimpleOrderedMap<>(); - val.add("lucene doc", d); + val.add("lucene doc", new Object[]{d}); rsp.add("map", val); w.write(buf, req, rsp); - assertEquals( - "a:1:{s:3:\"map\";a:1:{s:10:\"lucene doc\";a:3:{s:2:\"id\";s:1:\"2\";s:4:\"cost\";s:1:\"5\";s:4:\"name\";a:1:{s:7:\"foo bar\";}}}}", + System.out.println(buf.toString()); + assertEquals("I'm not sure what it is, but it can be handled by php", + "a:1:{s:3:\"map\";a:1:{s:10:\"lucene doc\";a:1:{i:0;a:1:{i:0;a:3:{s:2:\"id\";s:1:\"2\";s:4:\"cost\";s:1:\"5\";s:4:\"name\";a:1:{i:0;s:7:\"foo bar\";}}}}}}", buf.toString()); req.close(); }