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

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

Introduction

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

Prototype

@Override
public Document doc(int docId) throws IOException 

Source Link

Document

Retrieve the Document instance corresponding to the document id.

Usage

From source file:com.doculibre.constellio.solr.handler.component.ConstellioAuthorizationComponent.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//w  w w.j av a 2s  . com
public void prepare(ResponseBuilder rb) throws IOException {
    SolrQueryRequest req = rb.req;
    SolrIndexSearcher searcher = req.getSearcher();
    //IndexReader reader = req.getSearcher().getReader();
    SolrParams params = req.getParams();

    // A runtime param can skip
    if (!params.getBool(ENABLE, true)) {
        return;
    }

    Query query = rb.getQuery();
    String qstr = rb.getQueryString();
    if (query == null || qstr == null) {
        return;
    }

    ConstellioUser user;
    String userIdStr = params.get(ConstellioSolrQueryParams.USER_ID);
    if (userIdStr != null) {
        UserServices userServices = ConstellioSpringUtils.getUserServices();
        try {
            user = userServices.get(new Long(userIdStr));
        } catch (NumberFormatException e) {
            user = null;
        }
    } else {
        user = null;
    }

    String collectionName = params.get(ConstellioSolrQueryParams.COLLECTION_NAME);
    RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices();
    FederationServices federationServices = ConstellioSpringUtils.getFederationServices();
    RecordCollection collection = collectionServices.get(collectionName);

    List<TermQuery> restrictedCollectionQueries = new ArrayList<TermQuery>();
    if (collection.isFederationOwner()) {
        List<RecordCollection> includedCollections = federationServices.listIncludedCollections(collection);
        for (RecordCollection includedCollection : includedCollections) {
            if (includedCollection.hasSearchPermission()
                    && (user == null || !user.hasSearchPermission(includedCollection))) {
                restrictedCollectionQueries.add(new TermQuery(
                        new Term(IndexField.COLLECTION_ID_FIELD, "" + includedCollection.getId())));
            }
        }
    }

    // User must be logged in to see private records
    if (user != null) {
        String luceneQueryStr = params.get(ConstellioSolrQueryParams.LUCENE_QUERY);
        if (StringUtils.isBlank(luceneQueryStr)) {
            return;
        }

        IndexSchema schema = req.getSchema();
        SolrQueryParser queryParser = new SolrQueryParser(rb.getQparser(), IndexField.DEFAULT_SEARCH_FIELD);
        Query luceneQuery;
        try {
            luceneQuery = queryParser.parse(luceneQueryStr);
        } catch (SyntaxError e) {
            log.error("Error parsing lucene query " + luceneQueryStr, e);
            return;
        }
        // Create a new query which will only include private records
        BooleanQuery privateRecordQuery = new BooleanQuery(true);
        privateRecordQuery.add(luceneQuery, BooleanClause.Occur.MUST);
        for (TermQuery restrictionCollectionQuery : restrictedCollectionQueries) {
            privateRecordQuery.add(restrictionCollectionQuery, BooleanClause.Occur.MUST_NOT);
        }

        TermQuery privateRecordTQ = new TermQuery(new Term(IndexField.PUBLIC_RECORD_FIELD, "F"));
        privateRecordQuery.add(privateRecordTQ, BooleanClause.Occur.MUST);

        DocSet privateRecordIdDocSet = searcher.getDocSet(privateRecordQuery);

        if (privateRecordIdDocSet.size() > 0) {
            RecordServices recordServices = ConstellioSpringUtils.getRecordServices();
            ACLServices aclServices = ConstellioSpringUtils.getACLServices();
            ConnectorManagerServices connectorManagerServices = ConstellioSpringUtils
                    .getConnectorManagerServices();

            List<Record> privateRecords = new ArrayList<Record>();
            DocIterator docIt = privateRecordIdDocSet.iterator();
            while (docIt.hasNext()) {
                int docId = docIt.nextDoc();
                Document luceneDoc = searcher.doc(docId);
                Long recordId = new Long(luceneDoc.get(IndexField.RECORD_ID_FIELD));
                Record record = recordServices.get(recordId, collection);
                privateRecords.add(record);
            }
            // First pass : Remove ACL authorized records
            List<Record> unevaluatedPrivateRecords = aclServices.removeAuthorizedRecords(privateRecords, user);
            if (!unevaluatedPrivateRecords.isEmpty()) {
                Set<UserCredentials> userCredentials = user.getUserCredentials();
                // Second pass : Ask the connector manager
                ConnectorManager connectorManager = connectorManagerServices.getDefaultConnectorManager();
                List<Record> authorizedRecords = connectorManagerServices
                        .authorizeByConnector(unevaluatedPrivateRecords, userCredentials, connectorManager);
                List<Record> unauthorizedRecords = ListUtils.removeAll(unevaluatedPrivateRecords,
                        authorizedRecords);

                if (!unauthorizedRecords.isEmpty()) {
                    // Create a new query which will exclude unauthorized records
                    BooleanQuery authorizedRecordQuery = new BooleanQuery(true);
                    authorizedRecordQuery.add(query, BooleanClause.Occur.MUST);
                    for (Record unauthorizedRecord : unauthorizedRecords) {
                        TermQuery unauthorizedRecordTQ = new TermQuery(
                                new Term(IndexField.RECORD_ID_FIELD, "" + unauthorizedRecord.getId()));
                        authorizedRecordQuery.add(unauthorizedRecordTQ, BooleanClause.Occur.MUST_NOT);
                    }
                    rb.setQuery(authorizedRecordQuery);
                }
            }
        }
    } else {
        BooleanQuery publicRecordQuery = new BooleanQuery(true);
        publicRecordQuery.add(query, BooleanClause.Occur.MUST);
        TermQuery publicRecordTQ = new TermQuery(new Term(IndexField.PUBLIC_RECORD_FIELD, "T"));
        publicRecordQuery.add(publicRecordTQ, BooleanClause.Occur.MUST);
        for (TermQuery restrictionCollectionQuery : restrictedCollectionQueries) {
            publicRecordQuery.add(restrictionCollectionQuery, BooleanClause.Occur.MUST_NOT);
        }
        rb.setQuery(publicRecordQuery);
    }
}

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

