Example usage for org.apache.solr.core CoreContainer getCore

List of usage examples for org.apache.solr.core CoreContainer getCore

Introduction

In this page you can find the example usage for org.apache.solr.core CoreContainer getCore.

Prototype

public SolrCore getCore(String name) 

Source Link

Document

Gets a core by name and increase its refcount.

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;// w w w. j  a v a2 s.com
    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 {/*w w w  .ja v a  2  s . c o 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.sentric.hbase.coprocessor.TestSolrCore.java

License:Apache License

@Test
public void loadSolrCore() throws Exception {
    System.out.println("init Solr Core");

    System.setProperty("solr.velocity.enabled", "false");
    final String factoryProp = System.getProperty("solr.directoryFactory");
    if (factoryProp == null) {
        System.setProperty("solr.directoryFactory", "solr.RAMDirectoryFactory");
    }/*  w  w w. j  a  va2 s  .  co m*/

    boolean abortOnConfigurationError = true;
    final CoreContainer.Initializer init = new CoreContainer.Initializer();
    try {
        final CoreContainer coreContainer = init.initialize();
        abortOnConfigurationError = init.isAbortOnConfigurationError();
        System.out.println("user.dir=" + System.getProperty("user.dir"));
        Assert.assertNotNull(coreContainer.getCore(""));
    } catch (Throwable t) {
        System.out.println("Could not start Solr. Check solr/home property: " + t.getMessage());
        SolrConfig.severeErrors.add(t);
        SolrCore.log(t);
    }

    // Optionally abort if we found a sever error
    if (abortOnConfigurationError && SolrConfig.severeErrors.size() > 0) {
        System.out.println("Severe errors in solr configuration.");
        Assert.fail();
    }

}

From source file:org.alfresco.solr.component.QueryLoggingComponent.java

License:Open Source License

private void log(ResponseBuilder rb) throws IOException {
    boolean isShard = rb.req.getParams().getBool(ShardParams.IS_SHARD, false);
    if (!isShard) {
        CoreContainer container = rb.req.getCore().getCoreContainer();
        SolrCore logCore = container.getCore(rb.req.getCore().getName() + "_qlog");
        if (logCore != null) {
            JSONObject json = (JSONObject) rb.req.getContext().get(AbstractQParser.ALFRESCO_JSON);

            SolrQueryRequest request = null;
            UpdateRequestProcessor processor = null;
            try {
                request = new LocalSolrQueryRequest(logCore, new NamedList<>());
                processor = logCore.getUpdateProcessingChain(null).createProcessor(request,
                        new SolrQueryResponse());

                AddUpdateCommand cmd = new AddUpdateCommand(request);
                cmd.overwrite = true;/*  w w w .  j  ava2 s  . c o  m*/
                SolrInputDocument input = new SolrInputDocument();
                input.addField("id", GUID.generate());
                input.addField("_version_", "1");

                input.addField("timestamp", DateTimeFormatter.ISO_INSTANT.format(Instant.now()));

                if (json != null) {
                    try {
                        ArrayList<String> authorityList = new ArrayList<String>(1);
                        JSONArray authorities = json.getJSONArray("authorities");
                        for (int i = 0; i < authorities.length(); i++) {
                            String authorityString = authorities.getString(i);
                            authorityList.add(authorityString);
                        }

                        for (String authority : authorityList) {
                            if (AuthorityType.getAuthorityType(authority) == AuthorityType.USER) {
                                input.addField("user", authority);
                                break;
                            }
                        }
                    } catch (JSONException e) {
                        input.addField("user", "<UNKNOWN>");
                    }
                } else {
                    input.addField("user", "<UNKNOWN>");
                }

                String userQuery = rb.req.getParams().get(SpellingParams.SPELLCHECK_Q);
                if (userQuery == null) {
                    if (json != null) {
                        try {
                            userQuery = json.getString("query");
                        } catch (JSONException e) {
                        }
                    }
                }
                if (userQuery == null) {
                    userQuery = rb.req.getParams().get(CommonParams.Q);
                }

                if (userQuery != null) {
                    input.addField("user_query", userQuery);
                }

                Query query = rb.getQuery();
                input.addField("query", query.toString());

                if (rb.getResults().docList != null) {
                    input.addField("found", rb.getResults().docList.matches());
                }
                input.addField("time", rb.req.getRequestTimer().getTime());

                cmd.solrDoc = input;
                processor.processAdd(cmd);
            }

            finally {
                if (processor != null) {
                    processor.finish();
                }
                if (request != null) {
                    request.close();
                }
            }
        }
    }
}

From source file:org.opencms.search.solr.CmsSolrIndex.java

License:Open Source License

/**
 * @see org.opencms.search.CmsSearchIndex#createIndexBackup()
 *///from w w w  .  ja v a 2  s.  c  o m
@Override
protected String createIndexBackup() {

    if (!isBackupReindexing()) {
        // if no backup is generated we don't need to do anything
        return null;
    }
    if (m_solr instanceof EmbeddedSolrServer) {
        EmbeddedSolrServer ser = (EmbeddedSolrServer) m_solr;
        CoreContainer con = ser.getCoreContainer();
        SolrCore core = con.getCore(getCoreName());
        if (core != null) {
            try {
                SolrRequestHandler h = core.getRequestHandler("/replication");
                if (h instanceof ReplicationHandler) {
                    h.handleRequest(
                            new LocalSolrQueryRequest(core,
                                    CmsRequestUtil.createParameterMap("?command=backup")),
                            new SolrQueryResponse());
                }
            } finally {
                core.close();
            }
        }
    }
    return null;
}

From source file:org.vootoo.server.Vootoo.java

License:Apache License

public static SolrCore checkProps(CoreContainer cores, ZkNodeProps zkProps) {
    String corename;//from  w w w  .j ava2  s  .  co  m
    SolrCore core = null;
    if (cores.getZkController().getNodeName().equals(zkProps.getStr(ZkStateReader.NODE_NAME_PROP))) {
        corename = zkProps.getStr(ZkStateReader.CORE_NAME_PROP);
        core = cores.getCore(corename);
    }
    return core;
}