Search in sources :

Example 1 with MCRSolrIndexStatistic

use of org.mycore.solr.index.statistic.MCRSolrIndexStatistic in project mycore by MyCoRe-Org.

the class MCRSolrIndexer method deleteDerivate.

/**
 * Convenient method to delete a derivate and all its files at once.
 *
 * @param id the derivate id
 * @return the solr response
 */
public static UpdateResponse deleteDerivate(String id) {
    if (id == null) {
        return null;
    }
    SolrClient solrClient = MCRSolrClientFactory.getSolrClient();
    UpdateResponse updateResponse = null;
    long start = System.currentTimeMillis();
    try {
        LOGGER.debug("Deleting derivate \"{}\" from solr", id);
        UpdateRequest req = new UpdateRequest();
        StringBuilder deleteQuery = new StringBuilder();
        deleteQuery.append("id:").append(id).append(" ");
        deleteQuery.append("derivateID:").append(id);
        if (MCRSolrUtils.useNestedDocuments()) {
            deleteQuery.append(" ").append("_root_:").append(id);
        }
        req.deleteByQuery(deleteQuery.toString());
        updateResponse = req.process(solrClient);
        solrClient.commit();
    } catch (Exception e) {
        LOGGER.error("Error deleting document from solr", e);
    }
    long end = System.currentTimeMillis();
    MCRSolrIndexStatistic operations = MCRSolrIndexStatisticCollector.OPERATIONS;
    operations.addDocument(1);
    operations.addTime(end - start);
    return updateResponse;
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) MCRSolrIndexStatistic(org.mycore.solr.index.statistic.MCRSolrIndexStatistic) SolrClient(org.apache.solr.client.solrj.SolrClient) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException)

Example 2 with MCRSolrIndexStatistic

use of org.mycore.solr.index.statistic.MCRSolrIndexStatistic in project mycore by MyCoRe-Org.

the class MCRSolrIndexer method deleteById.

/**
 * Deletes a list of documents by unique ID. Also removes any nested document of that ID.
 *
 * @param solrIDs
 *            the list of solr document IDs to delete
 */
public static UpdateResponse deleteById(String... solrIDs) {
    if (solrIDs == null || solrIDs.length == 0) {
        return null;
    }
    SolrClient solrClient = MCRSolrClientFactory.getSolrClient();
    UpdateResponse updateResponse = null;
    long start = System.currentTimeMillis();
    try {
        LOGGER.debug("Deleting \"{}\" from solr", Arrays.asList(solrIDs));
        UpdateRequest req = new UpdateRequest();
        // delete all documents rooted at this id
        if (MCRSolrUtils.useNestedDocuments()) {
            StringBuilder deleteQuery = new StringBuilder("_root_:(");
            for (String solrID : solrIDs) {
                deleteQuery.append('"');
                deleteQuery.append(MCRSolrUtils.escapeSearchValue(solrID));
                deleteQuery.append("\" ");
            }
            deleteQuery.setCharAt(deleteQuery.length() - 1, ')');
            req.deleteByQuery(deleteQuery.toString());
        }
        // for document without nested
        req.deleteById(Arrays.asList(solrIDs));
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Delete request: {}", req.getXML());
        }
        updateResponse = req.process(solrClient);
        solrClient.commit();
    } catch (Exception e) {
        LOGGER.error("Error deleting document from solr", e);
    }
    long end = System.currentTimeMillis();
    MCRSolrIndexStatistic operations = MCRSolrIndexStatisticCollector.OPERATIONS;
    operations.addDocument(1);
    operations.addTime(end - start);
    return updateResponse;
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) MCRSolrIndexStatistic(org.mycore.solr.index.statistic.MCRSolrIndexStatistic) SolrClient(org.apache.solr.client.solrj.SolrClient) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException)

Example 3 with MCRSolrIndexStatistic

use of org.mycore.solr.index.statistic.MCRSolrIndexStatistic in project mycore by MyCoRe-Org.

the class MCRSolrIndexer method rebuildMetadataIndex.

/**
 * Rebuilds solr's metadata index.
 *
 * @param list
 *            list of identifiers of the objects to index
 * @param solrClient
 *            solr server to index
 */
public static void rebuildMetadataIndex(List<String> list, SolrClient solrClient) {
    LOGGER.info("Re-building Metadata Index");
    if (list.isEmpty()) {
        LOGGER.info("Sorry, no documents to index");
        return;
    }
    StopWatch swatch = new StopWatch();
    swatch.start();
    int totalCount = list.size();
    LOGGER.info("Sending {} objects to solr for reindexing", totalCount);
    MCRXMLMetadataManager metadataMgr = MCRXMLMetadataManager.instance();
    MCRSolrIndexStatistic statistic = null;
    HashMap<MCRObjectID, MCRContent> contentMap = new HashMap<>((int) (BULK_SIZE * 1.4));
    int i = 0;
    for (String id : list) {
        i++;
        try {
            LOGGER.debug("Preparing \"{}\" for indexing", id);
            MCRObjectID objId = MCRObjectID.getInstance(id);
            MCRContent content = metadataMgr.retrieveContent(objId);
            contentMap.put(objId, content);
            if (i % BULK_SIZE == 0 || totalCount == i) {
                MCRSolrIndexHandler indexHandler = MCRSolrIndexHandlerFactory.getInstance().getIndexHandler(contentMap);
                indexHandler.setCommitWithin(BATCH_AUTO_COMMIT_WITHIN_MS);
                indexHandler.setSolrServer(solrClient);
                statistic = indexHandler.getStatistic();
                submitIndexHandler(indexHandler);
                contentMap = new HashMap<>((int) (BULK_SIZE * 1.4));
            }
        } catch (Exception ex) {
            LOGGER.error("Error creating index thread for object {}", id, ex);
        }
    }
    long durationInMilliSeconds = swatch.getTime();
    if (statistic != null) {
        statistic.addTime(durationInMilliSeconds);
    }
}
Also used : MCRSolrIndexStatistic(org.mycore.solr.index.statistic.MCRSolrIndexStatistic) HashMap(java.util.HashMap) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRContent(org.mycore.common.content.MCRContent) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) StopWatch(org.apache.commons.lang.time.StopWatch) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Aggregations

IOException (java.io.IOException)3 SolrServerException (org.apache.solr.client.solrj.SolrServerException)3 MCRSolrIndexStatistic (org.mycore.solr.index.statistic.MCRSolrIndexStatistic)3 SolrClient (org.apache.solr.client.solrj.SolrClient)2 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)2 UpdateResponse (org.apache.solr.client.solrj.response.UpdateResponse)2 HashMap (java.util.HashMap)1 StopWatch (org.apache.commons.lang.time.StopWatch)1 MCRContent (org.mycore.common.content.MCRContent)1 MCRXMLMetadataManager (org.mycore.datamodel.common.MCRXMLMetadataManager)1 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)1