use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.
the class TestSubQueryTransformerDistrib method test.
@SuppressWarnings("serial")
@Test
public void test() throws SolrServerException, IOException {
int peopleMultiplier = atLeast(1);
int deptMultiplier = atLeast(1);
createIndex(people, peopleMultiplier, depts, deptMultiplier);
Random random1 = random();
{
final QueryRequest qr = new QueryRequest(params(new String[] { "q", "name_s:dave", "indent", "true", "fl", "*,depts:[subquery " + ((random1.nextBoolean() ? "" : "separator=,")) + "]", "rows", "" + peopleMultiplier, "depts.q", "{!terms f=dept_id_s v=$row.dept_ss_dv " + ((random1.nextBoolean() ? "" : "separator=,")) + "}", "depts.fl", "text_t" + (differentUniqueId ? ",id:notid" : ""), "depts.indent", "true", "depts.collection", "departments", differentUniqueId ? "depts.distrib.singlePass" : "notnecessary", "true", "depts.rows", "" + (deptMultiplier * 2), "depts.logParamsList", "q,fl,rows,row.dept_ss_dv", random().nextBoolean() ? "depts.wt" : "whatever", anyWt(), random().nextBoolean() ? "wt" : "whatever", anyWt() }));
final QueryResponse rsp = new QueryResponse();
rsp.setResponse(cluster.getSolrClient().request(qr, people));
final SolrDocumentList hits = rsp.getResults();
assertEquals(peopleMultiplier, hits.getNumFound());
int engineerCount = 0;
int supportCount = 0;
for (int res : new int[] { 0, (peopleMultiplier - 1) / 2, peopleMultiplier - 1 }) {
SolrDocument doc = hits.get(res);
assertEquals("dave", doc.getFieldValue("name_s_dv"));
SolrDocumentList relDepts = (SolrDocumentList) doc.getFieldValue("depts");
assertEquals("dave works in both depts " + rsp, deptMultiplier * 2, relDepts.getNumFound());
for (int deptN = 0; deptN < relDepts.getNumFound(); deptN++) {
SolrDocument deptDoc = relDepts.get(deptN);
String actual = (String) deptDoc.get("text_t");
assertTrue(deptDoc + "should be either " + engineering + " or " + support, (engineering.equals(actual) && ++engineerCount > 0) || (support.equals(actual) && ++supportCount > 0));
}
}
assertEquals(hits.toString(), engineerCount, supportCount);
}
}
use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.
the class TestPHPSerializedResponseWriter method testSolrDocuments.
@Test
public void testSolrDocuments() throws IOException {
SolrQueryRequest req = req("q", "*:*");
SolrQueryResponse rsp = new SolrQueryResponse();
QueryResponseWriter w = new PHPSerializedResponseWriter();
StringWriter buf = new StringWriter();
SolrDocument d = new SolrDocument();
SolrDocument d1 = d;
d.addField("id", "1");
d.addField("data1", "hello");
d.addField("data2", 42);
d.addField("data3", true);
// multivalued fields:
// extremely odd edge case: value is a map
// we use LinkedHashMap because we are doing a string comparison
// later and we need predictible ordering
LinkedHashMap<String, String> nl = new LinkedHashMap<>();
nl.put("data4.1", "hashmap");
nl.put("data4.2", "hello");
d.addField("data4", nl);
// array value
d.addField("data5", Arrays.asList("data5.1", "data5.2", "data5.3"));
// adding one more document to test array indexes
d = new SolrDocument();
SolrDocument d2 = d;
d.addField("id", "2");
SolrDocumentList sdl = new SolrDocumentList();
sdl.add(d1);
sdl.add(d2);
rsp.addResponse(sdl);
w.write(buf, req, rsp);
assertEquals("a:1:{s:8:\"response\";a:3:{s:8:\"numFound\";i:0;s:5:\"start\";i:0;s:4:\"docs\";a:2:{i:0;a:6:{s:2:\"id\";s:1:\"1\";s:5:\"data1\";s:5:\"hello\";s:5:\"data2\";i:42;s:5:\"data3\";b:1;s:5:\"data4\";a:2:{s:7:\"data4.1\";s:7:\"hashmap\";s:7:\"data4.2\";s:5:\"hello\";}s:5:\"data5\";a:3:{i:0;s:7:\"data5.1\";i:1;s:7:\"data5.2\";i:2;s:7:\"data5.3\";}}i:1;a:1:{s:2:\"id\";s:1:\"2\";}}}}", buf.toString());
req.close();
}
use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.
the class DocumentBuilderTest method testSolrDocumentEquals.
public void testSolrDocumentEquals() {
String randomString = TestUtil.randomSimpleString(random());
SolrDocument doc1 = new SolrDocument();
doc1.addField("foo", randomString);
SolrDocument doc2 = new SolrDocument();
doc2.addField("foo", randomString);
assertTrue(compareSolrDocument(doc1, doc2));
doc1.addField("foo", "bar");
assertFalse(compareSolrDocument(doc1, doc2));
doc1 = new SolrDocument();
doc1.addField("bar", randomString);
assertFalse(compareSolrDocument(doc1, doc2));
int randomInt = random().nextInt();
doc1 = new SolrDocument();
doc1.addField("foo", randomInt);
doc2 = new SolrDocument();
doc2.addField("foo", randomInt);
assertTrue(compareSolrDocument(doc1, doc2));
doc2 = new SolrDocument();
doc2.addField("bar", randomInt);
assertFalse(compareSolrDocument(doc1, doc2));
}
use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.
the class StreamingBinaryResponseParser method processResponse.
@Override
public NamedList<Object> processResponse(InputStream body, String encoding) {
try {
JavaBinCodec codec = new JavaBinCodec() {
@Override
public SolrDocument readSolrDocument(DataInputInputStream dis) throws IOException {
SolrDocument doc = super.readSolrDocument(dis);
callback.streamSolrDocument(doc);
return null;
}
@Override
public SolrDocumentList readSolrDocumentList(DataInputInputStream dis) throws IOException {
SolrDocumentList solrDocs = new SolrDocumentList();
List list = (List) readVal(dis);
solrDocs.setNumFound((Long) list.get(0));
solrDocs.setStart((Long) list.get(1));
solrDocs.setMaxScore((Float) list.get(2));
callback.streamDocListInfo(solrDocs.getNumFound(), solrDocs.getStart(), solrDocs.getMaxScore());
// Read the Array
tagByte = dis.readByte();
if ((tagByte >>> 5) != (ARR >>> 5)) {
throw new RuntimeException("doclist must have an array");
}
int sz = readSize(dis);
for (int i = 0; i < sz; i++) {
// must be a SolrDocument
readVal(dis);
}
return solrDocs;
}
};
return (NamedList<Object>) codec.unmarshal(body);
} catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "parsing error", e);
}
}
use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.
the class XMLResponseParser method readDocument.
protected SolrDocument readDocument(XMLStreamReader parser) throws XMLStreamException {
if (XMLStreamConstants.START_ELEMENT != parser.getEventType()) {
throw new RuntimeException("must be start element, not: " + parser.getEventType());
}
if (!"doc".equals(parser.getLocalName().toLowerCase(Locale.ROOT))) {
throw new RuntimeException("must be 'lst', not: " + parser.getLocalName());
}
SolrDocument doc = new SolrDocument();
StringBuilder builder = new StringBuilder();
KnownType type = null;
String name = null;
// just eat up the events...
int depth = 0;
while (true) {
switch(parser.next()) {
case XMLStreamConstants.START_ELEMENT:
depth++;
// reset the text
builder.setLength(0);
type = KnownType.get(parser.getLocalName());
if (type == null) {
throw new RuntimeException("this must be known type! not: " + parser.getLocalName());
}
name = null;
int cnt = parser.getAttributeCount();
for (int i = 0; i < cnt; i++) {
if ("name".equals(parser.getAttributeLocalName(i))) {
name = parser.getAttributeValue(i);
break;
}
}
//Nested documents
while (type == KnownType.DOC) {
doc.addChildDocument(readDocument(parser));
int event = parser.next();
if (event == XMLStreamConstants.END_ELEMENT) {
//Doc ends
return doc;
}
}
if (name == null) {
throw new XMLStreamException("requires 'name' attribute: " + parser.getLocalName(), parser.getLocation());
}
// Handle multi-valued fields
if (type == KnownType.ARR) {
for (Object val : readArray(parser)) {
doc.addField(name, val);
}
// the array reading clears out the 'endElement'
depth--;
} else if (type == KnownType.LST) {
doc.addField(name, readNamedList(parser));
depth--;
} else if (!type.isLeaf) {
System.out.println("nbot leaf!:" + type);
throw new XMLStreamException("must be value or array", parser.getLocation());
}
break;
case XMLStreamConstants.END_ELEMENT:
if (--depth < 0) {
return doc;
}
//System.out.println( "FIELD:"+type+"::"+name+"::"+builder );
Object val = type.read(builder.toString().trim());
if (val == null) {
throw new XMLStreamException("error reading value:" + type, parser.getLocation());
}
doc.addField(name, val);
break;
// TODO? should this be trimmed? make sure it only gets one/two space?
case XMLStreamConstants.SPACE:
case XMLStreamConstants.CDATA:
case XMLStreamConstants.CHARACTERS:
builder.append(parser.getText());
break;
}
}
}
Aggregations