use of org.apache.solr.client.solrj.impl.XMLResponseParser in project lucene-solr by apache.
the class SolrEntityProcessor method firstInit.
@Override
protected void firstInit(Context context) {
super.firstInit(context);
try {
String serverPath = context.getResolvedEntityAttribute(SOLR_SERVER);
if (serverPath == null) {
throw new DataImportHandlerException(DataImportHandlerException.SEVERE, "SolrEntityProcessor: parameter 'url' is required");
}
HttpClient client = getHttpClient();
URL url = new URL(serverPath);
// (wt="javabin|xml") default is javabin
if ("xml".equals(context.getResolvedEntityAttribute(CommonParams.WT))) {
// TODO: it doesn't matter for this impl when passing a client currently, but we should close this!
solrClient = new Builder(url.toExternalForm()).withHttpClient(client).withResponseParser(new XMLResponseParser()).build();
LOG.info("using XMLResponseParser");
} else {
// TODO: it doesn't matter for this impl when passing a client currently, but we should close this!
solrClient = new Builder(url.toExternalForm()).withHttpClient(client).build();
LOG.info("using BinaryResponseParser");
}
} catch (MalformedURLException e) {
throw new DataImportHandlerException(DataImportHandlerException.SEVERE, e);
}
}
use of org.apache.solr.client.solrj.impl.XMLResponseParser in project lucene-solr by apache.
the class TestDocumentObjectBinder method testSimple.
public void testSimple() throws Exception {
DocumentObjectBinder binder = new DocumentObjectBinder();
XMLResponseParser parser = new XMLResponseParser();
NamedList<Object> nl = parser.processResponse(new StringReader(xml));
QueryResponse res = new QueryResponse(nl, null);
SolrDocumentList solDocList = res.getResults();
List<Item> l = binder.getBeans(Item.class, res.getResults());
assertEquals(solDocList.size(), l.size());
assertEquals(solDocList.get(0).getFieldValue("features"), l.get(0).features);
Item item = new Item();
item.id = "aaa";
item.categories = new String[] { "aaa", "bbb", "ccc" };
SolrInputDocument out = binder.toSolrInputDocument(item);
assertEquals(item.id, out.getFieldValue("id"));
SolrInputField catfield = out.getField("cat");
assertEquals(3, catfield.getValueCount());
List<String> catValues = (List<String>) catfield.getValue();
assertEquals("aaa", catValues.get(0));
assertEquals("bbb", catValues.get(1));
assertEquals("ccc", catValues.get(2));
}
use of org.apache.solr.client.solrj.impl.XMLResponseParser in project lucene-solr by apache.
the class PropertiesRequestHandlerTest method readProperties.
private NamedList<NamedList<NamedList<Object>>> readProperties() throws Exception {
String xml = h.query(req(CommonParams.QT, "/admin/properties", CommonParams.WT, "xml"));
XMLResponseParser parser = new XMLResponseParser();
return (NamedList<NamedList<NamedList<Object>>>) parser.processResponse(new StringReader(xml)).get("system.properties");
}
use of org.apache.solr.client.solrj.impl.XMLResponseParser 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.XMLResponseParser in project lucene-solr by apache.
the class QueryResponseTest method testSimpleGroupResponse.
@Test
public void testSimpleGroupResponse() throws Exception {
XMLResponseParser parser = new XMLResponseParser();
InputStream is = new SolrResourceLoader().openResource("solrj/sampleSimpleGroupResponse.xml");
assertNotNull(is);
Reader in = new InputStreamReader(is, StandardCharsets.UTF_8);
NamedList<Object> response = parser.processResponse(in);
in.close();
QueryResponse qr = new QueryResponse(response, null);
assertNotNull(qr);
GroupResponse groupResponse = qr.getGroupResponse();
assertNotNull(groupResponse);
List<GroupCommand> commands = groupResponse.getValues();
assertNotNull(commands);
assertEquals(1, commands.size());
GroupCommand fieldCommand = commands.get(0);
assertEquals("acco_id", fieldCommand.getName());
assertEquals(30000000, fieldCommand.getMatches());
assertEquals(5687, fieldCommand.getNGroups().intValue());
List<Group> fieldCommandGroups = fieldCommand.getValues();
assertEquals(1, fieldCommandGroups.size());
assertEquals("acco_id", fieldCommandGroups.get(0).getGroupValue());
SolrDocumentList documents = fieldCommandGroups.get(0).getResult();
assertNotNull(documents);
assertEquals(10, documents.size());
assertEquals("116_AR", documents.get(0).getFieldValue("acco_id"));
assertEquals("116_HI", documents.get(1).getFieldValue("acco_id"));
assertEquals("953_AR", documents.get(2).getFieldValue("acco_id"));
assertEquals("953_HI", documents.get(3).getFieldValue("acco_id"));
assertEquals("954_AR", documents.get(4).getFieldValue("acco_id"));
assertEquals("954_HI", documents.get(5).getFieldValue("acco_id"));
assertEquals("546_AR", documents.get(6).getFieldValue("acco_id"));
assertEquals("546_HI", documents.get(7).getFieldValue("acco_id"));
assertEquals("708_AR", documents.get(8).getFieldValue("acco_id"));
assertEquals("708_HI", documents.get(9).getFieldValue("acco_id"));
}
Aggregations