Search in sources :

Example 76 with CloudSolrClient

use of org.apache.solr.client.solrj.impl.CloudSolrClient in project lucene-solr by apache.

the class TestSolrCLIRunExample method testInteractiveSolrCloudExample.

/**
   * Tests the interactive SolrCloud example; we cannot test the non-interactive because we need control over
   * the port and can only test with one node since the test relies on setting the host and jetty.port system
   * properties, i.e. there is no test coverage for the -noprompt option.
   */
@Test
public void testInteractiveSolrCloudExample() throws Exception {
    File solrHomeDir = new File(ExternalPaths.SERVER_HOME);
    if (!solrHomeDir.isDirectory())
        fail(solrHomeDir.getAbsolutePath() + " not found and is required to run this test!");
    Path tmpDir = createTempDir();
    File solrExampleDir = tmpDir.toFile();
    File solrServerDir = solrHomeDir.getParentFile();
    String[] toolArgs = new String[] { "-example", "cloud", "-serverDir", solrServerDir.getAbsolutePath(), "-exampleDir", solrExampleDir.getAbsolutePath() };
    int bindPort = -1;
    try (ServerSocket socket = new ServerSocket(0)) {
        bindPort = socket.getLocalPort();
    }
    String collectionName = "testCloudExamplePrompt";
    // sthis test only support launching one SolrCloud node due to how MiniSolrCloudCluster works
    // and the need for setting the host and port system properties ...
    String userInput = "1\n" + bindPort + "\n" + collectionName + "\n2\n2\ndata_driven_schema_configs\n";
    // simulate user input from stdin
    InputStream userInputSim = new ByteArrayInputStream(userInput.getBytes(StandardCharsets.UTF_8));
    // capture tool output to stdout
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream stdoutSim = new PrintStream(baos, true, StandardCharsets.UTF_8.name());
    RunExampleExecutor executor = new RunExampleExecutor(stdoutSim);
    closeables.add(executor);
    SolrCLI.RunExampleTool tool = new SolrCLI.RunExampleTool(executor, userInputSim, stdoutSim);
    try {
        tool.runTool(SolrCLI.processCommandLineArgs(SolrCLI.joinCommonAndToolOptions(tool.getOptions()), toolArgs));
    } catch (Exception e) {
        System.err.println("RunExampleTool failed due to: " + e + "; stdout from tool prior to failure: " + baos.toString(StandardCharsets.UTF_8.name()));
        throw e;
    }
    String toolOutput = baos.toString(StandardCharsets.UTF_8.name());
    // verify Solr is running on the expected port and verify the collection exists
    String solrUrl = "http://localhost:" + bindPort + "/solr";
    String collectionListUrl = solrUrl + "/admin/collections?action=list";
    if (!SolrCLI.safeCheckCollectionExists(collectionListUrl, collectionName)) {
        fail("After running Solr cloud example, test collection '" + collectionName + "' not found in Solr at: " + solrUrl + "; tool output: " + toolOutput);
    }
    // index some docs - to verify all is good for both shards
    CloudSolrClient cloudClient = null;
    try {
        cloudClient = getCloudSolrClient(executor.solrCloudCluster.getZkServer().getZkAddress());
        cloudClient.connect();
        cloudClient.setDefaultCollection(collectionName);
        int numDocs = 10;
        for (int d = 0; d < numDocs; d++) {
            SolrInputDocument doc = new SolrInputDocument();
            doc.setField("id", "doc" + d);
            doc.setField("str_s", "a");
            cloudClient.add(doc);
        }
        cloudClient.commit();
        QueryResponse qr = cloudClient.query(new SolrQuery("str_s:a"));
        if (qr.getResults().getNumFound() != numDocs) {
            fail("Expected " + numDocs + " to be found in the " + collectionName + " collection but only found " + qr.getResults().getNumFound());
        }
    } finally {
        if (cloudClient != null) {
            try {
                cloudClient.close();
            } catch (Exception ignore) {
            }
        }
    }
    File node1SolrHome = new File(solrExampleDir, "cloud/node1/solr");
    if (!node1SolrHome.isDirectory()) {
        fail(node1SolrHome.getAbsolutePath() + " not found! run cloud example failed; tool output: " + toolOutput);
    }
    // delete the collection
    SolrCLI.DeleteTool deleteTool = new SolrCLI.DeleteTool(stdoutSim);
    String[] deleteArgs = new String[] { "-name", collectionName, "-solrUrl", solrUrl };
    deleteTool.runTool(SolrCLI.processCommandLineArgs(SolrCLI.joinCommonAndToolOptions(deleteTool.getOptions()), deleteArgs));
    // dump all the output written by the SolrCLI commands to stdout
    //System.out.println(toolOutput);
    // stop the test instance
    executor.execute(org.apache.commons.exec.CommandLine.parse("bin/solr stop -p " + bindPort));
}
Also used : Path(java.nio.file.Path) PrintStream(java.io.PrintStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ServerSocket(java.net.ServerSocket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) SolrInputDocument(org.apache.solr.common.SolrInputDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) File(java.io.File) Test(org.junit.Test)

Example 77 with CloudSolrClient

use of org.apache.solr.client.solrj.impl.CloudSolrClient in project lucene-solr by apache.

the class BlobRepositoryCloudTest method getSolrDocuments.

private SolrDocumentList getSolrDocuments(String collection) throws SolrServerException, IOException {
    SolrQuery query = new SolrQuery("*:*");
    CloudSolrClient client = cluster.getSolrClient();
    QueryResponse resp1 = client.query(collection, query);
    return resp1.getResults();
}
Also used : QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrQuery(org.apache.solr.client.solrj.SolrQuery) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient)

