use of org.apache.solr.common.SolrDocumentList 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.common.SolrDocumentList 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());
}
use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.
the class SolrExampleTests method testMoreLikeThis.
@Test
public void testMoreLikeThis() throws Exception {
SolrClient client = getSolrClient();
client.deleteByQuery("*:*");
for (int i = 0; i < 20; i++) {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "testMoreLikeThis" + i);
doc.addField("x_s", "x_" + i);
doc.addField("y_s", "y_" + (i % 3));
doc.addField("z_s", "z_" + i);
client.add(doc);
}
client.commit();
// test with mlt.fl having comma separated values
SolrQuery q = new SolrQuery("*:*");
q.setRows(20);
q.setMoreLikeThisFields("x_s", "y_s", "z_s");
q.setMoreLikeThisMinTermFreq(0);
q.setMoreLikeThisCount(2);
QueryResponse response = client.query(q);
assertEquals(20, response.getResults().getNumFound());
NamedList<SolrDocumentList> moreLikeThis = response.getMoreLikeThis();
assertNotNull("MoreLikeThis response should not have been null", moreLikeThis);
for (int i = 0; i < 20; i++) {
String id = "testMoreLikeThis" + i;
SolrDocumentList mltResp = moreLikeThis.get(id);
assertNotNull("MoreLikeThis response for id=" + id + " should not be null", mltResp);
assertTrue("MoreLikeThis response for id=" + id + " had numFound=0", mltResp.getNumFound() > 0);
assertTrue("MoreLikeThis response for id=" + id + " had not returned exactly 2 documents", mltResp.size() == 2);
}
// now test with multiple mlt.fl parameters
q = new SolrQuery("*:*");
q.setRows(20);
q.setParam("mlt", "true");
q.setParam("mlt.fl", "x_s", "y_s", "z_s");
q.setMoreLikeThisMinTermFreq(0);
q.setMoreLikeThisCount(2);
response = client.query(q);
assertEquals(20, response.getResults().getNumFound());
moreLikeThis = response.getMoreLikeThis();
assertNotNull("MoreLikeThis response should not have been null", moreLikeThis);
for (int i = 0; i < 20; i++) {
String id = "testMoreLikeThis" + i;
SolrDocumentList mltResp = moreLikeThis.get(id);
assertNotNull("MoreLikeThis response for id=" + id + " should not be null", mltResp);
assertTrue("MoreLikeThis response for id=" + id + " had numFound=0", mltResp.getNumFound() > 0);
assertTrue("MoreLikeThis response for id=" + id + " had not returned exactly 2 documents", mltResp.size() == 2);
}
}
use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.
the class TestNamedListCodec method testSimple.
public void testSimple() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
NamedList nl = new NamedList();
Float fval = new Float(10.01f);
Boolean bval = Boolean.TRUE;
String sval = "12qwaszx";
// Set up a simple document
NamedList r = new NamedList();
nl.add("responseHeader", r);
r.add("status", 0);
r.add("QTime", 63);
NamedList p = new NamedList();
r.add("params", p);
p.add("rows", 10);
p.add("start", 0);
p.add("indent", "on");
p.add("q", "ipod");
SolrDocumentList list = new SolrDocumentList();
nl.add("response", list);
list.setMaxScore(1.0f);
list.setStart(10);
list.setNumFound(12);
SolrDocument doc = new SolrDocument();
doc.addField("f", fval);
doc.addField("b", bval);
doc.addField("s", sval);
doc.addField("f", 100);
list.add(doc);
doc = new SolrDocument();
doc.addField("f", fval);
doc.addField("b", bval);
doc.addField("s", sval);
doc.addField("f", 101);
list.add(doc);
nl.add("zzz", doc);
new JavaBinCodec(null).marshal(nl, baos);
byte[] arr = baos.toByteArray();
nl = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(arr));
assertEquals(3, nl.size());
assertEquals("ipod", ((NamedList) ((NamedList) nl.getVal(0)).get("params")).get("q"));
list = (SolrDocumentList) nl.getVal(1);
assertEquals(12, list.getNumFound());
assertEquals(10, list.getStart());
assertEquals(101, ((List) list.get(1).getFieldValue("f")).get(1));
}
use of org.apache.solr.common.SolrDocumentList 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