Example usage for org.apache.solr.client.solrj.request CoreAdminRequest reloadCore

List of usage examples for org.apache.solr.client.solrj.request CoreAdminRequest reloadCore

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.request CoreAdminRequest reloadCore.

Prototype

public static CoreAdminResponse reloadCore(String name, SolrClient client)
            throws SolrServerException, IOException 

Source Link

Usage

From source file:com.github.fengtan.sophie.toolbars.CoresToolbar.java

License:Open Source License

/**
 * Populate toolbar with buttons.//  w w  w .  ja v a2  s . c  o m
 * 
 * @param composite
 *            Parent composite.
 */
private void initToolbar(final Composite composite) {
    Display display = composite.getDisplay();
    ClassLoader loader = getClass().getClassLoader();

    // Instantiate toolbar.
    ToolBar toolBar = new ToolBar(composite, SWT.BORDER);

    // Instantiate images.
    imgRefresh = new Image(display, loader.getResourceAsStream("toolbar/refresh.png"));
    imgAdd = new Image(display, loader.getResourceAsStream("toolbar/add.png"));
    imgDelete = new Image(display, loader.getResourceAsStream("toolbar/delete.png"));
    imgRename = new Image(display, loader.getResourceAsStream("toolbar/rename.png"));
    imgSwap = new Image(display, loader.getResourceAsStream("toolbar/swap.png"));
    imgReload = new Image(display, loader.getResourceAsStream("toolbar/restore.png"));
    imgReloadAll = new Image(display, loader.getResourceAsStream("toolbar/restore.png"));

    // Instantiate buttons.
    itemRefresh = new ToolItem(toolBar, SWT.PUSH);
    itemRefresh.setImage(imgRefresh);
    itemRefresh.setText("Refresh");
    itemRefresh.setToolTipText("Refresh list of cores");
    itemRefresh.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            try {
                table.refresh();
            } catch (SophieException e) {
                ExceptionDialog.open(composite.getShell(),
                        new SophieException("Unable to refresh list of cores", e));
            }
        }
    });

    new ToolItem(toolBar, SWT.SEPARATOR);

    itemAdd = new ToolItem(toolBar, SWT.PUSH);
    itemAdd.setImage(imgAdd);
    itemAdd.setText("Add");
    itemAdd.setToolTipText("Add new core");
    itemAdd.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            String[] labels = new String[] { "Name:", "Instance Directory:", "Config file:", "Schema file:",
                    "Data Directory:", "Transaction log directory:" };
            String[] defaultValues = new String[] { "collectionX", "/path/to/solr/collectionX",
                    "solrconfig.xml", "schema.xml", "/path/to/solr/collectionX/data",
                    "/path/to/solr/collectionX/tlog" };
            MultipleInputDialog dialog = new MultipleInputDialog(composite.getShell(), "Add new core", labels,
                    defaultValues);
            dialog.open();
            if (dialog.getReturnCode() != IDialogConstants.OK_ID) {
                return;
            }
            try {
                CoreAdminRequest.createCore(dialog.getValue(0), dialog.getValue(1), Sophie.client,
                        dialog.getValue(2), dialog.getValue(3), dialog.getValue(4), dialog.getValue(5));
                table.refresh();
            } catch (SolrServerException | IOException | SolrException | SophieException e) {
                ExceptionDialog.open(composite.getShell(),
                        new SophieException("Unable to add new core \"" + dialog.getValue(0) + "\"", e));
            }
        }
    });

    itemDelete = new ToolItem(toolBar, SWT.PUSH);
    itemDelete.setImage(imgDelete);
    itemDelete.setText("Delete");
    itemDelete.setToolTipText("Delete core");
    itemDelete.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            String coreName = table.getSelectedCore();
            MessageBox messageBox = new MessageBox(composite.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
            messageBox.setText("Delete core");
            messageBox.setMessage("Do you really want to delete this core (\"" + coreName + "\") ?");
            int response = messageBox.open();
            if (response == SWT.YES) {
                try {
                    CoreAdminRequest.unloadCore(coreName, Sophie.client);
                    table.refresh();
                } catch (SolrServerException | IOException | SolrException | SophieException e) {
                    ExceptionDialog.open(composite.getShell(),
                            new SophieException("Unable to delete core \"" + coreName + "\"", e));
                }
            }
        }
    });
    itemDelete.setEnabled(false);

    itemRename = new ToolItem(toolBar, SWT.PUSH);
    itemRename.setImage(imgRename);
    itemRename.setText("Rename");
    itemRename.setToolTipText("Rename core");
    itemRename.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            String oldCoreName = table.getSelectedCore();
            InputDialog newCoreName = new InputDialog(composite.getShell(), "Rename core",
                    "New name (\"" + oldCoreName + "\"):", oldCoreName, null);
            newCoreName.open();
            if (newCoreName.getReturnCode() != IDialogConstants.OK_ID) {
                return;
            }
            try {
                CoreAdminRequest.renameCore(oldCoreName, newCoreName.getValue(), Sophie.client);
                table.refresh();
            } catch (SolrServerException | IOException | SolrException | SophieException e) {
                ExceptionDialog.open(composite.getShell(), new SophieException(
                        "Unable to rename core \"" + oldCoreName + "\" into \"" + newCoreName + "\"", e));
            }
        }
    });
    itemRename.setEnabled(false);

    new ToolItem(toolBar, SWT.SEPARATOR);

    itemSwap = new ToolItem(toolBar, SWT.PUSH);
    itemSwap.setImage(imgSwap);
    itemSwap.setText("Swap");
    itemSwap.setToolTipText("Swap cores");
    itemSwap.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            String coreName = table.getSelectedCore();
            Map<String, NamedList<Object>> cores;
            try {
                cores = SolrUtils.getCores();
            } catch (SophieException e) {
                Sophie.log.error("Unable to suggest list of cores", e);
                cores = Collections.emptyMap();
            }
            Object[] coreObjects = cores.keySet().toArray();
            String[] coreStrings = Arrays.copyOf(coreObjects, coreObjects.length, String[].class);
            CComboDialog dialog = new CComboDialog(composite.getShell(), "Swap cores",
                    "Swap core \"" + coreName + "\" with:", coreStrings, null);
            dialog.open();
            if (dialog.getReturnCode() != IDialogConstants.OK_ID) {
                return;
            }
            CoreAdminRequest request = new CoreAdminRequest();
            request.setCoreName(coreName);
            request.setOtherCoreName(dialog.getValue());
            request.setAction(CoreAdminAction.SWAP);
            try {
                request.process(Sophie.client);
                table.refresh();
            } catch (SolrServerException | IOException | SolrException | SophieException e) {
                ExceptionDialog.open(composite.getShell(), new SophieException(
                        "Unable to swap cores \"" + coreName + "\" and \"" + dialog.getValue() + "\"", e));
            }
        }
    });
    itemSwap.setEnabled(false);

    itemReload = new ToolItem(toolBar, SWT.PUSH);
    itemReload.setImage(imgReload);
    itemReload.setText("Reload");
    itemReload.setToolTipText(
            "Reload core - this will reload any configuration changes you may have made to solrconfig.xml or schema.xml");
    itemReload.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            String coreName = table.getSelectedCore();
            try {
                CoreAdminRequest.reloadCore(coreName, Sophie.client);
                table.refresh();
            } catch (SolrServerException | IOException | SolrException | SophieException e) {
                ExceptionDialog.open(composite.getShell(),
                        new SophieException("Unable to reload core \"" + coreName + "\"", e));
            }
        }
    });
    itemReload.setEnabled(false);

    itemReloadAll = new ToolItem(toolBar, SWT.PUSH);
    itemReloadAll.setImage(imgReloadAll);
    itemReloadAll.setText("Reload all");
    itemReloadAll.setToolTipText("Reload all cores");
    itemReloadAll.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            try {
                for (String coreName : SolrUtils.getCores().keySet()) {
                    CoreAdminRequest.reloadCore(coreName, Sophie.client);
                }
                table.refresh();
            } catch (SolrServerException | IOException | SolrException | SophieException e) {
                ExceptionDialog.open(composite.getShell(),
                        new SophieException("Unable to reload all cores", e));
            }
        }
    });

    toolBar.pack();
}