Example 78 with CloudSolrClient

use of org.apache.solr.client.solrj.impl.CloudSolrClient in project lucene-solr by apache.

the class TestTlogReplica method getSolrRunner.

private List<JettySolrRunner> getSolrRunner(boolean isLeader) {
    List<JettySolrRunner> rs = new ArrayList<>();
    CloudSolrClient cloudClient = cluster.getSolrClient();
    DocCollection docCollection = cloudClient.getZkStateReader().getClusterState().getCollection(collectionName);
    for (JettySolrRunner solrRunner : cluster.getJettySolrRunners()) {
        if (solrRunner.getCoreContainer() == null)
            continue;
        for (SolrCore solrCore : solrRunner.getCoreContainer().getCores()) {
            CloudDescriptor cloudDescriptor = solrCore.getCoreDescriptor().getCloudDescriptor();
            Slice slice = docCollection.getSlice(cloudDescriptor.getShardId());
            Replica replica = docCollection.getReplica(cloudDescriptor.getCoreNodeName());
            if (slice.getLeader() == replica && isLeader) {
                rs.add(solrRunner);
            } else if (slice.getLeader() != replica && !isLeader) {
                rs.add(solrRunner);
            }
        }
    }
    return rs;
}
Also used : JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) SolrCore(org.apache.solr.core.SolrCore) Slice(org.apache.solr.common.cloud.Slice) ArrayList(java.util.ArrayList) DocCollection(org.apache.solr.common.cloud.DocCollection) Replica(org.apache.solr.common.cloud.Replica) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient)

Example 79 with CloudSolrClient

use of org.apache.solr.client.solrj.impl.CloudSolrClient in project lucene-solr by apache.

the class TestTlogReplica method getSolrCore.

private List<SolrCore> getSolrCore(boolean isLeader) {
    List<SolrCore> rs = new ArrayList<>();
    CloudSolrClient cloudClient = cluster.getSolrClient();
    DocCollection docCollection = cloudClient.getZkStateReader().getClusterState().getCollection(collectionName);
    for (JettySolrRunner solrRunner : cluster.getJettySolrRunners()) {
        if (solrRunner.getCoreContainer() == null)
            continue;
        for (SolrCore solrCore : solrRunner.getCoreContainer().getCores()) {
            CloudDescriptor cloudDescriptor = solrCore.getCoreDescriptor().getCloudDescriptor();
            Slice slice = docCollection.getSlice(cloudDescriptor.getShardId());
            Replica replica = docCollection.getReplica(cloudDescriptor.getCoreNodeName());
            if (slice.getLeader().equals(replica) && isLeader) {
                rs.add(solrCore);
            } else if (!slice.getLeader().equals(replica) && !isLeader) {
                rs.add(solrCore);
            }
        }
    }
    return rs;
}
Also used : SolrCore(org.apache.solr.core.SolrCore) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) Slice(org.apache.solr.common.cloud.Slice) ArrayList(java.util.ArrayList) DocCollection(org.apache.solr.common.cloud.DocCollection) Replica(org.apache.solr.common.cloud.Replica) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient)

Example 80 with CloudSolrClient

use of org.apache.solr.client.solrj.impl.CloudSolrClient in project lucene-solr by apache.

the class CloudMLTQParserTest method setupCluster.

