Search in sources :

Example 1 with BinaryResponseParser

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

the class SolrExampleBinaryTest method createNewSolrClient.

@Override
public SolrClient createNewSolrClient() {
    try {
        // setup the server...
        String url = jetty.getBaseUrl().toString() + "/collection1";
        HttpSolrClient client = getHttpSolrClient(url);
        client.setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
        client.setUseMultiPartPost(random().nextBoolean());
        // where the magic happens
        client.setParser(new BinaryResponseParser());
        client.setRequestWriter(new BinaryRequestWriter());
        return client;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) BinaryRequestWriter(org.apache.solr.client.solrj.impl.BinaryRequestWriter)

Example 2 with BinaryResponseParser

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

the class SolrExampleTests method testUnicode.

public void testUnicode() throws Exception {
    Random random = random();
    int numIterations = atLeast(3);
    SolrClient client = getSolrClient();
    // save the old parser, so we can set it back.
    ResponseParser oldParser = null;
    if (client instanceof HttpSolrClient) {
        HttpSolrClient httpSolrClient = (HttpSolrClient) client;
        oldParser = httpSolrClient.getParser();
    }
    try {
        for (int iteration = 0; iteration < numIterations; iteration++) {
            // choose format
            if (client instanceof HttpSolrClient) {
                if (random.nextBoolean()) {
                    ((HttpSolrClient) client).setParser(new BinaryResponseParser());
                } else {
                    ((HttpSolrClient) client).setParser(new XMLResponseParser());
                }
            }
            int numDocs = TestUtil.nextInt(random(), 1, 10 * RANDOM_MULTIPLIER);
            // Empty the database...
            // delete everything!
            client.deleteByQuery("*:*");
            List<SolrInputDocument> docs = new ArrayList<>();
            for (int i = 0; i < numDocs; i++) {
                // Now add something...
                SolrInputDocument doc = new SolrInputDocument();
                doc.addField("id", "" + i);
                doc.addField("unicode_s", randomTestString(30));
                docs.add(doc);
            }
            client.add(docs);
            client.commit();
            SolrQuery query = new SolrQuery();
            query.setQuery("*:*");
            query.setRows(numDocs);
            QueryResponse rsp = client.query(query);
            for (int i = 0; i < numDocs; i++) {
                String expected = (String) docs.get(i).getFieldValue("unicode_s");
                String actual = (String) rsp.getResults().get(i).getFieldValue("unicode_s");
                assertEquals(expected, actual);
            }
        }
    } finally {
        if (oldParser != null) {
            // set the old parser back
            ((HttpSolrClient) client).setParser(oldParser);
        }
    }
}
Also used : NoOpResponseParser(org.apache.solr.client.solrj.impl.NoOpResponseParser) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser) ArrayList(java.util.ArrayList) StringContains.containsString(org.junit.internal.matchers.StringContains.containsString) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrInputDocument(org.apache.solr.common.SolrInputDocument) Random(java.util.Random) 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) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser)

Example 3 with BinaryResponseParser

use of org.apache.solr.client.solrj.impl.BinaryResponseParser 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 4 with BinaryResponseParser

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

the class SolrExampleStreamingBinaryTest method createNewSolrClient.

@Override
public SolrClient createNewSolrClient() {
    ConcurrentUpdateSolrClient client = (ConcurrentUpdateSolrClient) super.createNewSolrClient();
    client.setParser(new BinaryResponseParser());
    client.setRequestWriter(new BinaryRequestWriter());
    return client;
}
Also used : BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) BinaryRequestWriter(org.apache.solr.client.solrj.impl.BinaryRequestWriter) ConcurrentUpdateSolrClient(org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient)

Example 5 with BinaryResponseParser

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

the class ServerSnitchContext method invoke.

public SimpleSolrResponse invoke(UpdateShardHandler shardHandler, final String url, String path, SolrParams params) throws IOException, SolrServerException {
    GenericSolrRequest request = new GenericSolrRequest(SolrRequest.METHOD.GET, path, params);
    try (HttpSolrClient client = new HttpSolrClient.Builder(url).withHttpClient(shardHandler.getHttpClient()).withResponseParser(new BinaryResponseParser()).build()) {
        NamedList<Object> rsp = client.request(request);
        request.response.nl = rsp;
        return request.response;
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) GenericSolrRequest(org.apache.solr.client.solrj.request.GenericSolrRequest) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser)

Aggregations

BinaryResponseParser (org.apache.solr.client.solrj.impl.BinaryResponseParser)12 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)7 BinaryRequestWriter (org.apache.solr.client.solrj.impl.BinaryRequestWriter)4 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)4 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ErrorTrackingConcurrentUpdateSolrClient (org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient)3 XMLResponseParser (org.apache.solr.client.solrj.impl.XMLResponseParser)3 SolrDocument (org.apache.solr.common.SolrDocument)3 SolrInputDocument (org.apache.solr.common.SolrInputDocument)3 ConcurrentUpdateSolrClient (org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient)2 NoOpResponseParser (org.apache.solr.client.solrj.impl.NoOpResponseParser)2 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)2 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)2 StringContains.containsString (org.junit.internal.matchers.StringContains.containsString)2 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1