License:Apache License

@Test
public void testBasics() throws Exception {
    System.err.println(cc.getAllCoreNames());
    populate();//w w  w  .  j  a  v  a 2 s.  c o m
    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 a  va 2  s  .  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:com.searchbox.solr.SenseQueryHandler.java

private static NamedList<Explanation> getExplanations(Query query, DocList docs, SolrIndexSearcher searcher,
        IndexSchema schema) throws IOException {

    NamedList<Explanation> explainList = new SimpleOrderedMap<Explanation>();
    DocIterator iterator = docs.iterator();
    for (int i = 0; i < docs.size(); i++) {
        int id = iterator.nextDoc();

        Document doc = searcher.doc(id);
        String strid = schema.printableUniqueKey(doc);
        explainList.add(strid, searcher.explain(query, id));
    }//from   www  . j  a v  a 2s. c  o m
    return explainList;
}

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();/* ww  w.  jav a  2s .  com*/

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

}

From source file:in.geocoder.component.GeocodingComponent.java

License:Apache License

private List<GeocoderResult> getDocuments(SolrIndexSearcher searcher, List<Permutation> permutations) {
    List<GeocoderResult> gcResults = new ArrayList<GeocoderResult>();

    for (Permutation p : permutations) {
        if (p.query == null)
            continue;
        try {/*from w ww.j a v  a  2  s.  co  m*/
            TopDocs results = searcher.search(p.query, 1);
            if (results.totalHits > 0) {
                Document doc = searcher.doc(results.scoreDocs[0].doc);
                GeocoderResult gcRes = new GeocoderResult();

                gcRes.setPermutation(p);
                StringBuilder unmatched = new StringBuilder();
                for (int i = 0; i < p.fullAnnotation.length(); i++)
                    if (p.fullAnnotation.charAt(i) == '.')
                        unmatched.append(p.queryTokens.get(i) + " ");

                gcRes.addField("unmatched", unmatched.toString().trim());
                for (String field : hierarchicalFields.values())
                    if (doc.get(field) != null)
                        gcRes.addField(field, doc.get(field));
                for (String field : otherFields.values())
                    if (doc.get(field) != null)
                        gcRes.addField(field, doc.get(field));

                gcRes.setUnmatched(unmatched.toString().trim());
                gcRes.addField(geoField, doc.get(geoField));
                gcResults.add(gcRes);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return gcResults;
}

From source file:net.semanticmetadata.lire.solr.FastLireRequestHandler.java

License:Open Source License

/**
 * Actual search implementation based on (i) hash based retrieval and (ii) feature based re-ranking.
 *
 * @param rsp/*from  w w  w.j  ava2 s.  c  o  m*/
 * @param searcher
 * @param hashFieldName the hash field name
 * @param maximumHits
 * @param terms
 * @param queryFeature
 * @throws java.io.IOException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
private void doSearch(SolrQueryRequest req, SolrQueryResponse rsp, SolrIndexSearcher searcher,
        String hashFieldName, int maximumHits, List<Term> terms, Query query, LireFeature queryFeature)
        throws IOException, IllegalAccessException, InstantiationException {
    // temp feature instance
    LireFeature tmpFeature = queryFeature.getClass().newInstance();
    // Taking the time of search for statistical purposes.
    time = System.currentTimeMillis();

    Filter filter = null;
    // if the request contains a filter:
    if (req.getParams().get("fq") != null) {
        // only filters with [<field>:<value> ]+ are supported
        StringTokenizer st = new StringTokenizer(req.getParams().get("fq"), " ");
        LinkedList<Term> filterTerms = new LinkedList<Term>();
        while (st.hasMoreElements()) {
            String[] tmpToken = st.nextToken().split(":");
            if (tmpToken.length > 1) {
                filterTerms.add(new Term(tmpToken[0], tmpToken[1]));
            }
        }
        if (filterTerms.size() > 0)
            filter = new TermsFilter(filterTerms);
    }

    TopDocs docs; // with query only.
    if (filter == null) {
        docs = searcher.search(query, numberOfCandidateResults);
    } else {
        docs = searcher.search(query, filter, numberOfCandidateResults);
    }
    //        TopDocs docs = searcher.search(query, new TermsFilter(terms), numberOfCandidateResults);   // with TermsFilter and boosting by simple query
    //        TopDocs docs = searcher.search(new ConstantScoreQuery(new TermsFilter(terms)), numberOfCandidateResults); // just with TermsFilter
    time = System.currentTimeMillis() - time;
    rsp.add("RawDocsCount", docs.scoreDocs.length + "");
    rsp.add("RawDocsSearchTime", time + "");
    // re-rank
    time = System.currentTimeMillis();
    TreeSet<SimpleResult> resultScoreDocs = new TreeSet<SimpleResult>();
    float maxDistance = -1f;
    float tmpScore;

    String featureFieldName = FeatureRegistry.getFeatureFieldName(hashFieldName);
    // iterating and re-ranking the documents.
    BinaryDocValues binaryValues = MultiDocValues.getBinaryValues(searcher.getIndexReader(), featureFieldName); // ***  #
    BytesRef bytesRef = new BytesRef();
    for (int i = 0; i < docs.scoreDocs.length; i++) {
        // using DocValues to retrieve the field values ...
        binaryValues.get(docs.scoreDocs[i].doc, bytesRef);
        tmpFeature.setByteArrayRepresentation(bytesRef.bytes, bytesRef.offset, bytesRef.length);
        // Getting the document from the index.
        // This is the slow step based on the field compression of stored fields.
        //            tmpFeature.setByteArrayRepresentation(d.getBinaryValue(name).bytes, d.getBinaryValue(name).offset, d.getBinaryValue(name).length);
        tmpScore = queryFeature.getDistance(tmpFeature);
        if (resultScoreDocs.size() < maximumHits) { // todo: There's potential here for a memory saver, think of a clever data structure that can do the trick without creating a new SimpleResult for each result.
            resultScoreDocs.add(
                    new SimpleResult(tmpScore, searcher.doc(docs.scoreDocs[i].doc), docs.scoreDocs[i].doc));
            maxDistance = resultScoreDocs.last().getDistance();
        } else if (tmpScore < maxDistance) {
            //                if it is nearer to the sample than at least one of the current set:
            //                remove the last one ...
            resultScoreDocs.remove(resultScoreDocs.last());
            //                add the new one ...
            resultScoreDocs.add(
                    new SimpleResult(tmpScore, searcher.doc(docs.scoreDocs[i].doc), docs.scoreDocs[i].doc));
            //                and set our new distance border ...
            maxDistance = resultScoreDocs.last().getDistance();
        }
    }
    //        System.out.println("** Creating response.");
    time = System.currentTimeMillis() - time;
    rsp.add("ReRankSearchTime", time + "");
    LinkedList list = new LinkedList();
    for (Iterator<SimpleResult> it = resultScoreDocs.iterator(); it.hasNext();) {
        SimpleResult result = it.next();
        HashMap m = new HashMap(2);
        m.put("d", result.getDistance());
        // add fields as requested:
        if (req.getParams().get("fl") == null) {
            m.put("id", result.getDocument().get("id"));
            if (result.getDocument().get("title") != null)
                m.put("title", result.getDocument().get("title"));
        } else {
            String fieldsRequested = req.getParams().get("fl");
            if (fieldsRequested.contains("score")) {
                m.put("score", result.getDistance());
            }
            if (fieldsRequested.contains("*")) {
                // all fields
                for (IndexableField field : result.getDocument().getFields()) {
                    String tmpField = field.name();
                    if (result.getDocument().getFields(tmpField).length > 1) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getValues(tmpField));
                    } else if (result.getDocument().getFields(tmpField).length > 0) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getFields(tmpField)[0].stringValue());
                    }
                }
            } else {
                StringTokenizer st;
                if (fieldsRequested.contains(","))
                    st = new StringTokenizer(fieldsRequested, ",");
                else
                    st = new StringTokenizer(fieldsRequested, " ");
                while (st.hasMoreElements()) {
                    String tmpField = st.nextToken();
                    if (result.getDocument().getFields(tmpField).length > 1) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getValues(tmpField));
                    } else if (result.getDocument().getFields(tmpField).length > 0) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getFields(tmpField)[0].stringValue());
                    }
                }
            }
        }
        //            m.put(field, result.getDocument().get(field));
        //            m.put(field.replace("_ha", "_hi"), result.getDocument().getBinaryValue(field));
        list.add(m);
    }
    rsp.add("docs", list);
    // rsp.add("Test-name", "Test-val");
}

