Example usage for org.apache.solr.client.solrj.impl HttpSolrClient deleteByQuery

List of usage examples for org.apache.solr.client.solrj.impl HttpSolrClient deleteByQuery

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.impl HttpSolrClient deleteByQuery.

Prototype

public UpdateResponse deleteByQuery(String query) throws SolrServerException, IOException 

Source Link

Document

Deletes documents from the index based on a query

Usage

From source file:it.damore.solr.importexport.App.java

License:Open Source License

/**
 * @param client//from   w  w w .j  a  va  2 s  . co  m
 * @param outputFile
 * @throws FileNotFoundException
 * @throws IOException
 * @throws SolrServerException
 */
private static void writeAllDocuments(HttpSolrClient client, File outputFile)
        throws FileNotFoundException, IOException, SolrServerException {
    if (!config.getDryRun() && config.getDeleteAll()) {
        logger.info("delete all!");
        client.deleteByQuery("*:*");
    }
    logger.info("Reading " + config.getFileName());

    try (BufferedReader pw = new BufferedReader(new FileReader(outputFile))) {
        pw.lines().collect(StreamUtils.batchCollector(config.getBlockSize(), l -> {
            List<SolrInputDocument> collect = l.stream().map(App::json2SolrInputDocument)
                    .collect(Collectors.toList());
            try {
                if (!config.getDryRun()) {
                    logger.info("adding " + collect.size() + " documents (" + incrementCounter(collect.size())
                            + ")");
                    client.add(collect);
                }
            } catch (SolrServerException | IOException e) {
                throw new RuntimeException(e);
            }
        }));
    }

    if (!config.getDryRun()) {
        logger.info("Commit");
        client.commit();
    }

}

From source file:org.apache.ofbiz.solr.SolrProductSearch.java

License:Apache License

/**
 * Rebuilds the solr index./*from   w  w w . j a  v a2 s . com*/
 */
public static Map<String, Object> rebuildSolrIndex(DispatchContext dctx, Map<String, Object> context)
        throws GenericEntityException {
    HttpSolrClient client = null;
    Map<String, Object> result;
    GenericDelegator delegator = (GenericDelegator) dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    String solrIndexName = (String) context.get("indexName");

    Boolean treatConnectErrorNonFatal = (Boolean) context.get("treatConnectErrorNonFatal");

    try {
        client = SolrUtil.getInstance().getHttpSolrClient(solrIndexName);

        // now lets fetch all products
        List<Map<String, Object>> solrDocs = new ArrayList<Map<String, Object>>();
        List<GenericValue> products = delegator.findList("Product", null, null, null, null, true);
        int numDocs = 0;
        if (products != null) {
            numDocs = products.size();
        }

        Debug.logInfo("Solr: Clearing solr index and rebuilding with " + numDocs + " found products", module);

        Iterator<GenericValue> productIterator = products.iterator();
        while (productIterator.hasNext()) {
            GenericValue product = productIterator.next();
            Map<String, Object> dispatchContext = ProductUtil.getProductContent(product, dctx, context);
            solrDocs.add(dispatchContext);
        }

        // this removes everything from the index
        client.deleteByQuery("*:*");
        client.commit();

        // THis adds all products to the Index (instantly)
        Map<String, Object> runResult = dispatcher.runSync("addListToSolrIndex",
                UtilMisc.toMap("fieldList", solrDocs, "userLogin", userLogin, "locale", locale, "indexName",
                        solrIndexName, "treatConnectErrorNonFatal", treatConnectErrorNonFatal));

        String runMsg = ServiceUtil.getErrorMessage(runResult);
        if (UtilValidate.isEmpty(runMsg)) {
            runMsg = null;
        }
        if (ServiceUtil.isError(runResult)) {
            result = ServiceUtil.returnError(runMsg);
        } else if (ServiceUtil.isFailure(runResult)) {
            result = ServiceUtil.returnFailure(runMsg);
        } else {
            final String statusMsg = UtilProperties.getMessage(resource,
                    "SolrClearedSolrIndexAndReindexedDocuments", UtilMisc.toMap("numDocs", numDocs), locale);
            result = ServiceUtil.returnSuccess(statusMsg);
        }
    } catch (MalformedURLException e) {
        Debug.logError(e, e.getMessage(), module);
        result = ServiceUtil.returnError(e.toString());
    } catch (SolrServerException e) {
        if (e.getCause() != null && e.getCause() instanceof ConnectException) {
            final String statusStr = UtilProperties.getMessage(resource,
                    "SolrFailureConnectingToSolrServerToRebuildIndex", locale);
            if (Boolean.TRUE.equals(treatConnectErrorNonFatal)) {
                Debug.logWarning(e, "Solr: " + statusStr, module);
                result = ServiceUtil.returnFailure(statusStr);
            } else {
                Debug.logError(e, "Solr: " + statusStr, module);
                result = ServiceUtil.returnError(statusStr);
            }
        } else {
            Debug.logError(e, e.getMessage(), module);
            result = ServiceUtil.returnError(e.toString());
        }
    } catch (IOException e) {
        Debug.logError(e, e.getMessage(), module);
        result = ServiceUtil.returnError(e.toString());
    } catch (ServiceAuthException e) {
        Debug.logError(e, e.getMessage(), module);
        result = ServiceUtil.returnError(e.toString());
    } catch (ServiceValidationException e) {
        Debug.logError(e, e.getMessage(), module);
        result = ServiceUtil.returnError(e.toString());
    } catch (GenericServiceException e) {
        Debug.logError(e, e.getMessage(), module);
        result = ServiceUtil.returnError(e.toString());
    } catch (Exception e) {
        Debug.logError(e, e.getMessage(), module);
        result = ServiceUtil.returnError(e.toString());
    } finally {
        if (client != null) {
            try {
                client.close();
            } catch (IOException e) {
                // do nothing
            }
        }
    }
    return result;
}

From source file:org.zaizi.manifoldcf.agents.output.solrwrapper.SolrWrapperConnector.java

License:Open Source License

/**
 * Simply run a pruning query removing Solr Documents with 0 occurrences
 * //from w ww . j a va  2 s  . c  o  m
 * @param configuration
 * @throws IOException
 * @throws SolrServerException
 */
private void removeEmptyOccurrences(ConfigParams configuration) throws IOException, SolrServerException {
    HttpSolrClient httpSolrServer = this.getHttpSolrServer(configuration);
    httpSolrServer.deleteByQuery(OCCURRENCES_FIELD + ":" + 0);
    httpSolrServer.commit();
}