Search in sources :

Example 1 with HttpSolrClient

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

the class TestContentStreamDataSource method testSimple.

@Test
public void testSimple() throws Exception {
    DirectXmlRequest req = new DirectXmlRequest("/dataimport", xml);
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("command", "full-import");
    params.set("clean", "false");
    req.setParams(params);
    try (HttpSolrClient solrClient = getHttpSolrClient(buildUrl(jetty.getLocalPort(), "/solr/collection1"))) {
        solrClient.request(req);
        ModifiableSolrParams qparams = new ModifiableSolrParams();
        qparams.add("q", "*:*");
        QueryResponse qres = solrClient.query(qparams);
        SolrDocumentList results = qres.getResults();
        assertEquals(2, results.getNumFound());
        SolrDocument doc = results.get(0);
        assertEquals("1", doc.getFieldValue("id"));
        assertEquals("Hello C1", ((List) doc.getFieldValue("desc")).get(0));
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) DirectXmlRequest(org.apache.solr.client.solrj.request.DirectXmlRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) Test(org.junit.Test)

Example 2 with HttpSolrClient

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

the class ServerSnitchContext method invoke.

public SimpleSolrResponse invoke(UpdateShardHandler shardHandler, final String url, String path, SolrParams params) throws IOException, SolrServerException {
    GenericSolrRequest request = new GenericSolrRequest(SolrRequest.METHOD.GET, path, params);
    try (HttpSolrClient client = new HttpSolrClient.Builder(url).withHttpClient(shardHandler.getHttpClient()).withResponseParser(new BinaryResponseParser()).build()) {
        NamedList<Object> rsp = client.request(request);
        request.response.nl = rsp;
        return request.response;
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) GenericSolrRequest(org.apache.solr.client.solrj.request.GenericSolrRequest) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser)

Example 3 with HttpSolrClient

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

the class IndexFetcher method getLatestVersion.

/**
   * Gets the latest commit version and generation from the master
   */
@SuppressWarnings("unchecked")
NamedList getLatestVersion() throws IOException {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(COMMAND, CMD_INDEX_VERSION);
    params.set(CommonParams.WT, JAVABIN);
    params.set(CommonParams.QT, ReplicationHandler.PATH);
    QueryRequest req = new QueryRequest(params);
    // TODO modify to use shardhandler
    try (HttpSolrClient client = new Builder(masterUrl).withHttpClient(myHttpClient).build()) {
        client.setSoTimeout(soTimeout);
        client.setConnectionTimeout(connTimeout);
        return client.request(req);
    } catch (SolrServerException e) {
        throw new SolrException(ErrorCode.SERVER_ERROR, e.getMessage(), e);
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) Builder(org.apache.solr.client.solrj.impl.HttpSolrClient.Builder) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrException(org.apache.solr.common.SolrException)

Example 4 with HttpSolrClient

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

the class AbstractCloudBackupRestoreTestCase method getShardToDocCountMap.

private Map<String, Integer> getShardToDocCountMap(CloudSolrClient client, DocCollection docCollection) throws SolrServerException, IOException {
    Map<String, Integer> shardToDocCount = new TreeMap<>();
    for (Slice slice : docCollection.getActiveSlices()) {
        String shardName = slice.getName();
        try (HttpSolrClient leaderClient = new HttpSolrClient.Builder(slice.getLeader().getCoreUrl()).withHttpClient(client.getHttpClient()).build()) {
            long docsInShard = leaderClient.query(new SolrQuery("*:*").setParam("distrib", "false")).getResults().getNumFound();
            shardToDocCount.put(shardName, (int) docsInShard);
        }
    }
    return shardToDocCount;
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) Slice(org.apache.solr.common.cloud.Slice) TreeMap(java.util.TreeMap) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 5 with HttpSolrClient

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

the class BasicDistributedZk2Test method brindDownShardIndexSomeDocsAndRecover.

private void brindDownShardIndexSomeDocsAndRecover() throws Exception {
    SolrQuery query = new SolrQuery("*:*");
    query.set("distrib", false);
    commit();
    long deadShardCount = shardToJetty.get(SHARD2).get(0).client.solrClient.query(query).getResults().getNumFound();
    query("q", "*:*", "sort", "n_tl1 desc");
    int oldLiveNodes = cloudClient.getZkStateReader().getZkClient().getChildren(ZkStateReader.LIVE_NODES_ZKNODE, null, true).size();
    assertEquals(5, oldLiveNodes);
    // kill a shard
    CloudJettyRunner deadShard = chaosMonkey.stopShard(SHARD1, 0);
    // ensure shard is dead
    try {
        index_specific(deadShard.client.solrClient, id, 999, i1, 107, t1, "specific doc!");
        fail("This server should be down and this update should have failed");
    } catch (SolrServerException e) {
    // expected..
    }
    commit();
    query("q", "*:*", "sort", "n_tl1 desc");
    // long cloudClientDocs = cloudClient.query(new
    // SolrQuery("*:*")).getResults().getNumFound();
    // System.out.println("clouddocs:" + cloudClientDocs);
    // try to index to a living shard at shard2
    long numFound1 = cloudClient.query(new SolrQuery("*:*")).getResults().getNumFound();
    cloudClient.getZkStateReader().getLeaderRetry(DEFAULT_COLLECTION, SHARD1, 60000);
    index_specific(shardToJetty.get(SHARD1).get(1).client.solrClient, id, 1000, i1, 108, t1, "specific doc!");
    commit();
    checkShardConsistency(true, false);
    query("q", "*:*", "sort", "n_tl1 desc");
    cloudClient.setDefaultCollection(DEFAULT_COLLECTION);
    long numFound2 = cloudClient.query(new SolrQuery("*:*")).getResults().getNumFound();
    assertEquals(numFound1 + 1, numFound2);
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", 1001);
    controlClient.add(doc);
    // try adding a doc with CloudSolrServer
    UpdateRequest ureq = new UpdateRequest();
    ureq.add(doc);
    try {
        ureq.process(cloudClient);
    } catch (SolrServerException e) {
        // try again
        Thread.sleep(3500);
        ureq.process(cloudClient);
    }
    commit();
    query("q", "*:*", "sort", "n_tl1 desc");
    long numFound3 = cloudClient.query(new SolrQuery("*:*")).getResults().getNumFound();
    // lets just check that the one doc since last commit made it in...
    assertEquals(numFound2 + 1, numFound3);
    // test debugging
    testDebugQueries();
    if (VERBOSE) {
        System.err.println(controlClient.query(new SolrQuery("*:*")).getResults().getNumFound());
        for (SolrClient client : clients) {
            try {
                SolrQuery q = new SolrQuery("*:*");
                q.set("distrib", false);
                System.err.println(client.query(q).getResults().getNumFound());
            } catch (Exception e) {
            }
        }
    }
    // TODO: This test currently fails because debug info is obtained only
    // on shards with matches.
    // query("q","matchesnothing","fl","*,score", "debugQuery", "true");
    // this should trigger a recovery phase on deadShard
    ChaosMonkey.start(deadShard.jetty);
    // make sure we have published we are recovering
    Thread.sleep(1500);
    waitForRecoveriesToFinish(false);
    deadShardCount = shardToJetty.get(SHARD1).get(0).client.solrClient.query(query).getResults().getNumFound();
    // if we properly recovered, we should now have the couple missing docs that
    // came in while shard was down
    checkShardConsistency(true, false);
    // recover over 100 docs so we do more than just peer sync (replicate recovery)
    chaosMonkey.stopJetty(deadShard);
    for (int i = 0; i < 226; i++) {
        doc = new SolrInputDocument();
        doc.addField("id", 2000 + i);
        controlClient.add(doc);
        ureq = new UpdateRequest();
        ureq.add(doc);
        // ureq.setParam("update.chain", DISTRIB_UPDATE_CHAIN);
        ureq.process(cloudClient);
    }
    commit();
    Thread.sleep(1500);
    ChaosMonkey.start(deadShard.jetty);
    // make sure we have published we are recovering
    Thread.sleep(1500);
    waitForThingsToLevelOut(60);
    Thread.sleep(500);
    waitForRecoveriesToFinish(false);
    checkShardConsistency(true, false);
    // try a backup command
    try (final HttpSolrClient client = getHttpSolrClient((String) shardToJetty.get(SHARD2).get(0).info.get("base_url"))) {
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("qt", ReplicationHandler.PATH);
        params.set("command", "backup");
        Path location = createTempDir();
        location = FilterPath.unwrap(location).toRealPath();
        params.set("location", location.toString());
        QueryRequest request = new QueryRequest(params);
        client.request(request, DEFAULT_TEST_COLLECTION_NAME);
        checkForBackupSuccess(client, location);
        client.close();
    }
}
Also used : FilterPath(org.apache.lucene.mockfile.FilterPath) Path(java.nio.file.Path) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrClient(org.apache.solr.client.solrj.SolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient)

Aggregations

HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)211 SolrClient (org.apache.solr.client.solrj.SolrClient)53 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)50 Test (org.junit.Test)46 ArrayList (java.util.ArrayList)40 SolrQuery (org.apache.solr.client.solrj.SolrQuery)38 Replica (org.apache.solr.common.cloud.Replica)35 SolrInputDocument (org.apache.solr.common.SolrInputDocument)33 IOException (java.io.IOException)31 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)26 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)25 Slice (org.apache.solr.common.cloud.Slice)25 SolrServerException (org.apache.solr.client.solrj.SolrServerException)24 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)24 SolrException (org.apache.solr.common.SolrException)23 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)20 NamedList (org.apache.solr.common.util.NamedList)19 DocCollection (org.apache.solr.common.cloud.DocCollection)18 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)16 ZkCoreNodeProps (org.apache.solr.common.cloud.ZkCoreNodeProps)15