From source file:net.semanticmetadata.lire.solr.LireRequestHandler.java

License:Open Source License

/**
 * Returns a random set of documents from the index. Mainly for testing purposes.
 *
 * @param req/*from  ww w.jav a2 s .  c o  m*/
 * @param rsp
 * @throws IOException
 */
private void handleRandomSearch(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {
    SolrIndexSearcher searcher = req.getSearcher();
    DirectoryReader indexReader = searcher.getIndexReader();
    double maxDoc = indexReader.maxDoc();
    int paramRows = req.getParams().getInt("rows", defaultNumberOfResults);
    if (paramRows > maxDoc)
        paramRows = (int) Math.floor(maxDoc);
    if (maxDoc < 1)
        rsp.add("Error", "No documents in index");
    else {
        LinkedList list = new LinkedList();
        while (list.size() < paramRows) {
            Document d = searcher.doc((int) Math.floor(Math.random() * maxDoc));
            list.add(d);
        }
        rsp.addResponse(list);
    }
}

From source file:net.yacy.cora.federate.solr.responsewriter.FlatJSONResponseWriter.java

License:Open Source License

private static final void writeDocs(final Writer writer, final SolrQueryRequest request, final DocList response)
        throws IOException {
    boolean includeScore = false;
    final int sz = response.size();
    SolrIndexSearcher searcher = request.getSearcher();
    DocIterator iterator = response.iterator();
    includeScore = includeScore && response.hasScores();
    IndexSchema schema = request.getSchema();
    for (int i = 0; i < sz; i++) {
        int id = iterator.nextDoc();
        Document doc = searcher.doc(id);
        writeDoc(writer, schema, null, doc.getFields(), (includeScore ? iterator.score() : 0.0f), includeScore);
    }/*  ww  w .  jav a  2s  . c  o  m*/
}

From source file:org.alfresco.solr.AlfrescoSolrEventListener.java

License:Open Source License

@Override
public void newSearcher(SolrIndexSearcher newSearcher, SolrIndexSearcher currentSearcher) {
    Properties p = core.getResourceLoader().getCoreProperties();
    boolean doPermissionChecks = Boolean.parseBoolean(p.getProperty("alfresco.doPermissionChecks", "true"));

    SolrIndexReader newReader = newSearcher.getReader();
    log.info("Max " + newReader.maxDoc());
    log.info("Docs " + newReader.numDocs());
    log.info("Deleted " + newReader.numDeletedDocs());

    long startTime = System.nanoTime();

    ResizeableArrayList<CacheEntry> indexedByDocId = (ResizeableArrayList<CacheEntry>) newSearcher
            .cacheLookup(ALFRESCO_ARRAYLIST_CACHE, KEY_DBID_LEAF_PATH_BY_DOC_ID);
    indexedByDocId.resize(newReader.maxDoc());
    HashSet<String> globalReaders = new HashSet<String>();
    OpenBitSet allLeafDocs = new OpenBitSet(newReader.maxDoc());
    long[] aclIdByDocId = new long[newReader.maxDoc()];
    long[] txByDocId = new long[newReader.maxDoc()];
    long[] aclTxByDocId = new long[newReader.maxDoc()];
    for (int i = 0; i < aclIdByDocId.length; i++) {
        aclIdByDocId[i] = -1;//from  ww w.j  a  va 2 s.c o  m
        txByDocId[i] = -1;
        aclTxByDocId[i] = -1;
    }

    OpenBitSet deleted = new OpenBitSet(newReader.maxDoc());
    OwnerIdManager ownerIdManager = new OwnerIdManager();

    HashMap<Long, CacheEntry> unmatchedByDBID = new HashMap<Long, CacheEntry>();

    if ((incrementalCacheRebuild) && currentSearcher != null) {
        ResizeableArrayList<CacheEntry> oldIndexedByDocId = (ResizeableArrayList<CacheEntry>) currentSearcher
                .cacheLookup(ALFRESCO_ARRAYLIST_CACHE, KEY_DBID_LEAF_PATH_BY_DOC_ID);
        long[] oldAclIdByDocId = (long[]) currentSearcher.cacheLookup(ALFRESCO_CACHE, KEY_ACL_ID_BY_DOC_ID);
        long[] oldTxIdByDocId = (long[]) currentSearcher.cacheLookup(ALFRESCO_CACHE, KEY_TX_ID_BY_DOC_ID);
        long[] oldAclTxIdByDocId = (long[]) currentSearcher.cacheLookup(ALFRESCO_CACHE,
                KEY_ACL_TX_ID_BY_DOC_ID);
        OpenBitSet oldAllLeafDocs = (OpenBitSet) currentSearcher.cacheLookup(ALFRESCO_CACHE, KEY_ALL_LEAF_DOCS);
        OwnerIdManager oldOwnerIdManager = (OwnerIdManager) currentSearcher.cacheLookup(ALFRESCO_CACHE,
                KEY_OWNER_ID_MANAGER);
        ownerIdManager.addAll(oldOwnerIdManager);

        ConcurrentHashMap<Long, Long> addedLeaves = (ConcurrentHashMap<Long, Long>) currentSearcher
                .cacheLookup(ALFRESCO_CACHE, KEY_ADDED_LEAVES);
        ConcurrentHashMap<Long, Long> addedAux = (ConcurrentHashMap<Long, Long>) currentSearcher
                .cacheLookup(ALFRESCO_CACHE, KEY_ADDED_AUX);
        ConcurrentHashMap<Long, Long> addedAcl = (ConcurrentHashMap<Long, Long>) currentSearcher
                .cacheLookup(ALFRESCO_CACHE, KEY_ADDED_ACL);
        ConcurrentHashMap<Long, Long> addedTx = (ConcurrentHashMap<Long, Long>) currentSearcher
                .cacheLookup(ALFRESCO_CACHE, KEY_ADDED_TX);
        ConcurrentHashMap<Long, Long> addedAclTx = (ConcurrentHashMap<Long, Long>) currentSearcher
                .cacheLookup(ALFRESCO_CACHE, KEY_ADDED_ACL_TX);
        ConcurrentHashMap<Long, Long> updatedLeaves = (ConcurrentHashMap<Long, Long>) currentSearcher
                .cacheLookup(ALFRESCO_CACHE, KEY_UPDATED_LEAVES);
        ConcurrentHashMap<Long, Long> updatedAux = (ConcurrentHashMap<Long, Long>) currentSearcher
                .cacheLookup(ALFRESCO_CACHE, KEY_UPDATED_AUX);
        ConcurrentHashMap<Long, Long> updatedAcl = (ConcurrentHashMap<Long, Long>) currentSearcher
                .cacheLookup(ALFRESCO_CACHE, KEY_UPDATED_ACL);
        ConcurrentHashMap<Long, Long> updatedTx = (ConcurrentHashMap<Long, Long>) currentSearcher
                .cacheLookup(ALFRESCO_CACHE, KEY_UPDATED_TX);
        ConcurrentHashMap<Long, Long> updatedAclTx = (ConcurrentHashMap<Long, Long>) currentSearcher
                .cacheLookup(ALFRESCO_CACHE, KEY_UPDATED_ACL_TX);
        ConcurrentHashMap<Long, Long> deletedLeaves = (ConcurrentHashMap<Long, Long>) currentSearcher
                .cacheLookup(ALFRESCO_CACHE, KEY_DELETED_LEAVES);
        ConcurrentHashMap<Long, Long> deletedAux = (ConcurrentHashMap<Long, Long>) currentSearcher
                .cacheLookup(ALFRESCO_CACHE, KEY_DELETED_AUX);
        ConcurrentHashMap<Long, Long> deletedAcl = (ConcurrentHashMap<Long, Long>) currentSearcher
                .cacheLookup(ALFRESCO_CACHE, KEY_DELETED_ACL);
        ConcurrentHashMap<Long, Long> deletedTx = (ConcurrentHashMap<Long, Long>) currentSearcher
                .cacheLookup(ALFRESCO_CACHE, KEY_DELETED_TX);
        ConcurrentHashMap<Long, Long> deletedAclTx = (ConcurrentHashMap<Long, Long>) currentSearcher
                .cacheLookup(ALFRESCO_CACHE, KEY_DELETED_ACL_TX);
        AtomicBoolean deleteAll = (AtomicBoolean) currentSearcher.cacheLookup(ALFRESCO_CACHE, KEY_DELETE_ALL);
        AtomicBoolean checkCache = (AtomicBoolean) currentSearcher.cacheLookup(ALFRESCO_CACHE, KEY_CHECK_CACHE);

        if (checkCache == null) {
            checkCache = new AtomicBoolean(false);
        }

        boolean hasNew = (addedLeaves.size() + addedAux.size() + addedAcl.size() + addedTx.size()
                + addedAclTx.size() + updatedLeaves.size() + updatedAux.size() + updatedAcl.size()
                + updatedTx.size() + updatedAclTx.size()) > 0;

        if (newReader.maxDoc() == 0) {
            // nothing to do
        } else if ((oldIndexedByDocId == null) || (oldAclIdByDocId == null) || (oldTxIdByDocId == null)
                || (oldAclTxIdByDocId == null) || (oldAllLeafDocs == null) || (oldOwnerIdManager == null)) {
            log.warn("Recover from missing cache");
            buildCacheForReader(indexedByDocId, allLeafDocs, aclIdByDocId, txByDocId, aclTxByDocId, newReader,
                    0, newReader.maxDoc(), unmatchedByDBID, ownerIdManager);

        } else if (deleteAll.get()) {
            buildCacheForReader(indexedByDocId, allLeafDocs, aclIdByDocId, txByDocId, aclTxByDocId, newReader,
                    0, newReader.maxDoc(), unmatchedByDBID, ownerIdManager);
        } else {

            try {
                SolrIndexReader[] before = currentSearcher.getReader().getSequentialSubReaders();
                SolrIndexReader[] after = newSearcher.getReader().getSequentialSubReaders();

                CacheSection[] cacheSectionsBefore = SolrIndexReaderCacheSection.getCacheSections(before);
                CacheSection[] cacheSectionsAfter = SolrIndexReaderCacheSection.getCacheSections(after);

                // Copy old to new and apply deletions

                int currentCache = 0;

                for (int i = 0; i < oldAclIdByDocId.length; i++) {
                    CacheSection section = cacheSectionsBefore[currentCache];
                    if (section.getStart() + section.getLength() == i) {
                        currentCache++;
                        if (currentCache == cacheSectionsBefore.length) {
                            currentCache--;
                        }
                        section = cacheSectionsBefore[currentCache];
                    }

                    CacheEntry entry = oldIndexedByDocId.get(i);
                    if (entry != null) {
                        if (entry.getLeaf() == i) {
                            // Leaf
                            if ((updatedLeaves.get(entry.dbid) == null)
                                    && (deletedLeaves.get(entry.dbid) == null)) {
                                // leave
                            } else {
                                section.addDeletion(i);
                                deleted.set(i);
                            }
                        } else if (entry.getPath() == i) {
                            // Aux
                            if ((updatedAux.get(entry.dbid) == null) && (deletedAux.get(entry.dbid) == null)) {
                                // leave
                            } else {
                                section.addDeletion(i);
                                deleted.set(i);
                            }
                        }
                    } else {
                        if ((updatedAcl.get(oldAclIdByDocId[i]) != null)
                                || (deletedAcl.get(oldAclIdByDocId[i]) != null)) {
                            section.addDeletion(i);
                            deleted.set(i);
                        }

                        if ((updatedTx.get(oldTxIdByDocId[i]) != null)
                                || (deletedTx.get(oldTxIdByDocId[i]) != null)) {
                            section.addDeletion(i);
                            deleted.set(i);
                        }

                        if ((updatedAclTx.get(oldAclTxIdByDocId[i]) != null)
                                || (deletedAclTx.get(oldAclTxIdByDocId[i]) != null)) {
                            section.addDeletion(i);
                            deleted.set(i);
                        }

                    }
                }
                LinkedList<CacheMatch> operations = buildCacheUpdateOperations(hasNew, cacheSectionsBefore,
                        cacheSectionsAfter, after);

                log.info("Cache operatoins ...");
                for (CacheMatch match : operations) {
                    log.info(match.toString());
                }

                CacheUpdateTracker tracker = new CacheUpdateTracker(0, 0, 0);

                for (CacheMatch match : operations) {
                    match.updateCache(tracker, oldIndexedByDocId, oldAclIdByDocId, oldTxIdByDocId,
                            oldAclTxIdByDocId, indexedByDocId, allLeafDocs, aclIdByDocId, txByDocId,
                            aclTxByDocId, unmatchedByDBID, deleted, newReader, ownerIdManager);
                }

                // Check unmatched

                int hiddenDocCount = 0;
                for (Long unmatchedDBID : unmatchedByDBID.keySet()) {
                    // hidden docs appear as an unmatched path
                    CacheEntry entry = unmatchedByDBID.get(unmatchedDBID);
                    if ((entry.getLeaf() != 0) && (entry.getPath() == 0)) {
                        // leaf doc with no aux doc;
                        log.info("Leaf has no AUX doc for DBID " + unmatchedDBID + " at position "
                                + entry.getLeaf());
                    }
                    if ((entry.getLeaf() == 0) && (entry.getPath() != 0)) {
                        hiddenDocCount++;
                    }
                }
                log.info("Cache unindexed/error doc count = " + hiddenDocCount);

                // Simple position check;

                boolean simpleCheckOk = true;
                for (int i = 0; i < indexedByDocId.size(); i++) {
                    CacheEntry entry = indexedByDocId.get(i);
                    if (entry != null) {
                        if ((entry.getLeaf() != i) && (entry.getPath() != i)) {
                            log.warn("Core " + newSearcher.getIndexDir());
                            log.warn("Simple cache caheck failed: Incorrect indexedByDocId at " + i);
                            log.warn(".. leaf and path doc poistion do not match the doc position     .. "
                                    + indexedByDocId.get(i));
                            simpleCheckOk = false;
                            break;
                        }
                    }
                }

                if ((simpleCheckOk == false) || forceCheckCache || checkCache.get()) {
                    ResizeableArrayList<CacheEntry> checkIndexedByDocId = (ResizeableArrayList<CacheEntry>) currentSearcher
                            .cacheLookup(ALFRESCO_ARRAYLIST_CACHE, KEY_CHECK_INDEXED_BY_DOC_ID_LIST);
                    checkIndexedByDocId.resize(newReader.maxDoc());
                    OpenBitSet checkAllLeafDocs = new OpenBitSet(newReader.maxDoc());
                    long[] checkAclIdByDocId = new long[newReader.maxDoc()];
                    long[] checkTxIdByDocId = new long[newReader.maxDoc()];
                    long[] checkAclTxIdByDocId = new long[newReader.maxDoc()];

                    buildCacheForReader(checkIndexedByDocId, checkAllLeafDocs, checkAclIdByDocId,
                            checkTxIdByDocId, checkAclTxIdByDocId, newReader, 0, newReader.maxDoc(),
                            new HashMap<Long, CacheEntry>(), ownerIdManager);

                    boolean ok = true;
                    boolean thisTestOk = true;
                    for (int i = 0; i < checkIndexedByDocId.size(); i++) {
                        if (!EqualsHelper.nullSafeEquals(checkIndexedByDocId.get(i), indexedByDocId.get(i))) {
                            if (thisTestOk) {
                                log.warn("Core " + newSearcher.getIndexDir());
                                log.warn("Invalid indexedByDocId at " + i);
                                log.warn(".. found     .. " + indexedByDocId.get(i));
                                log.warn(".. expected  .. " + checkIndexedByDocId.get(i));
                                ok = false;
                                thisTestOk = false;
                            }
                        }
                    }

                    thisTestOk = true;
                    if (!checkAllLeafDocs.equals(allLeafDocs)) {
                        if (thisTestOk) {
                            log.warn("Core " + newSearcher.getIndexDir());
                            log.warn("Invalid AllLeafDocs cache");
                            ok = false;
                            thisTestOk = false;
                        }
                    }

                    thisTestOk = true;
                    for (int i = 0; i < checkAclIdByDocId.length; i++) {
                        if (checkAclIdByDocId[i] != aclIdByDocId[i]) {

                            if (thisTestOk) {
                                log.warn("Core " + newSearcher.getIndexDir());
                                log.warn("Invalid AclIdByDocId cache at " + i);
                                log.warn(".. found    .. " + aclIdByDocId[i]);
                                log.warn(".. expected .. " + checkAclIdByDocId[i]);
                                try {
                                    log.warn(".. expected .. " + newSearcher.doc(i));
                                } catch (IOException e) {
                                    log.error("IO Exception", e);
                                }
                                ok = false;
                                thisTestOk = false;
                            }

                        }
                    }

                    thisTestOk = true;
                    for (int i = 0; i < checkTxIdByDocId.length; i++) {
                        if (checkTxIdByDocId[i] != txByDocId[i]) {

                            if (thisTestOk) {
                                log.warn("Core " + newSearcher.getIndexDir());
                                log.warn("Invalid txByDocId cache at " + i);
                                log.warn(".. found    .. " + txByDocId[i]);
                                log.warn(".. expected .. " + checkTxIdByDocId[i]);
                                try {
                                    log.warn(".. expected .. " + newSearcher.doc(i));
                                } catch (IOException e) {
                                    log.error("IO Exception", e);
                                }

                                ok = false;
                                thisTestOk = false;
                            }

                        }
                    }

                    thisTestOk = true;
                    for (int i = 0; i < checkAclTxIdByDocId.length; i++) {
                        if (checkAclTxIdByDocId[i] != aclTxByDocId[i]) {

                            if (thisTestOk) {
                                log.warn("Core " + newSearcher.getIndexDir());
                                log.warn("Invalid aclTxByDocId cache at " + i);
                                log.warn(".. found    .. " + aclTxByDocId[i]);
                                log.warn(".. expected .. " + checkAclTxIdByDocId[i]);

                                try {
                                    log.warn(".. expected .. " + newSearcher.doc(i));
                                } catch (IOException e) {
                                    log.error("IO Exception", e);
                                }

                                ok = false;
                                thisTestOk = false;
                            }

                        }
                    }

                    if (!ok) {
                        indexedByDocId.copyFrom(checkIndexedByDocId);
                        allLeafDocs = checkAllLeafDocs;
                        aclIdByDocId = checkAclIdByDocId;
                        txByDocId = checkTxIdByDocId;
                        aclTxByDocId = checkAclTxIdByDocId;

                        log.warn("... Using recomputed cache");
                    } else {
                        log.info("... cache OK");
                    }

                }
            } catch (IllegalStateException ise) {
                log.info("Cache state error -> rebuilding", ise);
                buildCacheForReader(indexedByDocId, allLeafDocs, aclIdByDocId, txByDocId, aclTxByDocId,
                        newReader, 0, newReader.maxDoc(), new HashMap<Long, CacheEntry>(), ownerIdManager);
            }
        }

    } else {
        buildCacheForReader(indexedByDocId, allLeafDocs, aclIdByDocId, txByDocId, aclTxByDocId, newReader, 0,
                newReader.maxDoc(), new HashMap<Long, CacheEntry>(), ownerIdManager);
    }

    long endTime = System.nanoTime();
    log.info("Core cache rebuilt in " + ((endTime - startTime) / (1.0e9)));
    startTime = System.nanoTime();

    int size = doPermissionChecks ? (int) allLeafDocs.cardinality() : 0;

    ResizeableArrayList<CacheEntry> indexedOderedByAclIdThenDoc = (ResizeableArrayList<CacheEntry>) newSearcher
            .cacheLookup(ALFRESCO_ARRAYLIST_CACHE, KEY_DBID_LEAF_PATH_BY_ACL_ID_THEN_LEAF);
    indexedOderedByAclIdThenDoc.resize(size);
    ResizeableArrayList<CacheEntry> indexedOderedByOwnerIdThenDoc = (ResizeableArrayList<CacheEntry>) newSearcher
            .cacheLookup(ALFRESCO_ARRAYLIST_CACHE, KEY_DBID_LEAF_PATH_BY_OWNER_ID_THEN_LEAF);
    indexedOderedByOwnerIdThenDoc.resize(size);

    if (doPermissionChecks) {
        int doc = -1;
        int pos = 0;
        while ((doc = allLeafDocs.nextSetBit(doc + 1)) != -1) {
            CacheEntry entry = indexedByDocId.get(doc);
            indexedOderedByAclIdThenDoc.set(pos, entry);
            indexedOderedByOwnerIdThenDoc.set(pos, entry);
            pos++;
        }
    }

    indexedOderedByAclIdThenDoc.sort(new Comparator<CacheEntry>() {
        @Override
        public int compare(CacheEntry o1, CacheEntry o2) {
            if (o2 == null) {
                if (o1 == null) {
                    return 0;
                }

                else {
                    return -1; // nulls at the end
                }
            } else {
                if (o1 == null) {
                    return 1;
                } else {
                    long diff = o1.getAclid() - o2.getAclid();
                    if (diff == 0L) {
                        return o1.getLeaf() - o2.getLeaf();
                    } else {
                        return (diff > 0L) ? 1 : -1;
                    }
                }
            }

        }
    });

    // build lookups

    HashMap<AclLookUp, AclLookUp> aclLookUp = new HashMap<AclLookUp, AclLookUp>();

    AclLookUp currentAclLookUp = null;
    for (int i = 0; i < indexedOderedByAclIdThenDoc.size(); i++) {
        CacheEntry entry = indexedOderedByAclIdThenDoc.get(i);
        if (entry != null) {
            if (currentAclLookUp == null) {
                currentAclLookUp = new AclLookUp(entry.getAclid(), i);
            } else {
                if (currentAclLookUp.aclid == entry.aclid) {
                    // carry on
                } else {
                    // acl id has changed - new set
                    currentAclLookUp.setEnd(i);
                    AclLookUp next = new AclLookUp(entry.getAclid(), i);
                    aclLookUp.put(currentAclLookUp, currentAclLookUp);
                    currentAclLookUp = next;
                }
            }
        } else {
            // found first null we are done
            if (currentAclLookUp != null) {
                currentAclLookUp.setEnd(i);
                aclLookUp.put(currentAclLookUp, currentAclLookUp);
            }
            break;
        }
    }
    if (currentAclLookUp != null) {
        currentAclLookUp.setEnd(indexedOderedByAclIdThenDoc.size());
        aclLookUp.put(currentAclLookUp, currentAclLookUp);
    }

    indexedOderedByOwnerIdThenDoc.sort(new Comparator<CacheEntry>() {

        @Override
        public int compare(CacheEntry o1, CacheEntry o2) {
            if (o2 == null) {
                if (o1 == null) {
                    return 0;
                }

                else {
                    return -1; // nulls at the end
                }
            } else {
                if (o1 == null) {
                    return 1;
                } else {
                    int diff = o1.getOwner() - o2.getOwner();
                    if (diff == 0) {
                        return o1.getLeaf() - o2.getLeaf();
                    } else {
                        return diff;
                    }
                }
            }

        }
    });

    // build lookups

    HashMap<String, OwnerLookUp> ownerLookUp = new HashMap<String, OwnerLookUp>();

    OwnerLookUp currentOwnerLookUp = null;
    for (int i = 0; i < indexedOderedByOwnerIdThenDoc.size(); i++) {
        CacheEntry entry = indexedOderedByOwnerIdThenDoc.get(i);
        if (entry != null) {
            if (currentOwnerLookUp == null) {
                currentOwnerLookUp = new OwnerLookUp(entry.getOwner(), i);
            } else {
                if (currentOwnerLookUp.owner == entry.owner) {
                    // carry on
                } else {
                    // acl id has changed - new set
                    currentOwnerLookUp.setEnd(i);
                    OwnerLookUp next = new OwnerLookUp(entry.getOwner(), i);
                    try {
                        ownerLookUp.put(ownerIdManager.get(currentOwnerLookUp.owner), currentOwnerLookUp);
                    } catch (IndexOutOfBoundsException e) {
                        log.warn("  " + ownerIdManager);
                        log.warn("  looking for " + currentOwnerLookUp.owner);
                        throw e;
                    }
                    currentOwnerLookUp = next;
                }
            }
        } else {
            // found first null we are done
            if (currentOwnerLookUp != null) {
                currentOwnerLookUp.setEnd(i);
                try {
                    ownerLookUp.put(ownerIdManager.get(currentOwnerLookUp.owner), currentOwnerLookUp);
                } catch (IndexOutOfBoundsException e) {
                    log.warn("  " + ownerIdManager);
                    log.warn("  looking for " + currentOwnerLookUp.owner);
                    throw e;
                }
            }
            break;
        }
    }
    if (currentOwnerLookUp != null) {
        currentOwnerLookUp.setEnd(indexedOderedByOwnerIdThenDoc.size());
        try {
            ownerLookUp.put(ownerIdManager.get(currentOwnerLookUp.owner), currentOwnerLookUp);
        } catch (IndexOutOfBoundsException e) {
            log.warn("  " + ownerIdManager);
            log.warn("  looking for " + currentOwnerLookUp.owner);
            throw e;
        }
    }

    // cache readers and acl doc ids

    //HashMap<String, HashSet<Long>> readerToAclIds = new HashMap<String, HashSet<Long>>();
    BitDocSet publicDocSet = new BitDocSet(new OpenBitSet(newReader.maxDoc()));

    if (doPermissionChecks) {
        try {
            HashSet<Long> globallyReadableAcls = buildReaderAclIds(newSearcher, "GROUP_EVERYONE", aclIdByDocId);
            newSearcher.cacheInsert(AlfrescoSolrEventListener.ALFRESCO_READER_TO_ACL_IDS_CACHE,
                    "GROUP_EVERYONE", globallyReadableAcls);

            AclLookUp key = new AclLookUp(0);
            for (Long longAcl : globallyReadableAcls) {

                key.setAclid(longAcl);
                AlfrescoSolrEventListener.AclLookUp value = aclLookUp.get(key);
                if (value != null) {
                    for (int i = value.getStart(); i < value.getEnd(); i++) {
                        publicDocSet.add(indexedOderedByAclIdThenDoc.get(i).getLeaf());
                    }
                }
            }
        } catch (IOException e) {
            log.error("IO Exception while warming searcher", e);
        }

    }

    // transform to readers to acl ids

    endTime = System.nanoTime();
    log.info("Derived caches rebuilt in " + ((endTime - startTime) / (1.0e9)));

    newSearcher.cacheInsert(ALFRESCO_CACHE, KEY_ACL_ID_BY_DOC_ID, aclIdByDocId);
    newSearcher.cacheInsert(ALFRESCO_CACHE, KEY_TX_ID_BY_DOC_ID, txByDocId);
    newSearcher.cacheInsert(ALFRESCO_CACHE, KEY_ACL_TX_ID_BY_DOC_ID, aclTxByDocId);

    // TODO: Make global readers configurable.
    globalReaders.add(PermissionService.OWNER_AUTHORITY);
    globalReaders.add(PermissionService.ADMINISTRATOR_AUTHORITY);
    globalReaders.add(AuthenticationUtil.getSystemUserName());

    newSearcher.cacheInsert(ALFRESCO_CACHE, KEY_GLOBAL_READERS, globalReaders);

    newSearcher.cacheInsert(ALFRESCO_CACHE, KEY_ALL_LEAF_DOCS, allLeafDocs);

    newSearcher.cacheInsert(ALFRESCO_CACHE, KEY_ACL_LOOKUP, aclLookUp);

    newSearcher.cacheInsert(ALFRESCO_CACHE, KEY_OWNER_LOOKUP, ownerLookUp);

    newSearcher.cacheInsert(ALFRESCO_CACHE, KEY_OWNER_ID_MANAGER, ownerIdManager);

    newSearcher.cacheInsert(ALFRESCO_CACHE, KEY_PUBLIC_DOC_SET, publicDocSet);

    try {
        if (currentSearcher != null) {
            newSearcher.warm(currentSearcher);
        }
    } catch (IOException e) {
        log.error("IO Exception while warming searcher", e);
    }

}