Example usage for org.apache.solr.search SolrIndexSearcher close

List of usage examples for org.apache.solr.search SolrIndexSearcher close

Introduction

In this page you can find the example usage for org.apache.solr.search SolrIndexSearcher close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Free's resources associated with this searcher.

Usage

From source file:com.lucid.solr.sidecar.SidecarIndexReaderFactory.java

License:Apache License

DirectoryReader newReaderInternal(Directory indexDir, IndexWriter writer, SolrCore core) throws IOException {
    DirectoryReader main = null;//from   ww  w.  j  a va2s .c  o  m
    if (writer != null) {
        main = standardFactory.newReader(writer, core);
    } else {
        main = standardFactory.newReader(indexDir, core);
    }
    if (!enabled) {
        LOG.info("Sidecar index not enabled");
        return main;
    }
    currentCore = core;
    CoreContainer container = core.getCoreDescriptor().getCoreContainer();
    SolrCore source = container.getCore(sourceCollection);
    if (source == null) {
        LOG.info("Source collection '" + sourceCollection + "' not present, sidecar index is disabled.");
        try {
            return new SidecarIndexReader(this, main, null, SidecarIndexReader.getSequentialSubReaders(main),
                    sourceCollection, null);
        } catch (Exception e1) {
            LOG.warn("Unexpected exception, returning single main index", e1);
            return main;
        }
    }
    if (source.isClosed()) {
        LOG.info("Source collection '" + sourceCollection + "' is closed, sidecar index is disabled.");
        try {
            return new SidecarIndexReader(this, main, null, SidecarIndexReader.getSequentialSubReaders(main),
                    sourceCollection, null);
        } catch (Exception e1) {
            LOG.warn("Unexpected exception, returning single main index", e1);
            return main;
        }
    }
    DirectoryReader parallel = null;
    SolrIndexSearcher searcher = null;
    try {
        searcher = source.getNewestSearcher(true).get();
        parallel = buildParallelReader(main, searcher, true);
    } finally {
        if (searcher != null) {
            LOG.info("-- closing " + searcher);
            searcher.close();
        }
        source.close();
    }
    return parallel;
}

From source file:com.lucid.solr.sidecar.SidecarIndexReaderFactory.java

License:Apache License

DirectoryReader reopen(DirectoryReader newMain, boolean rebuild) throws IOException {
    CoreContainer container = currentCore.getCoreDescriptor().getCoreContainer();
    SolrCore source = container.getCore(sourceCollection);
    if (source == null) {
        LOG.info("Source collection '" + sourceCollection + "' not present, sidecar index is disabled.");
        try {//www.  j a  v  a 2s  . co  m
            return new SidecarIndexReader(this, newMain, null,
                    SidecarIndexReader.getSequentialSubReaders(newMain), sourceCollection, null);
        } catch (Exception e1) {
            LOG.warn("Unexpected exception, returning single main index", e1);
            return newMain;
        }
    }
    if (source.isClosed()) {
        LOG.info("Source collection '" + sourceCollection + "' is closed, sidecar index is disabled.");
        try {
            return new SidecarIndexReader(this, newMain, null,
                    SidecarIndexReader.getSequentialSubReaders(newMain), sourceCollection, null);
        } catch (Exception e1) {
            LOG.warn("Unexpected exception, returning single main index", e1);
            return newMain;
        }
    }
    DirectoryReader parallel = null;
    SolrIndexSearcher searcher = null;
    try {
        searcher = source.getNewestSearcher(true).get();
        parallel = buildParallelReader(newMain, searcher, rebuild);
    } finally {
        if (searcher != null && searcher.getIndexReader().getRefCount() > 0) {
            LOG.info("-- closing " + searcher);
            searcher.close();
        }
        if (source != null) {
            source.close();
        }
    }
    return parallel;
}

From source file:com.lucid.solr.sidecar.SidecarIndexReaderFactoryTest.java

License:Apache License

@Test
public void testBasics() throws Exception {
    System.err.println(cc.getAllCoreNames());
    populate();/*from  ww w . j  a  v  a2 s  .  c  om*/
    SolrCore target = cc.getCore("target");
    SolrIndexSearcher searcher = target.getSearcher().get();
    Query q = new MatchAllDocsQuery();
    try {
        // verify the stored parts
        TopDocs td = searcher.search(q, 101);
        assertNotNull(td);
        for (ScoreDoc sd : td.scoreDocs) {
            Document doc = searcher.doc(sd.doc);
            String[] vals = doc.getValues("id");
            assertNotNull(vals);
            assertEquals(1, vals.length);
            String id = vals[0];
            vals = doc.getValues("text");
            assertNotNull(vals);
            assertEquals(1, vals.length);
            if (!id.equals("id100")) {
                // should have also the sidecar fields
                vals = doc.getValues("side1_t");
                assertNotNull(vals);
                assertEquals(1, vals.length);
                vals = doc.getValues("side2_t");
                assertNotNull(vals);
                assertEquals(1, vals.length);
            } else {
                // should not have the sidecar fields
                vals = doc.getValues("side1_t");
                assertTrue(vals == null || vals.length == 0);
                vals = doc.getValues("side2_t");
                assertTrue(vals == null || vals.length == 0);
            }
        }
        // verify the inverted parts
        q = new TermQuery(new Term("side1_t", "foo"));
        td = searcher.search(q, 101);
        assertEquals(100, td.totalHits);
        q = new TermQuery(new Term("side2_t", "blah"));
        td = searcher.search(q, 101);
        assertEquals(100, td.totalHits);
    } finally {
        searcher.close();
        target.close();
    }
}

From source file:com.lucid.solr.sidecar.SidecarIndexReaderFactoryTest.java

License:Apache License

@Test
public void testChanges() throws Exception {
    populate();//from w  w w.j av a  2s .  c o m
    // add some docs, overwriting some of the existing ones
    SolrCore target = cc.getCore("target");
    try {
        for (int i = 50; i < 150; i++) {
            AddUpdateCommand cmd = new AddUpdateCommand(makeReq(target));
            cmd.overwrite = true;
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("id", "id" + i);
            doc.addField("text", "new document " + i);
            cmd.solrDoc = doc;
            target.getUpdateHandler().addDoc(cmd);
        }
        target.getUpdateHandler().commit(new CommitUpdateCommand(makeReq(target), false));
    } finally {
        target.close();
    }
    target = cc.getCore("target");
    SolrIndexSearcher searcher = target.getSearcher().get();
    Query q = new MatchAllDocsQuery();
    try {
        // verify the stored parts
        TopDocs td = searcher.search(q, 151);
        assertNotNull(td);
        for (ScoreDoc sd : td.scoreDocs) {
            Document doc = searcher.doc(sd.doc);
            System.err.println(doc);
        }
    } finally {
        searcher.close();
        target.close();
    }
}

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();//from  www .j a  va  2 s.c om

    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());
    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();

}