Search in sources :

Example 1 with PHPSerializedResponseWriter

use of org.apache.solr.response.PHPSerializedResponseWriter in project lucene-solr by apache.

the class TestPHPSerializedResponseWriter method testSolrDocuments.

@Test
public void testSolrDocuments() throws IOException {
    SolrQueryRequest req = req("q", "*:*");
    SolrQueryResponse rsp = new SolrQueryResponse();
    QueryResponseWriter w = new PHPSerializedResponseWriter();
    StringWriter buf = new StringWriter();
    SolrDocument d = new SolrDocument();
    SolrDocument d1 = d;
    d.addField("id", "1");
    d.addField("data1", "hello");
    d.addField("data2", 42);
    d.addField("data3", true);
    // multivalued fields: 
    // extremely odd edge case: value is a map
    // we use LinkedHashMap because we are doing a string comparison 
    // later and we need predictible ordering
    LinkedHashMap<String, String> nl = new LinkedHashMap<>();
    nl.put("data4.1", "hashmap");
    nl.put("data4.2", "hello");
    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.addResponse(sdl);
    w.write(buf, req, rsp);
    assertEquals("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:\"data2\";i:42;s:5:\"data3\";b:1;s:5:\"data4\";a:2:{s:7:\"data4.1\";s:7:\"hashmap\";s:7:\"data4.2\";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\";}}i:1;a:1:{s:2:\"id\";s:1:\"2\";}}}}", buf.toString());
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrDocument(org.apache.solr.common.SolrDocument) StringWriter(java.io.StringWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) PHPSerializedResponseWriter(org.apache.solr.response.PHPSerializedResponseWriter) SolrDocumentList(org.apache.solr.common.SolrDocumentList) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 2 with PHPSerializedResponseWriter

use of org.apache.solr.response.PHPSerializedResponseWriter in project lucene-solr by apache.

the class TestPHPSerializedResponseWriter method testSimple.

@Test
public void testSimple() throws IOException {
    SolrQueryRequest req = req("dummy");
    SolrQueryResponse rsp = new SolrQueryResponse();
    QueryResponseWriter w = new PHPSerializedResponseWriter();
    StringWriter buf = new StringWriter();
    rsp.add("data1", "hello");
    rsp.add("data2", 42);
    rsp.add("data3", true);
    w.write(buf, req, rsp);
    assertEquals("a:3:{s:5:\"data1\";s:5:\"hello\";s:5:\"data2\";i:42;s:5:\"data3\";b:1;}", buf.toString());
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) StringWriter(java.io.StringWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) PHPSerializedResponseWriter(org.apache.solr.response.PHPSerializedResponseWriter) Test(org.junit.Test)

Aggregations

StringWriter (java.io.StringWriter)2 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)2 PHPSerializedResponseWriter (org.apache.solr.response.PHPSerializedResponseWriter)2 QueryResponseWriter (org.apache.solr.response.QueryResponseWriter)2 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)2 Test (org.junit.Test)2 LinkedHashMap (java.util.LinkedHashMap)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1