Search in sources :

Example 86 with UpdateRequest

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

the class CustomCollectionTest method testRouteFieldForImplicitRouter.

@Test
public void testRouteFieldForImplicitRouter() throws Exception {
    int numShards = 4;
    int replicationFactor = TestUtil.nextInt(random(), 0, 3) + 2;
    int maxShardsPerNode = ((numShards * replicationFactor) / NODE_COUNT) + 1;
    String shard_fld = "shard_s";
    final String collection = "withShardField";
    CollectionAdminRequest.createCollectionWithImplicitRouter(collection, "conf", "a,b,c,d", replicationFactor).setMaxShardsPerNode(maxShardsPerNode).setRouterField(shard_fld).process(cluster.getSolrClient());
    new UpdateRequest().add("id", "6", shard_fld, "a").add("id", "7", shard_fld, "a").add("id", "8", shard_fld, "b").commit(cluster.getSolrClient(), collection);
    assertEquals(3, cluster.getSolrClient().query(collection, new SolrQuery("*:*")).getResults().getNumFound());
    assertEquals(1, cluster.getSolrClient().query(collection, new SolrQuery("*:*").setParam(_ROUTE_, "b")).getResults().getNumFound());
    assertEquals(2, cluster.getSolrClient().query(collection, new SolrQuery("*:*").setParam(_ROUTE_, "a")).getResults().getNumFound());
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 87 with UpdateRequest

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

the class TestDistribDocBasedVersion method doDBQ.

// TODO: refactor some of this stuff into the SolrJ client... it should be easier to use
void doDBQ(String q, String... reqParams) throws Exception {
    UpdateRequest req = new UpdateRequest();
    req.deleteByQuery(q);
    req.setParams(params(reqParams));
    req.process(cloudClient);
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest)

Example 88 with UpdateRequest

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

the class TestDistribDocBasedVersion method vadd.

void vadd(String id, long version, String... params) throws Exception {
    UpdateRequest req = new UpdateRequest();
    req.add(sdoc("id", id, vfield, version));
    for (int i = 0; i < params.length; i += 2) {
        req.setParam(params[i], params[i + 1]);
    }
    solrClient.request(req);
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest)

Example 89 with UpdateRequest

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

the class BasicAuthIntegrationTest method testBasicAuth.

@Test
public void testBasicAuth() throws Exception {
    boolean isUseV2Api = random().nextBoolean();
    String authcPrefix = "/admin/authentication";
    String authzPrefix = "/admin/authorization";
    if (isUseV2Api) {
        authcPrefix = "/____v2/cluster/security/authentication";
        authzPrefix = "/____v2/cluster/security/authorization";
    }
    NamedList<Object> rsp;
    HttpClient cl = null;
    try {
        cl = HttpClientUtil.createClient(null);
        JettySolrRunner randomJetty = cluster.getRandomJetty(random());
        String baseUrl = randomJetty.getBaseUrl().toString();
        verifySecurityStatus(cl, baseUrl + authcPrefix, "/errorMessages", null, 20);
        zkClient().setData("/security.json", STD_CONF.replaceAll("'", "\"").getBytes(UTF_8), true);
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/class", "solr.BasicAuthPlugin", 20);
        randomJetty.stop();
        randomJetty.start(false);
        baseUrl = randomJetty.getBaseUrl().toString();
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/class", "solr.BasicAuthPlugin", 20);
        String command = "{\n" + "'set-user': {'harry':'HarryIsCool'}\n" + "}";
        final SolrRequest genericReq;
        if (isUseV2Api) {
            genericReq = new V2Request.Builder("/cluster/security/authentication").withMethod(SolrRequest.METHOD.POST).build();
        } else {
            genericReq = new GenericSolrRequest(SolrRequest.METHOD.POST, authcPrefix, new ModifiableSolrParams());
            ((GenericSolrRequest) genericReq).setContentStreams(Collections.singletonList(new ContentStreamBase.ByteArrayStream(command.getBytes(UTF_8), "")));
        }
        HttpSolrClient.RemoteSolrException exp = expectThrows(HttpSolrClient.RemoteSolrException.class, () -> {
            cluster.getSolrClient().request(genericReq);
        });
        assertEquals(401, exp.code());
        command = "{\n" + "'set-user': {'harry':'HarryIsUberCool'}\n" + "}";
        HttpPost httpPost = new HttpPost(baseUrl + authcPrefix);
        setBasicAuthHeader(httpPost, "solr", "SolrRocks");
        httpPost.setEntity(new ByteArrayEntity(command.getBytes(UTF_8)));
        httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication.enabled", "true", 20);
        HttpResponse r = cl.execute(httpPost);
        int statusCode = r.getStatusLine().getStatusCode();
        Utils.consumeFully(r.getEntity());
        assertEquals("proper_cred sent, but access denied", 200, statusCode);
        baseUrl = cluster.getRandomJetty(random()).getBaseUrl().toString();
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/credentials/harry", NOT_NULL_PREDICATE, 20);
        command = "{\n" + "'set-user-role': {'harry':'admin'}\n" + "}";
        executeCommand(baseUrl + authzPrefix, cl, command, "solr", "SolrRocks");
        baseUrl = cluster.getRandomJetty(random()).getBaseUrl().toString();
        verifySecurityStatus(cl, baseUrl + authzPrefix, "authorization/user-role/harry", NOT_NULL_PREDICATE, 20);
        executeCommand(baseUrl + authzPrefix, cl, Utils.toJSONString(singletonMap("set-permission", Utils.makeMap("collection", "x", "path", "/update/*", "role", "dev"))), "harry", "HarryIsUberCool");
        verifySecurityStatus(cl, baseUrl + authzPrefix, "authorization/permissions[1]/collection", "x", 20);
        executeCommand(baseUrl + authzPrefix, cl, Utils.toJSONString(singletonMap("set-permission", Utils.makeMap("name", "collection-admin-edit", "role", "admin"))), "harry", "HarryIsUberCool");
        verifySecurityStatus(cl, baseUrl + authzPrefix, "authorization/permissions[2]/name", "collection-admin-edit", 20);
        CollectionAdminRequest.Reload reload = CollectionAdminRequest.reloadCollection(COLLECTION);
        try (HttpSolrClient solrClient = getHttpSolrClient(baseUrl)) {
            try {
                rsp = solrClient.request(reload);
                fail("must have failed");
            } catch (HttpSolrClient.RemoteSolrException e) {
            }
            reload.setMethod(SolrRequest.METHOD.POST);
            try {
                rsp = solrClient.request(reload);
                fail("must have failed");
            } catch (HttpSolrClient.RemoteSolrException e) {
            }
        }
        cluster.getSolrClient().request(CollectionAdminRequest.reloadCollection(COLLECTION).setBasicAuthCredentials("harry", "HarryIsUberCool"));
        try {
            cluster.getSolrClient().request(CollectionAdminRequest.reloadCollection(COLLECTION).setBasicAuthCredentials("harry", "Cool12345"));
            fail("This should not succeed");
        } catch (HttpSolrClient.RemoteSolrException e) {
        }
        executeCommand(baseUrl + authzPrefix, cl, "{set-permission : { name : update , role : admin}}", "harry", "HarryIsUberCool");
        SolrInputDocument doc = new SolrInputDocument();
        doc.setField("id", "4");
        UpdateRequest update = new UpdateRequest();
        update.setBasicAuthCredentials("harry", "HarryIsUberCool");
        update.add(doc);
        update.setCommitWithin(100);
        cluster.getSolrClient().request(update, COLLECTION);
        executeCommand(baseUrl + authcPrefix, cl, "{set-property : { blockUnknown: true}}", "harry", "HarryIsUberCool");
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/blockUnknown", "true", 20, "harry", "HarryIsUberCool");
        verifySecurityStatus(cl, baseUrl + "/admin/info/key?wt=json", "key", NOT_NULL_PREDICATE, 20);
        String[] toolArgs = new String[] { "status", "-solr", baseUrl };
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream stdoutSim = new PrintStream(baos, true, StandardCharsets.UTF_8.name());
        SolrCLI.StatusTool tool = new SolrCLI.StatusTool(stdoutSim);
        try {
            System.setProperty("basicauth", "harry:HarryIsUberCool");
            tool.runTool(SolrCLI.processCommandLineArgs(SolrCLI.joinCommonAndToolOptions(tool.getOptions()), toolArgs));
            Map obj = (Map) Utils.fromJSON(new ByteArrayInputStream(baos.toByteArray()));
            assertTrue(obj.containsKey("version"));
            assertTrue(obj.containsKey("startTime"));
            assertTrue(obj.containsKey("uptime"));
            assertTrue(obj.containsKey("memory"));
        } catch (Exception e) {
            log.error("RunExampleTool failed due to: " + e + "; stdout from tool prior to failure: " + baos.toString(StandardCharsets.UTF_8.name()));
        }
        executeCommand(baseUrl + authcPrefix, cl, "{set-property : { blockUnknown: false}}", "harry", "HarryIsUberCool");
    } finally {
        if (cl != null) {
            HttpClientUtil.close(cl);
        }
    }
}
Also used : GenericSolrRequest(org.apache.solr.client.solrj.request.GenericSolrRequest) HttpPost(org.apache.http.client.methods.HttpPost) SolrRequest(org.apache.solr.client.solrj.SolrRequest) GenericSolrRequest(org.apache.solr.client.solrj.request.GenericSolrRequest) CollectionAdminRequest(org.apache.solr.client.solrj.request.CollectionAdminRequest) SolrCLI(org.apache.solr.util.SolrCLI) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrInputDocument(org.apache.solr.common.SolrInputDocument) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) PrintStream(java.io.PrintStream) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) HttpResponse(org.apache.http.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) V2Request(org.apache.solr.client.solrj.request.V2Request) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(org.apache.http.client.HttpClient) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Test(org.junit.Test)

Example 90 with UpdateRequest

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

the class TestInPlaceUpdatesDistrib method addDocAndGetVersion.

@SuppressWarnings("rawtypes")
protected long addDocAndGetVersion(Object... fields) throws Exception {
    SolrInputDocument doc = new SolrInputDocument();
    addFields(doc, fields);
    UpdateRequest ureq = new UpdateRequest();
    ureq.setParam("versions", "true");
    ureq.add(doc);
    UpdateResponse resp;
    // send updates to leader, to avoid SOLR-8733
    resp = ureq.process(LEADER);
    long returnedVersion = Long.parseLong(((NamedList) resp.getResponse().get("adds")).getVal(0).toString());
    assertTrue("Due to SOLR-8733, sometimes returned version is 0. Let us assert that we have successfully" + " worked around that problem here.", returnedVersion > 0);
    return returnedVersion;
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) NamedList(org.apache.solr.common.util.NamedList)

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