Example usage for org.apache.solr.core SolrCore execute

List of usage examples for org.apache.solr.core SolrCore execute

Introduction

In this page you can find the example usage for org.apache.solr.core SolrCore execute.

Prototype

public void execute(SolrRequestHandler handler, SolrQueryRequest req, SolrQueryResponse rsp) 

Source Link

Usage

From source file:examples.adsabs.BlackBoxFailingRecords.java

License:Apache License

public void testImport() throws Exception {

    WaitingDataImportHandler handler = (WaitingDataImportHandler) h.getCore()
            .getRequestHandler("/invenio/import");
    SolrCore core = h.getCore();

    SolrQueryRequest req = req("command", "full-import", "commit", "true", "url",
            "python://search?p=" + URLEncoder.encode("recid:1->104", "UTF-8"));
    SolrQueryResponse rsp = new SolrQueryResponse();
    core.execute(handler, req, rsp);

    assertU(commit());/* w  ww .  j  a v  a 2s.  c  om*/
    assertQ(req("q", "*:*", "fl", "recid,title"), "//*[@numFound='22']");
    assertQ(req("q", "id:84"), "//*[@numFound='1']");
    assertQ(req("q", "id:78"), "//*[@numFound='1']");
    assertQ(req("q", "abstract:\"Hubbard-Stratonovich\""), "//*[@numFound='1']");

    // clean the slate
    assertU(delQ("*:*"));
    assertU(commit());
    assertQ(req("q", "*:*"), "//*[@numFound='0']");

    failThis.put("78", true);
    failThis.put("84", true);

    req = req("command", "full-import", "commit", "true", "writerImpl", TestFailingWriter.class.getName(),
            "url", "python://search?p=" + URLEncoder.encode("recid:1->60 OR recid:61->104", "UTF-8"));
    rsp = new SolrQueryResponse();
    core.execute(handler, req, rsp);

    assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='queueSize'][.='3']",
            "//str[@name='failedRecs'][.='0']", "//str[@name='failedBatches'][.='0']",
            "//str[@name='failedTotal'][.='0']", "//str[@name='registeredRequests'][.='3']",
            "//str[@name='restartedRequests'][.='0']", "//str[@name='docsToCheck'][.='103']",
            "//str[@name='status'][.='idle']");
    assertQ(req("qt", "/invenio-doctor", "command", "detailed-info"), "//str[@name='queueSize'][.='3']",
            "//str[@name='failedRecs'][.='0']", "//str[@name='failedBatches'][.='0']",
            "//str[@name='failedTotal'][.='0']", "//str[@name='registeredRequests'][.='3']",
            "//str[@name='restartedRequests'][.='0']", "//str[@name='docsToCheck'][.='103']",
            "//str[@name='status'][.='idle']", "*[count(//arr[@name='toBeDone']/str)=3]",
            "*[count(//arr[@name='failedBatches']/str)=0]");

    InvenioDoctor doctor = (InvenioDoctor) h.getCore().getRequestHandler("/invenio-doctor");
    req = req("command", "start");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='status'][.='busy']");
        Thread.sleep(300);
    }

    assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='status'][.='idle']");

    assertQ(req("q", "*:*"), "//*[@numFound='20']");
    assertQ(req("q", "id:84"), "//*[@numFound='0']");
    assertQ(req("q", "id:83"), "//*[@numFound='1']");
    assertQ(req("q", "id:85"), "//*[@numFound='1']");

    assertQ(req("q", "id:78"), "//*[@numFound='0']");
    assertQ(req("q", "id:77"), "//*[@numFound='1']");
    assertQ(req("q", "id:79"), "//*[@numFound='1']");

    String response = h.query("/invenio-doctor", req("qt", "/invenio-doctor", "command", "detailed-info"));

    //System.out.println(response);

    assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='queueSize'][.='0']",
            "//str[@name='failedRecs'][.='2']", "//str[@name='failedBatches'][.='0']",
            "//str[@name='failedTotal'][.='2']", "//str[@name='registeredRequests'][.='21']",
            "//str[@name='restartedRequests'][.='21']", "//str[@name='docsToCheck'][.='0']",
            "//str[@name='status'][.='idle']");

    assertQ(req("qt", "/invenio-doctor", "command", "reset"));
    assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='queueSize'][.='0']",
            "//str[@name='failedRecs'][.='0']");

    // now we expect the least possible number of restarts
    req = req("command", "full-import", "commit", "true", "writerImpl", TestFailingWriter.class.getName(),
            "url", "python://search?p=" + URLEncoder.encode("recid:82->86", "UTF-8"));
    rsp = new SolrQueryResponse();
    core.execute(handler, req, rsp);

    assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='queueSize'][.='3']",
            "//str[@name='failedRecs'][.='0']", "//str[@name='failedBatches'][.='0']",
            "//str[@name='failedTotal'][.='0']", "//str[@name='registeredRequests'][.='3']",
            "//str[@name='restartedRequests'][.='0']", "//str[@name='docsToCheck'][.='3']",
            "//str[@name='status'][.='idle']");

    req = req("command", "start");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='queueSize'][.='0']",
            "//str[@name='failedRecs'][.='1']", "//str[@name='failedBatches'][.='0']",
            "//str[@name='failedTotal'][.='1']", "//str[@name='registeredRequests'][.='3']",
            "//str[@name='restartedRequests'][.='3']", "//str[@name='docsToCheck'][.='0']",
            "//str[@name='status'][.='idle']");

    // now test the new component which looks inside the invenio db
    // discovers the missing recs and calls indexing on them

    assertU(commit());
    assertQ(req("q", "recid:77 OR recid:80"), "//*[@numFound='2']");
    assertU(delQ("recid:77 OR recid:80"));

    // this is necessary, otherwise the results may be wrong because
    // lucene cache is used to compare records (and the cache may
    // contain deleted records even after the commit)
    assertU(commit("expungeDeletes", "true"));
    assertQ(req("q", "recid:77 OR recid:80"), "//*[@numFound='0']");

    assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='queueSize'][.='0']",
            "//str[@name='failedRecs'][.='1']", "//str[@name='failedBatches'][.='0']",
            "//str[@name='failedTotal'][.='1']", "//str[@name='registeredRequests'][.='3']",
            "//str[@name='restartedRequests'][.='3']", "//str[@name='docsToCheck'][.='0']",
            "//str[@name='status'][.='idle']");

    req = req("command", "discover", "params", "batchSize=50");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    req = req("command", "start");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    assertU(commit());

    assertQ(req("q", "recid:77 OR recid:80"), "//*[@numFound='2']");

    assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='queueSize'][.='0']",
            "//str[@name='failedRecs'][.='1']", "//str[@name='failedBatches'][.='0']",
            "//str[@name='failedTotal'][.='1']", "//str[@name='registeredRequests'][.='7']",
            "//str[@name='restartedRequests'][.='7']", "//str[@name='docsToCheck'][.='0']",
            "//str[@name='status'][.='idle']");

    assertQ(req("qt", "/invenio-doctor", "command", "show-missing"), "//str[@name='queueSize'][.='0']",
            "//str[@name='failedRecs'][.='1']", "//str[@name='failedBatches'][.='0']",
            "//str[@name='failedTotal'][.='1']", "//str[@name='registeredRequests'][.='7']",
            "//str[@name='restartedRequests'][.='7']", "//str[@name='docsToCheck'][.='0']",
            "//str[@name='status'][.='idle']", "//arr[@name='missingRecs']/int[.='77']",
            "//arr[@name='missingRecs']/int[.='80']");

    assertQ(req("qt", "/invenio-doctor", "command", "reset"), null);
    assertQ(req("qt", "/invenio-doctor", "command", "discover", "params",
            "batchSize=1&fetch_size=2&max_records=1"), null);
    req = req("command", "start");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    assertU(commit());
    assertQ(req("q", "recid:77 OR recid:80"), "//*[@numFound='2']");

    /*
    assertQ(req("qt", "/invenio-doctor", "command", "detailed-info"), 
      "//str[@name='registeredRequests'][.='9']",
      "//str[@name='restartedRequests'][.='9']",
      "//str[@name='status'][.='idle']"
    );
     */

    // verify that deleted recs are discovered
    //MontySolrVM.INSTANCE.evalCommand("sys.stderr.write(str(self._handler._db['*:create_record'].__module__))");
    //MontySolrVM.INSTANCE.evalCommand("sys.stderr.write(sys.modules['monty_invenio.tests.demotest_updating'].__file__ + '\\n')");
    PythonMessage message = MontySolrVM.INSTANCE.createMessage("create_record").setParam("diff", 5)
            .setParam("data", "970:a:bibcodeXXXXXXXXXXXX");
    MontySolrVM.INSTANCE.sendMessage(message);
    Integer added = (Integer) message.getResults();
    tempRecids.add(added);

    assertQ(req("qt", "/invenio-doctor", "command", "discover"), null);
    req = req("command", "start");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);
    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    assertU(commit());
    assertQ(req("q", "recid:" + added), "//*[@numFound='1']");

    message = MontySolrVM.INSTANCE.createMessage("delete_record").setParam("recid", added).setParam("diff", 7);
    MontySolrVM.INSTANCE.sendMessage(message);

    assertQ(req("qt", "/invenio-doctor", "command", "discover"), null);
    req = req("command", "start");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);
    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    assertU(commit());
    assertQ(req("q", "recid:" + added), "//*[@numFound='0']");

    // now delete records inside solr and see whether the doctor can
    // discover them and recover them

    assertU(delQ("*:*"));
    assertU(commit());
    assertQ(req("q", "*:*"), "//*[@numFound='0']");

    failThis.clear();

    req = req("command", "discover");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    req = req("command", "start");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    assertU(commit());

    assertQ(req("q", "*:*"), "//*[@numFound='22']");

    // check that force-reindexing will update recs
    RefCounted<SolrIndexSearcher> searcher = h.getCore().getSearcher();
    SolrIndexSearcher s = searcher.get();
    Document doc77 = s.doc(s.search(new TermQuery(new Term("recid", "77")), 1).scoreDocs[0].doc);
    String is = doc77.get("indexstamp");

    req = req("command", "force-reindexing");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    req = req("command", "start");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        Thread.sleep(300);
    }
    assertU(commit());

    Document doc77b = s.doc(s.search(new TermQuery(new Term("recid", "77")), 1).scoreDocs[0].doc);
    String is2 = doc77b.get("indexstamp");

    assertQ(req("q", "*:*"), "//*[@numFound='22']");
    assertTrue("Docs were not re-indexed", !is.equals(is2));

    s.close();
    searcher.decref();

}

