use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.
the class JavaBinCodec method readSolrDocument.
public SolrDocument readSolrDocument(DataInputInputStream dis) throws IOException {
tagByte = dis.readByte();
int size = readSize(dis);
SolrDocument doc = new SolrDocument(new LinkedHashMap<>(size));
for (int i = 0; i < size; i++) {
String fieldName;
// could be a field name, or a child document
Object obj = readVal(dis);
if (obj instanceof SolrDocument) {
doc.addChildDocument((SolrDocument) obj);
continue;
} else {
fieldName = (String) obj;
}
Object fieldVal = readVal(dis);
doc.setField(fieldName, fieldVal);
}
return doc;
}
use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.
the class GetByIdTest method testGetIdWithParams.
@Test
public void testGetIdWithParams() throws Exception {
final SolrParams ID_FL_ONLY = params(CommonParams.FL, "id");
SolrDocument rsp = getSolrClient().getById("0", ID_FL_ONLY);
assertNull(rsp);
rsp = getSolrClient().getById("1", ID_FL_ONLY);
assertEquals("1", rsp.get("id"));
assertNull("This field should have been removed from the response.", rsp.get("term_s"));
assertNull("This field should have been removed from the response.", rsp.get("term2_s"));
rsp = getSolrClient().getById("2", ID_FL_ONLY);
assertEquals("2", rsp.get("id"));
assertNull("This field should have been removed from the response.", rsp.get("term_s"));
assertNull("This field should have been removed from the response.", rsp.get("term2_s"));
}
use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.
the class TestJavaBinCodec method testBackCompatForSolrDocumentWithChildDocs.
@Test
public void testBackCompatForSolrDocumentWithChildDocs() throws IOException {
JavaBinCodec javabin = new JavaBinCodec() {
@Override
public List<Object> readIterator(DataInputInputStream fis) throws IOException {
return super.readIterator(fis);
}
};
try {
InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN_CHILD_DOCS);
SolrDocument sdoc = (SolrDocument) javabin.unmarshal(is);
SolrDocument matchSolrDoc = generateSolrDocumentWithChildDocs();
assertTrue(compareSolrDocument(sdoc, matchSolrDoc));
} catch (IOException e) {
throw e;
}
}
use of org.apache.solr.common.SolrDocument 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.SolrDocument in project lucene-solr by apache.
the class SolrDocumentTest method testAddCollections.
public void testAddCollections() {
final List<String> c0 = new ArrayList<>();
c0.add("aaa");
c0.add("aaa");
c0.add("aaa");
c0.add("bbb");
c0.add("ccc");
c0.add("ddd");
SolrDocument doc = new SolrDocument();
doc.addField("v", c0);
assertEquals(c0.size(), doc.getFieldValues("v").size());
assertEquals(c0.get(0), doc.getFirstValue("v"));
// Same thing with an array
Object[] arr = new Object[] { "aaa", "aaa", "aaa", 10, 'b' };
doc = new SolrDocument();
doc.addField("v", arr);
assertEquals(arr.length, doc.getFieldValues("v").size());
// try the same thing with 'setField'
doc.setField("v", arr);
assertEquals(arr.length, doc.getFieldValues("v").size());
doc.clear();
assertEquals(0, doc.getFieldNames().size());
Iterable iter = new Iterable() {
@Override
public Iterator iterator() {
return c0.iterator();
}
};
doc.addField("v", iter);
assertEquals(c0.size(), doc.getFieldValues("v").size());
// do it again to get twice the size...
doc.addField("v", iter);
assertEquals(c0.size() * 2, doc.getFieldValues("v").size());
// An empty list:
doc.setField("empty", new ArrayList<String>());
assertNull(doc.getFirstValue("empty"));
// Try the JSTL accessor functions...
assertFalse(doc.getFieldValueMap().isEmpty());
assertFalse(doc.getFieldValuesMap().isEmpty());
assertEquals(2, doc.getFieldValueMap().size());
assertEquals(2, doc.getFieldValuesMap().size());
assertTrue(doc.getFieldValueMap().containsKey("v"));
assertTrue(doc.getFieldValuesMap().containsKey("v"));
assertTrue(doc.getFieldValueMap().keySet().contains("v"));
assertTrue(doc.getFieldValuesMap().keySet().contains("v"));
assertFalse(doc.getFieldValueMap().containsKey("g"));
assertFalse(doc.getFieldValuesMap().containsKey("g"));
assertFalse(doc.getFieldValueMap().keySet().contains("g"));
assertFalse(doc.getFieldValuesMap().keySet().contains("g"));
// A read-only list shouldn't break addField("v", ...).
List<String> ro = Collections.unmodifiableList(c0);
doc = new SolrDocument();
doc.addField("v", ro);
// This should NOT throw an UnsupportedOperationException.
doc.addField("v", "asdf");
// set field using a collection is documented to be backed by
// that collection, so changes should affect it.
Collection<String> tmp = new ArrayList<>(3);
tmp.add("one");
doc.setField("collection_backed", tmp);
assertEquals("collection not the same", tmp, doc.getFieldValues("collection_backed"));
tmp.add("two");
assertEquals("wrong size", 2, doc.getFieldValues("collection_backed").size());
assertEquals("collection not the same", tmp, doc.getFieldValues("collection_backed"));
}
Aggregations