Search in sources :

Example 16 with UpdateRequest

use of org.apache.solr.client.solrj.request.UpdateRequest in project lucene-solr by apache.

the class SolrClient method add.

/**
   * Adds a single document specifying max time before it becomes committed
   *
   * @param collection the Solr collection to add the document to
   * @param doc  the input document
   * @param commitWithinMs  max time (in ms) before a commit will happen
   *
   * @return an {@link org.apache.solr.client.solrj.response.UpdateResponse} from the server
   *
   * @throws IOException         if there is a communication error with the server
   * @throws SolrServerException if there is an error on the server
   *
   * @since solr 5.1
   */
public UpdateResponse add(String collection, SolrInputDocument doc, int commitWithinMs) throws SolrServerException, IOException {
    UpdateRequest req = new UpdateRequest();
    req.add(doc);
    req.setCommitWithin(commitWithinMs);
    return req.process(this, collection);
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest)

Example 17 with UpdateRequest

use of org.apache.solr.client.solrj.request.UpdateRequest in project lucene-solr by apache.

the class BasicHttpSolrClientTest method testUpdate.

@Test
public void testUpdate() throws Exception {
    DebugServlet.clear();
    try (HttpSolrClient client = getHttpSolrClient(jetty.getBaseUrl().toString() + "/debug/foo")) {
        UpdateRequest req = new UpdateRequest();
        req.add(new SolrInputDocument());
        req.setParam("a", "ሴ");
        try {
            client.request(req);
        } catch (ParseException ignored) {
        }
        //default method
        assertEquals("post", DebugServlet.lastMethod);
        //agent
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        //default wt
        assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
        assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
        //default version
        assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
        assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
        //content type
        assertEquals("application/javabin", DebugServlet.headers.get("Content-Type"));
        //parameter encoding
        assertEquals(1, DebugServlet.parameters.get("a").length);
        assertEquals("ሴ", DebugServlet.parameters.get("a")[0]);
        //XML response and writer
        client.setParser(new XMLResponseParser());
        client.setRequestWriter(new RequestWriter());
        try {
            client.request(req);
        } catch (ParseException ignored) {
        }
        assertEquals("post", DebugServlet.lastMethod);
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
        assertEquals("xml", DebugServlet.parameters.get(CommonParams.WT)[0]);
        assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
        assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
        assertEquals("application/xml; charset=UTF-8", DebugServlet.headers.get("Content-Type"));
        assertEquals(1, DebugServlet.parameters.get("a").length);
        assertEquals("ሴ", DebugServlet.parameters.get("a")[0]);
        //javabin request
        client.setParser(new BinaryResponseParser());
        client.setRequestWriter(new BinaryRequestWriter());
        DebugServlet.clear();
        try {
            client.request(req);
        } catch (ParseException ignored) {
        }
        assertEquals("post", DebugServlet.lastMethod);
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
        assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
        assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
        assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
        assertEquals("application/javabin", DebugServlet.headers.get("Content-Type"));
        assertEquals(1, DebugServlet.parameters.get("a").length);
        assertEquals("ሴ", DebugServlet.parameters.get("a")[0]);
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) ParseException(org.apache.http.ParseException) RequestWriter(org.apache.solr.client.solrj.request.RequestWriter) Test(org.junit.Test)

Example 18 with UpdateRequest

use of org.apache.solr.client.solrj.request.UpdateRequest 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 19 with UpdateRequest

use of org.apache.solr.client.solrj.request.UpdateRequest in project lucene-solr by apache.

the class SolrExampleStreamingTest method testWaitOptions.

public void testWaitOptions() throws Exception {
    // SOLR-3903
    final List<Throwable> failures = new ArrayList<>();
    final String serverUrl = jetty.getBaseUrl().toString() + "/collection1";
    try (ConcurrentUpdateSolrClient concurrentClient = new FailureRecordingConcurrentUpdateSolrClient(serverUrl, 2, 2)) {
        int docId = 42;
        for (UpdateRequest.ACTION action : EnumSet.allOf(UpdateRequest.ACTION.class)) {
            for (boolean waitSearch : Arrays.asList(true, false)) {
                for (boolean waitFlush : Arrays.asList(true, false)) {
                    UpdateRequest updateRequest = new UpdateRequest();
                    SolrInputDocument document = new SolrInputDocument();
                    document.addField("id", docId++);
                    updateRequest.add(document);
                    updateRequest.setAction(action, waitSearch, waitFlush);
                    concurrentClient.request(updateRequest);
                }
            }
        }
        concurrentClient.commit();
        concurrentClient.blockUntilFinished();
    }
    if (0 != failures.size()) {
        assertEquals(failures.size() + " Unexpected Exception, starting with...", null, failures.get(0));
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) ArrayList(java.util.ArrayList) ConcurrentUpdateSolrClient(org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient)

Example 20 with UpdateRequest

use of org.apache.solr.client.solrj.request.UpdateRequest in project lucene-solr by apache.

the class TestSolrProperties method testProperties.

@Test
public void testProperties() throws Exception {
    UpdateRequest up = new UpdateRequest();
    up.setAction(ACTION.COMMIT, true, true);
    up.deleteByQuery("*:*");
    up.process(getSolrCore0());
    up.process(getSolrCore1());
    up.clear();
    // Add something to each core
    SolrInputDocument doc = new SolrInputDocument();
    doc.setField("id", "AAA");
    doc.setField("core0", "yup stopfra stopfrb stopena stopenb");
    // Add to core0
    up.add(doc);
    up.process(getSolrCore0());
    SolrTestCaseJ4.ignoreException("unknown field");
    // You can't add it to core1
    try {
        up.process(getSolrCore1());
        fail("Can't add core0 field to core1!");
    } catch (Exception ex) {
    }
    // Add to core1
    doc.setField("id", "BBB");
    doc.setField("core1", "yup stopfra stopfrb stopena stopenb");
    doc.removeField("core0");
    up.add(doc);
    up.process(getSolrCore1());
    // You can't add it to core1
    try {
        SolrTestCaseJ4.ignoreException("core0");
        up.process(getSolrCore0());
        fail("Can't add core1 field to core0!");
    } catch (Exception ex) {
    }
    SolrTestCaseJ4.resetExceptionIgnores();
    // now Make sure AAA is in 0 and BBB in 1
    SolrQuery q = new SolrQuery();
    QueryRequest r = new QueryRequest(q);
    q.setQuery("id:AAA");
    assertEquals(1, r.process(getSolrCore0()).getResults().size());
    assertEquals(0, r.process(getSolrCore1()).getResults().size());
    // Now test Changing the default core
    assertEquals(1, getSolrCore0().query(new SolrQuery("id:AAA")).getResults().size());
    assertEquals(0, getSolrCore0().query(new SolrQuery("id:BBB")).getResults().size());
    assertEquals(0, getSolrCore1().query(new SolrQuery("id:AAA")).getResults().size());
    assertEquals(1, getSolrCore1().query(new SolrQuery("id:BBB")).getResults().size());
    // Now test reloading it should have a newer open time
    String name = "core0";
    SolrClient coreadmin = getSolrAdmin();
    CoreAdminResponse mcr = CoreAdminRequest.getStatus(name, coreadmin);
    long before = mcr.getStartTime(name).getTime();
    CoreAdminRequest.reloadCore(name, coreadmin);
    mcr = CoreAdminRequest.getStatus(name, coreadmin);
    long after = mcr.getStartTime(name).getTime();
    assertTrue("should have more recent time: " + after + "," + before, after > before);
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) SolrClient(org.apache.solr.client.solrj.SolrClient) CoreAdminResponse(org.apache.solr.client.solrj.response.CoreAdminResponse) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Aggregations

UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)276 Test (org.junit.Test)151 Tuple (org.apache.solr.client.solrj.io.Tuple)114 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)89 SolrClientCache (org.apache.solr.client.solrj.io.SolrClientCache)88 SolrInputDocument (org.apache.solr.common.SolrInputDocument)76 StreamFactory (org.apache.solr.client.solrj.io.stream.expr.StreamFactory)64 ArrayList (java.util.ArrayList)38 StreamExpression (org.apache.solr.client.solrj.io.stream.expr.StreamExpression)36 IOException (java.io.IOException)30 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)26 SolrParams (org.apache.solr.common.params.SolrParams)26 SolrQuery (org.apache.solr.client.solrj.SolrQuery)24 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)23 AbstractUpdateRequest (org.apache.solr.client.solrj.request.AbstractUpdateRequest)22 HashMap (java.util.HashMap)21 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)21 FieldComparator (org.apache.solr.client.solrj.io.comp.FieldComparator)21 SolrServerException (org.apache.solr.client.solrj.SolrServerException)20 List (java.util.List)19