Search in sources :

Example 36 with SolrDocument

use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.

the class DocumentObjectBinder method getBeans.

public <T> List<T> getBeans(Class<T> clazz, SolrDocumentList solrDocList) {
    List<DocField> fields = getDocFields(clazz);
    List<T> result = new ArrayList<>(solrDocList.size());
    for (SolrDocument sdoc : solrDocList) {
        result.add(getBean(clazz, fields, sdoc));
    }
    return result;
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument)

Example 37 with SolrDocument

use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.

the class CloudSolrClientTest method testVersionsAreReturned.

@Test
public void testVersionsAreReturned() throws Exception {
    // assert that "adds" are returned
    UpdateRequest updateRequest = new UpdateRequest().add("id", "1", "a_t", "hello1").add("id", "2", "a_t", "hello2");
    updateRequest.setParam(UpdateParams.VERSIONS, Boolean.TRUE.toString());
    NamedList<Object> response = updateRequest.commit(getRandomClient(), COLLECTION).getResponse();
    Object addsObject = response.get("adds");
    assertNotNull("There must be a adds parameter", addsObject);
    assertTrue(addsObject instanceof NamedList<?>);
    NamedList<?> adds = (NamedList<?>) addsObject;
    assertEquals("There must be 2 versions (one per doc)", 2, adds.size());
    Map<String, Long> versions = new HashMap<>();
    Object object = adds.get("1");
    assertNotNull("There must be a version for id 1", object);
    assertTrue("Version for id 1 must be a long", object instanceof Long);
    versions.put("1", (Long) object);
    object = adds.get("2");
    assertNotNull("There must be a version for id 2", object);
    assertTrue("Version for id 2 must be a long", object instanceof Long);
    versions.put("2", (Long) object);
    QueryResponse resp = getRandomClient().query(COLLECTION, new SolrQuery("*:*"));
    assertEquals("There should be one document because overwrite=true", 2, resp.getResults().getNumFound());
    for (SolrDocument doc : resp.getResults()) {
        Long version = versions.get(doc.getFieldValue("id"));
        assertEquals("Version on add must match _version_ field", version, doc.getFieldValue("_version_"));
    }
    // assert that "deletes" are returned
    UpdateRequest deleteRequest = new UpdateRequest().deleteById("1");
    deleteRequest.setParam(UpdateParams.VERSIONS, Boolean.TRUE.toString());
    response = deleteRequest.commit(getRandomClient(), COLLECTION).getResponse();
    Object deletesObject = response.get("deletes");
    assertNotNull("There must be a deletes parameter", deletesObject);
    NamedList deletes = (NamedList) deletesObject;
    assertEquals("There must be 1 version", 1, deletes.size());
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) AbstractUpdateRequest(org.apache.solr.client.solrj.request.AbstractUpdateRequest) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) HashMap(java.util.HashMap) NamedList(org.apache.solr.common.util.NamedList) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 38 with SolrDocument

use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.

the class SolrExampleJettyTest method testArbitraryJsonIndexing.

@Test
public void testArbitraryJsonIndexing() throws Exception {
    HttpSolrClient client = (HttpSolrClient) getSolrClient();
    client.deleteByQuery("*:*");
    client.commit();
    // make sure it got in
    assertNumFound("*:*", 0);
    // two docs, one with uniqueKey, another without it
    String json = "{\"id\":\"abc1\", \"name\": \"name1\"} {\"name\" : \"name2\"}";
    HttpClient httpClient = client.getHttpClient();
    HttpPost post = new HttpPost(getUri(client));
    post.setHeader("Content-Type", "application/json");
    post.setEntity(new InputStreamEntity(new ByteArrayInputStream(json.getBytes("UTF-8")), -1));
    HttpResponse response = httpClient.execute(post, HttpClientUtil.createNewHttpClientRequestContext());
    assertEquals(200, response.getStatusLine().getStatusCode());
    client.commit();
    QueryResponse rsp = getSolrClient().query(new SolrQuery("*:*"));
    assertEquals(2, rsp.getResults().getNumFound());
    SolrDocument doc = rsp.getResults().get(0);
    String src = (String) doc.getFieldValue("_src_");
    Map m = (Map) ObjectBuilder.fromJSON(src);
    assertEquals("abc1", m.get("id"));
    assertEquals("name1", m.get("name"));
    doc = rsp.getResults().get(1);
    src = (String) doc.getFieldValue("_src_");
    m = (Map) ObjectBuilder.fromJSON(src);
    assertEquals("name2", m.get("name"));
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) HttpPost(org.apache.http.client.methods.HttpPost) SolrDocument(org.apache.solr.common.SolrDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(org.apache.http.client.HttpClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) HttpResponse(org.apache.http.HttpResponse) Map(java.util.Map) SolrQuery(org.apache.solr.client.solrj.SolrQuery) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Test(org.junit.Test)

Example 39 with SolrDocument

use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.

the class TestDocumentObjectBinder method testSingleVal4Array.

public void testSingleVal4Array() {
    DocumentObjectBinder binder = new DocumentObjectBinder();
    SolrDocumentList solDocList = new SolrDocumentList();
    SolrDocument d = new SolrDocument();
    solDocList.add(d);
    d.setField("cat", "hello");
    List<Item> l = binder.getBeans(Item.class, solDocList);
    assertEquals("hello", l.get(0).categories[0]);
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) SolrDocumentList(org.apache.solr.common.SolrDocumentList)

Example 40 with SolrDocument

use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.

the class FullSolrCloudDistribCmdsTest method testOptimisticUpdate.

private void testOptimisticUpdate(QueryResponse results) throws Exception {
    SolrDocument doc = results.getResults().get(0);
    Long version = (Long) doc.getFieldValue(VERSION_FIELD);
    Integer theDoc = (Integer) doc.getFieldValue("id");
    UpdateRequest uReq = new UpdateRequest();
    SolrInputDocument doc1 = new SolrInputDocument();
    uReq.setParams(new ModifiableSolrParams());
    uReq.getParams().set(VERSION_FIELD, Long.toString(version));
    addFields(doc1, "id", theDoc, t1, "theupdatestuff");
    uReq.add(doc1);
    uReq.process(cloudClient);
    uReq.process(controlClient);
    commit();
    // updating the old version should fail...
    SolrInputDocument doc2 = new SolrInputDocument();
    uReq = new UpdateRequest();
    uReq.setParams(new ModifiableSolrParams());
    uReq.getParams().set(VERSION_FIELD, Long.toString(version));
    addFields(doc2, "id", theDoc, t1, "thenewupdatestuff");
    uReq.add(doc2);
    uReq.process(cloudClient);
    uReq.process(controlClient);
    commit();
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add("q", t1 + ":thenewupdatestuff");
    QueryResponse res = clients.get(0).query(params);
    assertEquals(0, res.getResults().getNumFound());
    params = new ModifiableSolrParams();
    params.add("q", t1 + ":theupdatestuff");
    res = clients.get(0).query(params);
    assertEquals(1, res.getResults().getNumFound());
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

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