From source file:com.hurence.logisland.service.solr.api.SolrClientService.java

License:Apache License

protected void refreshCore(String name) throws IOException, SolrServerException {
    CoreAdminRequest.reloadCore(name, getClient());
}

From source file:io.redlink.solrlib.standalone.SolrServerConnector.java

License:Apache License

@Override
@SuppressWarnings("squid:S3776")
protected void init(ExecutorService executorService) throws IOException, SolrServerException {
    Preconditions.checkState(initialized.compareAndSet(false, true));
    Preconditions.checkArgument(Objects.nonNull(solrBaseUrl));

    if (configuration.isDeployCores() && Objects.nonNull(configuration.getSolrHome())) {
        final Path solrHome = configuration.getSolrHome();
        Files.createDirectories(solrHome);
        final Path libDir = solrHome.resolve("lib");
        Files.createDirectories(libDir);

        try (HttpSolrClient solrClient = new HttpSolrClient.Builder(solrBaseUrl).build()) {
            for (SolrCoreDescriptor coreDescriptor : coreDescriptors) {
                final String coreName = coreDescriptor.getCoreName();
                if (availableCores.containsKey(coreName)) {
                    log.warn("CoreName-Clash: {} already initialized. Skipping {}", coreName,
                            coreDescriptor.getClass());
                    continue;
                }/*from  w  w w.j  a  v a 2  s  .c om*/
                final String remoteName = createRemoteName(coreName);

                final Path coreHome = solrHome.resolve(remoteName);
                coreDescriptor.initCoreDirectory(coreHome, libDir);

                final Path corePropertiesFile = coreHome.resolve("core.properties");
                // core.properties is created by the CreateCore-Command.
                Files.deleteIfExists(corePropertiesFile);

                if (coreDescriptor.getNumShards() > 1 || coreDescriptor.getReplicationFactor() > 1) {
                    log.warn("Deploying {} to SolrServerConnector, ignoring config of shards={},replication={}",
                            coreName, coreDescriptor.getNumShards(), coreDescriptor.getReplicationFactor());
                }

                // Create or reload the core
                if (CoreAdminRequest.getStatus(remoteName, solrClient).getStartTime(remoteName) == null) {
                    final CoreAdminResponse adminResponse = CoreAdminRequest.createCore(remoteName,
                            coreHome.toAbsolutePath().toString(), solrClient);
                } else {
                    final CoreAdminResponse adminResponse = CoreAdminRequest.reloadCore(remoteName, solrClient);
                }
                // schedule client-side core init
                final boolean isNewCore = findInNamedList(
                        CoreAdminRequest.getStatus(remoteName, solrClient).getCoreStatus(remoteName), "index",
                        "lastModified") == null;
                scheduleCoreInit(executorService, coreDescriptor, isNewCore);

                availableCores.put(coreName, coreDescriptor);
            }
        }
    } else {
        try (HttpSolrClient solrClient = new HttpSolrClient.Builder(solrBaseUrl).build()) {
            for (SolrCoreDescriptor coreDescriptor : coreDescriptors) {
                final String coreName = coreDescriptor.getCoreName();
                if (availableCores.containsKey(coreName)) {
                    log.warn("CoreName-Clash: {} already initialized. Skipping {}", coreName,
                            coreDescriptor.getClass());
                    continue;
                }
                final String remoteName = createRemoteName(coreName);
                if (CoreAdminRequest.getStatus(remoteName, solrClient).getStartTime(remoteName) == null) {
                    // Core does not exists
                    log.warn("Collection {} (remote: {}) not available in Solr '{}' "
                            + "but deployCores is set to false", coreName, remoteName, solrBaseUrl);
                } else {
                    log.debug("Collection {} exists in Solr '{}' as {}", coreName, solrBaseUrl, remoteName);
                    scheduleCoreInit(executorService, coreDescriptor, false);
                    availableCores.put(coreName, coreDescriptor);
                }
            }
        }
    }
}

