Search in sources :

Example 66 with SolrDocument

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;
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument)

Example 67 with SolrDocument

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"));
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) SolrParams(org.apache.solr.common.params.SolrParams) Test(org.junit.Test)

Example 68 with SolrDocument

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;
    }
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 69 with SolrDocument

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));
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SolrDocumentList(org.apache.solr.common.SolrDocumentList)

Example 70 with SolrDocument

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"));
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) ArrayList(java.util.ArrayList)

Aggregations

SolrDocument (org.apache.solr.common.SolrDocument)230 SolrDocumentList (org.apache.solr.common.SolrDocumentList)93 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)81 ArrayList (java.util.ArrayList)50 SolrQuery (org.apache.solr.client.solrj.SolrQuery)47 Test (org.junit.Test)46 SolrParams (org.apache.solr.common.params.SolrParams)38 SolrServerException (org.apache.solr.client.solrj.SolrServerException)35 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)35 IOException (java.io.IOException)32 SolrInputDocument (org.apache.solr.common.SolrInputDocument)28 HashMap (java.util.HashMap)26 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)24 NamedList (org.apache.solr.common.util.NamedList)21 Map (java.util.Map)20 List (java.util.List)18 SolrClient (org.apache.solr.client.solrj.SolrClient)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)12 SolrException (org.apache.solr.common.SolrException)12