Search in sources :

Example 21 with UpdateResponse

use of org.apache.solr.client.solrj.response.UpdateResponse in project YCSB by brianfrankcooper.

the class SolrClient method update.

/**
   * Update a record in the database. Any field/value pairs in the specified values HashMap will be
   * written into the record with the specified record key, overwriting any existing values with the
   * same field name.
   *
   * @param table
   *          The name of the table
   * @param key
   *          The record key of the record to write.
   * @param values
   *          A HashMap of field/value pairs to update in the record
   * @return Zero on success, a non-zero error code on error. See this class's description for a
   *         discussion of error codes.
   */
@Override
public Status update(String table, String key, HashMap<String, ByteIterator> values) {
    try {
        SolrInputDocument updatedDoc = new SolrInputDocument();
        updatedDoc.addField("id", key);
        for (Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) {
            updatedDoc.addField(entry.getKey(), Collections.singletonMap("set", entry.getValue()));
        }
        UpdateResponse writeResponse;
        if (batchMode) {
            writeResponse = client.add(table, updatedDoc, commitTime);
        } else {
            writeResponse = client.add(table, updatedDoc);
            client.commit(table);
        }
        return checkStatus(writeResponse.getStatus());
    } catch (IOException | SolrServerException e) {
        e.printStackTrace();
    }
    return Status.ERROR;
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException)

Example 22 with UpdateResponse

use of org.apache.solr.client.solrj.response.UpdateResponse in project YCSB by brianfrankcooper.

the class SolrClient method insert.

/**
   * Insert a record in the database. Any field/value pairs in the specified values HashMap will be
   * written into the record with the specified record key.
   *
   * @param table
   *          The name of the table
   * @param key
   *          The record key of the record to insert.
   * @param values
   *          A HashMap of field/value pairs to insert in the record
   * @return Zero on success, a non-zero error code on error. See this class's description for a
   *         discussion of error codes.
   */
@Override
public Status insert(String table, String key, HashMap<String, ByteIterator> values) {
    try {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", key);
        for (Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) {
            doc.addField(entry.getKey(), entry.getValue());
        }
        UpdateResponse response;
        if (batchMode) {
            response = client.add(table, doc, commitTime);
        } else {
            response = client.add(table, doc);
            client.commit(table);
        }
        return checkStatus(response.getStatus());
    } catch (IOException | SolrServerException e) {
        e.printStackTrace();
    }
    return Status.ERROR;
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException)

Example 23 with UpdateResponse

use of org.apache.solr.client.solrj.response.UpdateResponse in project YCSB by brianfrankcooper.

the class SolrClient method delete.

/**
   * Delete a record from the database.
   *
   * @param table
   *          The name of the table
   * @param key
   *          The record key of the record to delete.
   * @return Zero on success, a non-zero error code on error. See this class's description for a
   *         discussion of error codes.
   */
@Override
public Status delete(String table, String key) {
    try {
        UpdateResponse response;
        if (batchMode) {
            response = client.deleteById(table, key, commitTime);
        } else {
            response = client.deleteById(table, key);
            client.commit(table);
        }
        return checkStatus(response.getStatus());
    } catch (IOException | SolrServerException e) {
        e.printStackTrace();
    }
    return Status.ERROR;
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException)

Example 24 with UpdateResponse

use of org.apache.solr.client.solrj.response.UpdateResponse in project gora by apache.

the class SolrStore method deleteByQuery.