From source file:org.codice.solr.factory.impl.HttpSolrClientFactory.java

License:Open Source License

private static void createSolrCore(String url, String coreName, String configFileName, HttpClient httpClient)
        throws IOException, SolrServerException {
    HttpSolrClient client;//from ww  w.j a va2s . c  om
    if (httpClient != null) {
        client = new HttpSolrClient(url, httpClient);
    } else {
        client = new HttpSolrClient(url);
    }

    HttpResponse ping = client.getHttpClient().execute(new HttpHead(url));
    if (ping != null && ping.getStatusLine().getStatusCode() == 200) {
        ConfigurationFileProxy configProxy = new ConfigurationFileProxy(ConfigurationStore.getInstance());
        configProxy.writeSolrConfiguration(coreName);
        if (!solrCoreExists(client, coreName)) {
            LOGGER.debug("Creating Solr core {}", coreName);

            String configFile = StringUtils.defaultIfBlank(configFileName, DEFAULT_SOLRCONFIG_XML);

            String solrDir;
            if (System.getProperty("solr.data.dir") != null) {
                solrDir = System.getProperty("solr.data.dir");
            } else {
                solrDir = Paths.get(System.getProperty("karaf.home"), "data", "solr").toString();
            }

            String instanceDir = Paths.get(solrDir, coreName).toString();

            String dataDir = Paths.get(instanceDir, "data").toString();

            CoreAdminRequest.createCore(coreName, instanceDir, client, configFile, DEFAULT_SCHEMA_XML, dataDir,
                    dataDir);
        } else {
            LOGGER.debug("Solr core ({}) already exists - reloading it", coreName);
            CoreAdminRequest.reloadCore(coreName, client);
        }
    } else {
        LOGGER.debug("Unable to ping Solr core {}", coreName);
        throw new SolrServerException("Unable to ping Solr core");
    }
}