@BeforeClass
public static void setupCluster() throws Exception {
    configureCluster(2).addConfig("conf", configset("cloud-dynamic")).configure();
    final CloudSolrClient client = cluster.getSolrClient();
    CollectionAdminRequest.createCollection(COLLECTION, "conf", 2, 1).processAndWait(client, DEFAULT_TIMEOUT);
    client.waitForState(COLLECTION, DEFAULT_TIMEOUT, TimeUnit.SECONDS, (n, c) -> DocCollection.isFullyActive(n, c, 2, 1));
    String id = "id";
    String FIELD1 = "lowerfilt_u";
    String FIELD2 = "lowerfilt1_u";
    new UpdateRequest().add(sdoc(id, "1", FIELD1, "toyota")).add(sdoc(id, "2", FIELD1, "chevrolet")).add(sdoc(id, "3", FIELD1, "bmw usa")).add(sdoc(id, "4", FIELD1, "ford")).add(sdoc(id, "5", FIELD1, "ferrari")).add(sdoc(id, "6", FIELD1, "jaguar")).add(sdoc(id, "7", FIELD1, "mclaren moon or the moon and moon moon shine and the moon but moon was good foxes too")).add(sdoc(id, "8", FIELD1, "sonata")).add(sdoc(id, "9", FIELD1, "The quick red fox jumped over the lazy big and large brown dogs.")).add(sdoc(id, "10", FIELD1, "blue")).add(sdoc(id, "12", FIELD1, "glue")).add(sdoc(id, "13", FIELD1, "The quote red fox jumped over the lazy brown dogs.")).add(sdoc(id, "14", FIELD1, "The quote red fox jumped over the lazy brown dogs.")).add(sdoc(id, "15", FIELD1, "The fat red fox jumped over the lazy brown dogs.")).add(sdoc(id, "16", FIELD1, "The slim red fox jumped over the lazy brown dogs.")).add(sdoc(id, "17", FIELD1, "The quote red fox jumped moon over the lazy brown dogs moon. Of course moon. Foxes and moon come back to the foxes and moon")).add(sdoc(id, "18", FIELD1, "The quote red fox jumped over the lazy brown dogs.")).add(sdoc(id, "19", FIELD1, "The hose red fox jumped over the lazy brown dogs.")).add(sdoc(id, "20", FIELD1, "The quote red fox jumped over the lazy brown dogs.")).add(sdoc(id, "21", FIELD1, "The court red fox jumped over the lazy brown dogs.")).add(sdoc(id, "22", FIELD1, "The quote red fox jumped over the lazy brown dogs.")).add(sdoc(id, "23", FIELD1, "The quote red fox jumped over the lazy brown dogs.")).add(sdoc(id, "24", FIELD1, "The file red fox jumped over the lazy brown dogs.")).add(sdoc(id, "25", FIELD1, "rod fix")).add(sdoc(id, "26", FIELD1, "bmw usa 328i")).add(sdoc(id, "27", FIELD1, "bmw usa 535i")).add(sdoc(id, "28", FIELD1, "bmw 750Li")).add(sdoc(id, "29", FIELD1, "bmw usa", FIELD2, "red green blue")).add(sdoc(id, "30", FIELD1, "The quote red fox jumped over the lazy brown dogs.", FIELD2, "red green yellow")).add(sdoc(id, "31", FIELD1, "The fat red fox jumped over the lazy brown dogs.", FIELD2, "green blue yellow")).add(sdoc(id, "32", FIELD1, "The slim red fox jumped over the lazy brown dogs.", FIELD2, "yellow white black")).commit(client, COLLECTION);
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) BeforeClass(org.junit.BeforeClass)

Aggregations

CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)140 Test (org.junit.Test)52 ArrayList (java.util.ArrayList)40 SolrQuery (org.apache.solr.client.solrj.SolrQuery)30 HashMap (java.util.HashMap)26 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)25 SolrInputDocument (org.apache.solr.common.SolrInputDocument)25 CollectionAdminRequest (org.apache.solr.client.solrj.request.CollectionAdminRequest)24 Slice (org.apache.solr.common.cloud.Slice)24 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)22 List (java.util.List)21 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)21 Map (java.util.Map)20 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)20 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)19 NamedList (org.apache.solr.common.util.NamedList)18 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)17 Replica (org.apache.solr.common.cloud.Replica)17 SolrRequest (org.apache.solr.client.solrj.SolrRequest)15 DocCollection (org.apache.solr.common.cloud.DocCollection)15