Search in sources :

Example 6 with QueryRequest

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

the class CdcrReplicatorManager method sendCdcrCommand.

private NamedList sendCdcrCommand(SolrClient client, CdcrParams.CdcrAction action, String... params) throws SolrServerException, IOException {
    ModifiableSolrParams solrParams = new ModifiableSolrParams();
    solrParams.set(CommonParams.QT, "/cdcr");
    solrParams.set(CommonParams.ACTION, action.toString());
    for (int i = 0; i < params.length - 1; i += 2) {
        solrParams.set(params[i], params[i + 1]);
    }
    SolrRequest request = new QueryRequest(solrParams);
    return client.request(request);
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrRequest(org.apache.solr.client.solrj.SolrRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 7 with QueryRequest

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

the class SubQueryAugmenter method transform.

@Override
public void transform(SolrDocument doc, int docid, float score) {
    final SolrParams docWithDeprefixed = SolrParams.wrapDefaults(new DocRowParams(doc, prefix, separator), baseSubParams);
    try {
        Callable<QueryResponse> subQuery = new Callable<QueryResponse>() {

            @Override
            public QueryResponse call() throws Exception {
                try {
                    return new QueryResponse(server.request(new QueryRequest(docWithDeprefixed), coreName), server);
                } finally {
                }
            }
        };
        QueryResponse response = SolrRequestInfoSuspender.doInSuspension(subQuery);
        final SolrDocumentList docList = (SolrDocumentList) response.getResults();
        doc.setField(getName(), new Result(docList));
    } catch (Exception e) {
        String docString = doc.toString();
        throw new SolrException(ErrorCode.BAD_REQUEST, "while invoking " + name + ":[subquery" + (coreName != null ? "fromIndex=" + coreName : "") + "] on doc=" + docString.substring(0, Math.min(100, docString.length())), e.getCause());
    } finally {
    }
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Callable(java.util.concurrent.Callable) SolrException(org.apache.solr.common.SolrException) SolrException(org.apache.solr.common.SolrException)

Example 8 with QueryRequest

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

the class HttpShardHandler method submit.

@Override
public void submit(final ShardRequest sreq, final String shard, final ModifiableSolrParams params) {
    // do this outside of the callable for thread safety reasons
    final List<String> urls = getURLs(shard);
    Callable<ShardResponse> task = () -> {
        ShardResponse srsp = new ShardResponse();
        if (sreq.nodeName != null) {
            srsp.setNodeName(sreq.nodeName);
        }
        srsp.setShardRequest(sreq);
        srsp.setShard(shard);
        SimpleSolrResponse ssr = new SimpleSolrResponse();
        srsp.setSolrResponse(ssr);
        long startTime = System.nanoTime();
        try {
            // use default (currently javabin)
            params.remove(CommonParams.WT);
            params.remove(CommonParams.VERSION);
            QueryRequest req = makeQueryRequest(sreq, params, shard);
            req.setMethod(SolrRequest.METHOD.POST);
            // if there are no shards available for a slice, urls.size()==0
            if (urls.size() == 0) {
                // all of the servers for a shard are down.
                throw new SolrException(SolrException.ErrorCode.SERVICE_UNAVAILABLE, "no servers hosting shard: " + shard);
            }
            if (urls.size() <= 1) {
                String url = urls.get(0);
                srsp.setShardAddress(url);
                try (SolrClient client = new Builder(url).withHttpClient(httpClient).build()) {
                    ssr.nl = client.request(req);
                }
            } else {
                LBHttpSolrClient.Rsp rsp = httpShardHandlerFactory.makeLoadBalancedRequest(req, urls);
                ssr.nl = rsp.getResponse();
                srsp.setShardAddress(rsp.getServer());
            }
        } catch (ConnectException cex) {
            //????
            srsp.setException(cex);
        } catch (Exception th) {
            srsp.setException(th);
            if (th instanceof SolrException) {
                srsp.setResponseCode(((SolrException) th).code());
            } else {
                srsp.setResponseCode(-1);
            }
        }
        ssr.elapsedTime = TimeUnit.MILLISECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
        return transfomResponse(sreq, srsp, shard);
    };
    try {
        if (shard != null) {
            MDC.put("ShardRequest.shards", shard);
        }
        if (urls != null && !urls.isEmpty()) {
            MDC.put("ShardRequest.urlList", urls.toString());
        }
        pending.add(completionService.submit(task));
    } finally {
        MDC.remove("ShardRequest.shards");
        MDC.remove("ShardRequest.urlList");
    }
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrClient(org.apache.solr.client.solrj.SolrClient) LBHttpSolrClient(org.apache.solr.client.solrj.impl.LBHttpSolrClient) Builder(org.apache.solr.client.solrj.impl.HttpSolrClient.Builder) SolrException(org.apache.solr.common.SolrException) SolrException(org.apache.solr.common.SolrException) ConnectException(java.net.ConnectException) ExecutionException(java.util.concurrent.ExecutionException) ConnectException(java.net.ConnectException)

Example 9 with QueryRequest

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

the class TestSubQueryTransformerDistrib method test.

@SuppressWarnings("serial")
@Test
public void test() throws SolrServerException, IOException {
    int peopleMultiplier = atLeast(1);
    int deptMultiplier = atLeast(1);
    createIndex(people, peopleMultiplier, depts, deptMultiplier);
    Random random1 = random();
    {
        final QueryRequest qr = new QueryRequest(params(new String[] { "q", "name_s:dave", "indent", "true", "fl", "*,depts:[subquery " + ((random1.nextBoolean() ? "" : "separator=,")) + "]", "rows", "" + peopleMultiplier, "depts.q", "{!terms f=dept_id_s v=$row.dept_ss_dv " + ((random1.nextBoolean() ? "" : "separator=,")) + "}", "depts.fl", "text_t" + (differentUniqueId ? ",id:notid" : ""), "depts.indent", "true", "depts.collection", "departments", differentUniqueId ? "depts.distrib.singlePass" : "notnecessary", "true", "depts.rows", "" + (deptMultiplier * 2), "depts.logParamsList", "q,fl,rows,row.dept_ss_dv", random().nextBoolean() ? "depts.wt" : "whatever", anyWt(), random().nextBoolean() ? "wt" : "whatever", anyWt() }));
        final QueryResponse rsp = new QueryResponse();
        rsp.setResponse(cluster.getSolrClient().request(qr, people));
        final SolrDocumentList hits = rsp.getResults();
        assertEquals(peopleMultiplier, hits.getNumFound());
        int engineerCount = 0;
        int supportCount = 0;
        for (int res : new int[] { 0, (peopleMultiplier - 1) / 2, peopleMultiplier - 1 }) {
            SolrDocument doc = hits.get(res);
            assertEquals("dave", doc.getFieldValue("name_s_dv"));
            SolrDocumentList relDepts = (SolrDocumentList) doc.getFieldValue("depts");
            assertEquals("dave works in both depts " + rsp, deptMultiplier * 2, relDepts.getNumFound());
            for (int deptN = 0; deptN < relDepts.getNumFound(); deptN++) {
                SolrDocument deptDoc = relDepts.get(deptN);
                String actual = (String) deptDoc.get("text_t");
                assertTrue(deptDoc + "should be either " + engineering + " or " + support, (engineering.equals(actual) && ++engineerCount > 0) || (support.equals(actual) && ++supportCount > 0));
            }
        }
        assertEquals(hits.toString(), engineerCount, supportCount);
    }
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) Random(java.util.Random) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Test(org.junit.Test)

Example 10 with QueryRequest

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

the class PeerSyncTest method assertSync.

void assertSync(SolrClient client, int numVersions, boolean expectedResult, String... syncWith) throws IOException, SolrServerException {
    QueryRequest qr = new QueryRequest(params("qt", "/get", "getVersions", Integer.toString(numVersions), "sync", StrUtils.join(Arrays.asList(syncWith), ',')));
    NamedList rsp = client.request(qr);
    assertEquals(expectedResult, (Boolean) rsp.get("sync"));
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) NamedList(org.apache.solr.common.util.NamedList)

Aggregations

QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)112 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)77 SolrRequest (org.apache.solr.client.solrj.SolrRequest)35 NamedList (org.apache.solr.common.util.NamedList)35 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)29 Test (org.junit.Test)28 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)21 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)17 Map (java.util.Map)17 SolrQuery (org.apache.solr.client.solrj.SolrQuery)17 IOException (java.io.IOException)16 SolrException (org.apache.solr.common.SolrException)15 SolrInputDocument (org.apache.solr.common.SolrInputDocument)14 SolrClient (org.apache.solr.client.solrj.SolrClient)13 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)12 List (java.util.List)11 SolrServerException (org.apache.solr.client.solrj.SolrServerException)8 RemoteSolrException (org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException)8 SolrDocumentList (org.apache.solr.common.SolrDocumentList)8