@Override
public long deleteByQuery(Query<K, T> query) {
    UpdateResponse rsp;
    try {
        /*
        In this If block we check whether, user needs to delete full document or some fields in the document. We can't delete fields in a document by using solr deleteByQuery method.
        therefore what we have done here is setting the particular fields values into null.
       */
        if (query.getFields() != null && query.getFields().length < mapping.mapping.size() && !(Arrays.asList(query.getFields()).contains(mapping.getPrimaryKey()))) {
            Result<K, T> result = query.execute();
            Map<String, String> partialUpdateNull = new HashMap<>();
            partialUpdateNull.put("set", null);
            while (result.next()) {
                SolrInputDocument inputDoc = new SolrInputDocument();
                inputDoc.setField(mapping.getPrimaryKey(), result.getKey());
                for (String field : query.getFields()) {
                    inputDoc.setField(field, partialUpdateNull);
                }
                batch.add(inputDoc);
            }
            if (commitWithin == 0) {
                rsp = server.add(batch);
                server.commit(false, true, true);
                batch.clear();
                LOG.info(rsp.toString());
            } else {
                rsp = server.add(batch, commitWithin);
                batch.clear();
                LOG.info(rsp.toString());
            }
        } else {
            SolrQuery<K, T> solrQuery = (SolrQuery<K, T>) query;
            String q = solrQuery.toSolrQuery();
            rsp = server.deleteByQuery(q);
            server.commit();
            LOG.info(rsp.toString());
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    }
    return 0;
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SolrQuery(org.apache.gora.solr.query.SolrQuery) SolrServerException(org.apache.solr.client.solrj.SolrServerException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 25 with UpdateResponse

use of org.apache.solr.client.solrj.response.UpdateResponse in project lucene-solr by apache.

the class TestTolerantUpdateProcessorCloud method testSanity.

public void testSanity() throws Exception {
    // verify some basic sanity checking of indexing & querying across the collection
    // w/o using our custom update processor chain
    assertEquals(0, CLOUD_CLIENT.add(doc(f("id", S_ONE_PRE + "1"), f("foo_i", 42))).getStatus());
    assertEquals(0, CLOUD_CLIENT.add(doc(f("id", S_TWO_PRE + "2"), f("foo_i", 66))).getStatus());
    assertEquals(0, CLOUD_CLIENT.commit().getStatus());
    for (SolrClient c : Arrays.asList(S_ONE_LEADER_CLIENT, S_TWO_LEADER_CLIENT, S_ONE_NON_LEADER_CLIENT, S_TWO_NON_LEADER_CLIENT, NO_COLLECTION_CLIENT, CLOUD_CLIENT)) {
        assertQueryDocIds(c, true, S_ONE_PRE + "1", S_TWO_PRE + "2");
        assertQueryDocIds(c, false, "id_not_exists");
        // verify adding 2 broken docs causes a clint exception
        try {
            UpdateResponse rsp = update(params(), doc(f("id", S_ONE_PRE + "X"), f("foo_i", "bogus_val_X")), doc(f("id", S_TWO_PRE + "Y"), f("foo_i", "bogus_val_Y"))).process(c);
            fail("did not get a top level exception when more then 10 docs failed: " + rsp.toString());
        } catch (SolrException e) {
            assertEquals("not the type of error we were expecting (" + e.code() + "): " + e.toString(), 400, e.code());
        }
        // verify malformed deleteByQuerys fail
        try {
            UpdateResponse rsp = update(params()).deleteByQuery("foo_i:not_a_num").process(c);
            fail("sanity check for malformed DBQ didn't fail: " + rsp.toString());
        } catch (SolrException e) {
            assertEquals("not the expected DBQ failure: " + e.getMessage(), 400, e.code());
        }
        // verify opportunistic concurrency deletions fail as we expect when docs are / aren't present
        for (UpdateRequest r : new UpdateRequest[] { update(params("commit", "true")).deleteById(S_ONE_PRE + "1", -1L), update(params("commit", "true")).deleteById(S_TWO_PRE + "2", -1L), update(params("commit", "true")).deleteById("id_not_exists", 1L) }) {
            try {
                UpdateResponse rsp = r.process(c);
                fail("sanity check for opportunistic concurrency delete didn't fail: " + r.toString() + " => " + rsp.toString());
            } catch (SolrException e) {
                assertEquals("not the expected opportunistic concurrency failure code: " + r.toString() + " => " + e.getMessage(), 409, e.code());
            }
        }
    }
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) SolrClient(org.apache.solr.client.solrj.SolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) SolrException(org.apache.solr.common.SolrException)

Aggregations

UpdateResponse (org.apache.solr.client.solrj.response.UpdateResponse)43 SolrInputDocument (org.apache.solr.common.SolrInputDocument)17 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)16 IOException (java.io.IOException)13 SolrServerException (org.apache.solr.client.solrj.SolrServerException)12 ArrayList (java.util.ArrayList)10 SolrClient (org.apache.solr.client.solrj.SolrClient)8 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)8 SolrDocument (org.apache.solr.common.SolrDocument)8 NamedList (org.apache.solr.common.util.NamedList)7 Future (java.util.concurrent.Future)6 Test (org.junit.Test)6 ExecutorService (java.util.concurrent.ExecutorService)5 SolrException (org.apache.solr.common.SolrException)5 DefaultSolrThreadFactory (org.apache.solr.util.DefaultSolrThreadFactory)5 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)4 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)3 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)3 MalformedURLException (java.net.MalformedURLException)2 HashMap (java.util.HashMap)2