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);
}
}
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);
}
}
}
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);
}
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;
}
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;
}
}
Aggregations