use of org.apache.solr.client.solrj.response.QueryResponse 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.response.QueryResponse 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.response.QueryResponse in project lucene-solr by apache.
the class SolrExampleTests method testUpdateRequestWithParameters.
@Test
public void testUpdateRequestWithParameters() throws Exception {
SolrClient client = createNewSolrClient();
client.deleteByQuery("*:*");
client.commit();
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "id1");
UpdateRequest req = new UpdateRequest();
req.setParam("overwrite", "false");
req.add(doc);
client.request(req);
client.request(req);
client.commit();
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
QueryResponse rsp = client.query(query);
SolrDocumentList out = rsp.getResults();
assertEquals(2, out.getNumFound());
if (!(client instanceof EmbeddedSolrServer)) {
/* Do not close in case of using EmbeddedSolrServer,
* as that would close the CoreContainer */
client.close();
}
}
use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.
the class SolrExampleTests method testChineseDefaults.
@Test
public void testChineseDefaults() throws Exception {
SolrClient client = getSolrClient();
// Empty the database...
// delete everything!
client.deleteByQuery("*:*");
client.commit();
// make sure it got in
assertNumFound("*:*", 0);
// Beijing medical University
UpdateRequest req = new UpdateRequest();
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "42");
doc.addField("text", "北京医科大学");
req.add(doc);
req.setAction(ACTION.COMMIT, true, true);
req.process(client);
// Beijing university should match:
SolrQuery query = new SolrQuery("北京大学");
QueryResponse rsp = client.query(query);
assertEquals(1, rsp.getResults().getNumFound());
}
use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.
the class SolrExampleTests method testAugmentFields.
@Test
public void testAugmentFields() throws Exception {
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("price", 11);
client.add(doc);
// make sure this gets in first
client.commit();
doc = new SolrInputDocument();
doc.addField("id", "222");
doc.addField("name", "doc2");
doc.addField("price", 22);
client.add(doc);
client.commit();
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.set(CommonParams.FL, "id,price,[docid],[explain style=nl],score,aaa:[value v=aaa],ten:[value v=10 t=int]");
query.addSort(new SolrQuery.SortClause("price", SolrQuery.ORDER.asc));
QueryResponse rsp = client.query(query);
SolrDocumentList out = rsp.getResults();
assertEquals(2, out.getNumFound());
SolrDocument out1 = out.get(0);
SolrDocument out2 = out.get(1);
assertEquals("111", out1.getFieldValue("id"));
assertEquals("222", out2.getFieldValue("id"));
assertEquals(1.0f, out1.getFieldValue("score"));
assertEquals(1.0f, out2.getFieldValue("score"));
// check that the docid is one bigger
int id1 = (Integer) out1.getFieldValue("[docid]");
int id2 = (Integer) out2.getFieldValue("[docid]");
assertTrue("should be bigger [" + id1 + "," + id2 + "]", id2 > id1);
// The score from explain should be the same as the score
NamedList explain = (NamedList) out1.getFieldValue("[explain]");
assertEquals(out1.get("score"), explain.get("value"));
// Augmented _value_ with alias
assertEquals("aaa", out1.get("aaa"));
assertEquals(10, ((Integer) out1.get("ten")).intValue());
}
Aggregations