From source file:org.restlet.ext.lucene.SolrClientHelper.java

License:Open Source License

@Override
public void handle(Request request, Response response) {
    super.handle(request, response);

    Reference resRef = request.getResourceRef();
    String path = resRef.getPath();

    if (path != null) {
        path = resRef.getPath(true);//from   w w  w  .j ava2s .  c o  m
    }

    String coreName = request.getResourceRef().getHostDomain();

    if (coreName == null || "".equals(coreName)) {
        coreName = getContext().getParameters().getFirstValue("DefaultCore");
    }

    SolrCore core = coreContainer.getCore(coreName);

    if (core == null) {
        response.setStatus(Status.SERVER_ERROR_INTERNAL, "No such core: " + coreName);
        return;
    }

    // Extract the handler from the path or params
    SolrRequestHandler handler = core.getRequestHandler(path);

    if (handler == null) {
        if ("/select".equals(path) || "/select/".equalsIgnoreCase(path)) {
            String qt = request.getResourceRef().getQueryAsForm().getFirstValue(CommonParams.QT);
            handler = core.getRequestHandler(qt);
            if (handler == null) {
                response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "unknown handler: " + qt);
                return;
            }
        }
        // Perhaps the path is to manage the cores
        if (handler == null && coreContainer != null && path.equals(coreContainer.getAdminPath())) {
            handler = coreContainer.getMultiCoreHandler();
        }
    }

    if (handler == null) {
        core.close();
        response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "unknown handler: " + path);
        return;
    }

    try {
        SolrQueryRequest solrReq = new SolrRestletQueryRequest(request, core);
        SolrQueryResponse solrResp = new SolrQueryResponse();
        core.execute(handler, solrReq, solrResp);

        if (solrResp.getException() != null) {
            response.setStatus(Status.SERVER_ERROR_INTERNAL, solrResp.getException());
        } else {
            response.setEntity(new SolrRepresentation(MediaType.APPLICATION_XML, solrReq, solrResp));
            response.setStatus(Status.SUCCESS_OK);
        }
    } catch (Exception e) {
        getLogger().log(Level.WARNING, "Unable to evaluate " + resRef.toString(), e);
        response.setStatus(Status.SERVER_ERROR_INTERNAL, e);
    } finally {
        core.close();
    }
}

