Search in sources :

Example 1 with NoOpResponseParser

use of org.apache.solr.client.solrj.impl.NoOpResponseParser in project lucene-solr by apache.

the class SolrExampleTests method testRawFields.

@Test
public void testRawFields() throws Exception {
    String rawJson = "{ \"raw\": 1.234, \"id\":\"111\" }";
    String rawXml = "<hello>this is <some/><xml/></hello>";
    SolrClient client = getSolrClient();
    // Empty the database...
    // delete everything!
    client.deleteByQuery("*:*");
    // Now add something...
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", "111");
    doc.addField("name", "doc1");
    doc.addField("json_s", rawJson);
    doc.addField("xml_s", rawXml);
    client.add(doc);
    // make sure this gets in first
    client.commit();
    SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    query.set(CommonParams.FL, "id,json_s:[json],xml_s:[xml]");
    QueryRequest req = new QueryRequest(query);
    req.setResponseParser(new BinaryResponseParser());
    QueryResponse rsp = req.process(client);
    SolrDocumentList out = rsp.getResults();
    assertEquals(1, out.getNumFound());
    SolrDocument out1 = out.get(0);
    assertEquals("111", out1.getFieldValue("id"));
    // Check that the 'raw' fields are unchanged using the standard formats
    assertEquals(rawJson, out1.get("json_s"));
    assertEquals(rawXml, out1.get("xml_s"));
    if (client instanceof EmbeddedSolrServer) {
        // the EmbeddedSolrServer ignores the configured parser
        return;
    }
    // Check raw JSON Output
    query.set("fl", "id,json_s:[json],xml_s:[xml]");
    query.set(CommonParams.WT, "json");
    req = new QueryRequest(query);
    req.setResponseParser(new NoOpResponseParser("json"));
    NamedList<Object> resp = client.request(req);
    String raw = (String) resp.get("response");
    // Check that the response parses as JSON
    JSONParser parser = new JSONParser(raw);
    int evt = parser.nextEvent();
    while (evt != JSONParser.EOF) {
        evt = parser.nextEvent();
    }
    // no escaping
    assertTrue(raw.indexOf(rawJson) > 0);
    // quoted xml
    assertTrue(raw.indexOf('"' + rawXml + '"') > 0);
    // Check raw XML Output
    req.setResponseParser(new NoOpResponseParser("xml"));
    query.set("fl", "id,json_s:[json],xml_s:[xml]");
    query.set(CommonParams.WT, "xml");
    req = new QueryRequest(query);
    req.setResponseParser(new NoOpResponseParser("xml"));
    resp = client.request(req);
    raw = (String) resp.get("response");
    // Check that we get raw xml and json is escaped
    // escaped
    assertTrue(raw.indexOf('>' + rawJson + '<') > 0);
    // raw xml
    assertTrue(raw.indexOf(rawXml) > 0);
}
Also used : NoOpResponseParser(org.apache.solr.client.solrj.impl.NoOpResponseParser) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) StringContains.containsString(org.junit.internal.matchers.StringContains.containsString) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) ErrorTrackingConcurrentUpdateSolrClient(org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) JSONParser(org.noggit.JSONParser) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) Test(org.junit.Test)

Example 2 with NoOpResponseParser

use of org.apache.solr.client.solrj.impl.NoOpResponseParser in project lucene-solr by apache.

the class NoOpResponseParserTest method testInputStreamResponse.

/**
   * Parse response from java.io.InputStream.
   */
@Test
public void testInputStreamResponse() throws Exception {
    NoOpResponseParser parser = new NoOpResponseParser();
    try (final InputStream is = getResponse()) {
        assertNotNull(is);
        NamedList<Object> response = parser.processResponse(is, "UTF-8");
        assertNotNull(response.get("response"));
        String expectedResponse = IOUtils.toString(getResponse(), "UTF-8");
        assertEquals(expectedResponse, response.get("response"));
    }
}
Also used : NoOpResponseParser(org.apache.solr.client.solrj.impl.NoOpResponseParser) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 3 with NoOpResponseParser

use of org.apache.solr.client.solrj.impl.NoOpResponseParser in project lucene-solr by apache.

the class NoOpResponseParserTest method testReaderResponse.

/**
   * Parse response from java.io.Reader.
   */
@Test
public void testReaderResponse() throws Exception {
    NoOpResponseParser parser = new NoOpResponseParser();
    try (final InputStream is = getResponse()) {
        assertNotNull(is);
        Reader in = new InputStreamReader(is, StandardCharsets.UTF_8);
        NamedList<Object> response = parser.processResponse(in);
        assertNotNull(response.get("response"));
        String expectedResponse = IOUtils.toString(getResponse(), "UTF-8");
        assertEquals(expectedResponse, response.get("response"));
    }
}
Also used : NoOpResponseParser(org.apache.solr.client.solrj.impl.NoOpResponseParser) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Test(org.junit.Test)

Example 4 with NoOpResponseParser

use of org.apache.solr.client.solrj.impl.NoOpResponseParser in project lucene-solr by apache.

the class NoOpResponseParserTest method testQueryParse.

/**
   * Parse response from query using NoOpResponseParser.
   */
@Test
public void testQueryParse() throws Exception {
    try (HttpSolrClient client = (HttpSolrClient) createNewSolrClient()) {
        SolrQuery query = new SolrQuery("id:1234");
        QueryRequest req = new QueryRequest(query);
        client.setParser(new NoOpResponseParser());
        NamedList<Object> resp = client.request(req);
        String responseString = (String) resp.get("response");
        assertResponse(responseString);
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) NoOpResponseParser(org.apache.solr.client.solrj.impl.NoOpResponseParser) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 5 with NoOpResponseParser

use of org.apache.solr.client.solrj.impl.NoOpResponseParser in project lucene-solr by apache.

the class SolrTestCaseHS method getQueryResponse.

public static String getQueryResponse(SolrClient client, String wt, SolrParams params) throws Exception {
    if (client == null) {
        return getQueryResponse(wt, params);
    }
    ModifiableSolrParams p = new ModifiableSolrParams(params);
    p.set("wt", wt);
    String path = p.get("qt");
    p.remove("qt");
    p.set("indent", "true");
    QueryRequest query = new QueryRequest(p);
    if (path != null) {
        query.setPath(path);
    }
    query.setResponseParser(new NoOpResponseParser(wt));
    NamedList<Object> rsp = client.request(query);
    String raw = (String) rsp.get("response");
    return raw;
}
Also used : NoOpResponseParser(org.apache.solr.client.solrj.impl.NoOpResponseParser) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Aggregations

NoOpResponseParser (org.apache.solr.client.solrj.impl.NoOpResponseParser)5 Test (org.junit.Test)4 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)3 InputStream (java.io.InputStream)2 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)2 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 EmbeddedSolrServer (org.apache.solr.client.solrj.embedded.EmbeddedSolrServer)1 ErrorTrackingConcurrentUpdateSolrClient (org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient)1 BinaryResponseParser (org.apache.solr.client.solrj.impl.BinaryResponseParser)1 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)1 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)1 StringContains.containsString (org.junit.internal.matchers.StringContains.containsString)1 JSONParser (org.noggit.JSONParser)1