From source file:org.codice.solr.factory.SolrServerFactory.java

License:Open Source License

private static void createSolrCore(String url, String coreName, String configFileName, HttpClient client) {
    HttpSolrServer solrServer;//w w  w  .j  av  a 2 s  . co m
    if (client != null) {
        solrServer = new HttpSolrServer(url, client);
    } else {
        solrServer = new HttpSolrServer(url);
    }

    if (!solrCoreExists(solrServer, coreName)) {
        LOGGER.info("Creating Solr core {}", coreName);

        String instanceDir = System.getProperty("karaf.home") + "/data/solr/" + coreName;
        String configFile = StringUtils.defaultIfBlank(configFileName, DEFAULT_SOLRCONFIG_XML);

        try {
            CoreAdminRequest.createCore(coreName, instanceDir, solrServer, configFile, DEFAULT_SCHEMA_XML);
        } catch (SolrServerException e) {
            LOGGER.error("SolrServerException creating " + coreName + " core", e);
        } catch (IOException e) {
            LOGGER.error("IOException creating " + coreName + " core", e);
        }
    } else {
        LOGGER.info("Solr core {} already exists - just reload it", coreName);
        try {
            CoreAdminRequest.reloadCore(coreName, solrServer);
        } catch (SolrServerException e) {
            LOGGER.error("SolrServerException reloading " + coreName + " core", e);
        } catch (IOException e) {
            LOGGER.error("IOException reloading " + coreName + " core", e);
        }
    }
}

From source file:org.hara.sodra.index.SodraServer.java

License:Apache License

protected void updateSchemaConfigFile(String indexName, Collection<ColumnDefinition> columns) throws Exception {
    Path corePath = Paths.get(solrHome.toString(), indexName);
    Path schemaXML = Paths.get(corePath.toString(), "conf", "schema.xml");
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(schemaXML.toFile());
    Element rootNode = document.getDocumentElement();
    for (ColumnDefinition cd : columns) {
        Element field = document.createElement("field");
        NamedNodeMap attributes = field.getAttributes();
        Attr name = document.createAttribute("name");
        Attr type = document.createAttribute("type");
        Attr indexed = document.createAttribute("indexed");
        Attr stored = document.createAttribute("stored");
        Attr required = document.createAttribute("required");
        Attr multi = document.createAttribute("multiValued");

        name.setValue(cd.name.toString());
        type.setValue(CassandraToSodraTypeMapper.getSolrType(cd.type));
        indexed.setValue("true");
        if (cd.isPrimaryKeyColumn()) {
            stored.setValue("true");
        } else {/*from w  w  w.java2 s .  c  o  m*/
            stored.setValue("false");
        }
        required.setValue("true");
        multi.setValue("false");

        attributes.setNamedItem(name);
        attributes.setNamedItem(type);
        attributes.setNamedItem(indexed);
        attributes.setNamedItem(stored);
        attributes.setNamedItem(required);
        attributes.setNamedItem(multi);

        rootNode.appendChild(field);
    }

    // update unique key
    NodeList nodeList = document.getElementsByTagName("uniqueKey");
    if (nodeList.getLength() != 1) {
        throw new SolrException(ErrorCode.SERVER_ERROR, "Should have 1 <uniqueKey> node in schema.xml");
    }
    Node uniqueKey = nodeList.item(0);
    uniqueKey.setTextContent(idColumn.name.toString());

    // save the updated file
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StreamResult result = new StreamResult(schemaXML.toFile());
    DOMSource source = new DOMSource(document);
    transformer.transform(source, result);

    CoreAdminRequest.reloadCore(indexName, client);
}