From source file:org.restlet.ext.solr.SolrClientHelper.java

License:LGPL

@Override
public void handle(Request request, Response response) {
    super.handle(request, response);

    Reference resRef = request.getResourceRef();
    String path = resRef.getPath();

    if (path != null) {
        path = resRef.getPath(true);//  ww  w  . j a va  2  s .c  o m
    }

    String coreName = request.getResourceRef().getHostDomain();

    if (coreName == null || "".equals(coreName)) {
        coreName = getContext().getParameters().getFirstValue("DefaultCore");
    }

    SolrCore core = coreContainer.getCore(coreName);

    if (core == null) {
        response.setStatus(Status.SERVER_ERROR_INTERNAL, "No such core: " + coreName);
        return;
    }

    // Extract the handler from the path or params
    SolrRequestHandler handler = core.getRequestHandler(path);

    if (handler == null) {
        if ("/select".equals(path) || "/select/".equalsIgnoreCase(path)) {
            String qt = request.getResourceRef().getQueryAsForm().getFirstValue(CommonParams.QT);
            handler = core.getRequestHandler(qt);
            if (handler == null) {
                response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "unknown handler: " + qt);
                return;
            }
        }
        // Perhaps the path is to manage the cores
        if (handler == null && coreContainer != null && path.equals(coreContainer.getAdminPath())) {
            handler = coreContainer.getMultiCoreHandler();
        }
    }

    if (handler == null) {
        core.close();
        response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "unknown handler: " + path);
        return;
    }

    try {
        SolrQueryRequest solrReq = new SolrRestletQueryRequest(request, core);

        SolrQueryResponse solrResp = new SolrQueryResponse();
        core.execute(handler, solrReq, solrResp);

        if (solrResp.getException() != null) {
            response.setStatus(Status.SERVER_ERROR_INTERNAL, solrResp.getException());
        } else {
            response.setEntity(new SolrRepresentation(solrReq, solrResp, core));
            response.setStatus(Status.SUCCESS_OK);
        }
    } catch (Exception e) {
        getLogger().log(Level.WARNING, "Unable to evaluate " + resRef.toString(), e);
        response.setStatus(Status.SERVER_ERROR_INTERNAL, e);
    } finally {
        core.close();
    }
}