Search in sources :

Example 56 with SolrServerException

use of org.apache.solr.client.solrj.SolrServerException in project vind by RBMHTechnology.

the class SolrSearchServer method commit.

@Override
public void commit(boolean optimize) {
    try {
        solrClientLogger.debug(">>> commit()");
        this.solrClient.commit();
        if (optimize) {
            solrClientLogger.debug(">>> optimize()");
            this.solrClient.optimize();
        }
    } catch (SolrServerException | IOException e) {
        log.error("Cannot commit", e);
        throw new SearchServerException("Cannot commit", e);
    }
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) SearchServerException(com.rbmhtechnology.vind.SearchServerException)

Example 57 with SolrServerException

use of org.apache.solr.client.solrj.SolrServerException in project opencast by opencast.

the class SeriesServiceSolrIndex method getSolrDocumentByID.

/**
 * Returns SolrDocument corresponding to given ID or null if such document does not exist.
 *
 * @param id
 *          SolrDocument ID
 * @return corresponding document
 * @throws SeriesServiceDatabaseException
 *           if exception occurred
 */
protected SolrDocument getSolrDocumentByID(String id) throws SeriesServiceDatabaseException {
    String orgId = securityService.getOrganization().getId();
    StringBuilder solrQueryString = new StringBuilder(SolrFields.COMPOSITE_ID_KEY).append(":").append(ClientUtils.escapeQueryChars(getCompositeKey(id, orgId)));
    SolrQuery q = new SolrQuery(solrQueryString.toString());
    QueryResponse response;
    try {
        response = solrServer.query(q);
        if (response.getResults().isEmpty()) {
            return null;
        } else {
            return response.getResults().get(0);
        }
    } catch (SolrServerException e) {
        logger.error("Could not perform series retrieval:", e);
        throw new SeriesServiceDatabaseException(e);
    }
}
Also used : SeriesServiceDatabaseException(org.opencastproject.series.impl.SeriesServiceDatabaseException) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 58 with SolrServerException

use of org.apache.solr.client.solrj.SolrServerException in project opencast by opencast.

the class SolrIndexManager method delete.

/**
 * Removes the entry with the given <code>id</code> from the database. The entry can either be a series or an episode.
 *
 * @param id
 *          identifier of the series or episode to delete
 * @param deletionDate
 *          the deletion date
 * @throws SolrServerException
 *           if an errors occurs while talking to solr
 */
public boolean delete(String id, Date deletionDate) throws SolrServerException {
    try {
        // Load the existing episode
        QueryResponse solrResponse = null;
        try {
            SolrQuery query = new SolrQuery(Schema.ID + ":" + ClientUtils.escapeQueryChars(id) + " AND -" + Schema.OC_DELETED + ":[* TO *]");
            solrResponse = solrServer.query(query);
        } catch (Exception e1) {
            throw new SolrServerException(e1);
        }
        // Did we find the episode?
        if (solrResponse.getResults().size() == 0) {
            logger.warn("Trying to delete non-existing media package {} from the search index", id);
            return false;
        }
        // Use all existing fields
        SolrDocument doc = solrResponse.getResults().get(0);
        SolrInputDocument inputDocument = new SolrInputDocument();
        for (String field : doc.getFieldNames()) {
            inputDocument.setField(field, doc.get(field));
        }
        // Set the oc_deleted field to the current date, then update
        Schema.setOcDeleted(inputDocument, deletionDate);
        solrServer.add(inputDocument);
        solrServer.commit();
        return true;
    } catch (IOException e) {
        throw new SolrServerException(e);
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SeriesException(org.opencastproject.series.api.SeriesException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) SearchServiceDatabaseException(org.opencastproject.search.impl.persistence.SearchServiceDatabaseException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException)

Example 59 with SolrServerException

use of org.apache.solr.client.solrj.SolrServerException in project opencast by opencast.

the class SolrRequester method createSearchResult.

/**
 * Creates a search result from a given solr response.
 *
 * @param query
 *          The solr query.
 * @return The search result.
 * @throws SolrServerException
 *           if the solr server is not working as expected
 */
private SearchResult createSearchResult(final SolrQuery query) throws SolrServerException {
    // Execute the query and try to get hold of a query response
    QueryResponse solrResponse = null;
    try {
        solrResponse = solrServer.query(query);
    } catch (Exception e) {
        throw new SolrServerException(e);
    }
    // Create and configure the query result
    final SearchResultImpl result = new SearchResultImpl(query.getQuery());
    result.setSearchTime(solrResponse.getQTime());
    result.setOffset(solrResponse.getResults().getStart());
    result.setLimit(solrResponse.getResults().size());
    result.setTotal(solrResponse.getResults().getNumFound());
    // Walk through response and create new items with title, creator, etc:
    for (final SolrDocument doc : solrResponse.getResults()) {
        final SearchResultItemImpl item = SearchResultItemImpl.fill(new SearchResultItem() {

            private final String dfltString = null;

            @Override
            public String getId() {
                return Schema.getId(doc);
            }

            /**
             * {@inheritDoc}
             *
             * @see org.opencastproject.search.api.SearchResultItem#getOrganization()
             */
            @Override
            public String getOrganization() {
                return Schema.getOrganization(doc);
            }

            @Override
            public MediaPackage getMediaPackage() {
                MediaPackageBuilder builder = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder();
                if (serializer != null)
                    builder.setSerializer(serializer);
                String mediaPackageFieldValue = Schema.getOcMediapackage(doc);
                if (mediaPackageFieldValue != null) {
                    try {
                        return builder.loadFromXml(mediaPackageFieldValue);
                    } catch (Exception e) {
                        logger.warn("Unable to read media package from search result", e);
                    }
                }
                return null;
            }

            @Override
            public long getDcExtent() {
                if (getType().equals(SearchResultItemType.AudioVisual)) {
                    Long extent = Schema.getDcExtent(doc);
                    if (extent != null)
                        return extent;
                }
                return -1;
            }

            @Override
            public String getDcTitle() {
                final List<DField<String>> titles = Schema.getDcTitle(doc);
                // try to return the first title without any language information first...
                return head(filter(titles, new Predicate<DField<String>>() {

                    @Override
                    public Boolean apply(DField<String> f) {
                        return f.getSuffix().equals(Schema.LANGUAGE_UNDEFINED);
                    }
                })).map(new Function<DField<String>, String>() {

                    @Override
                    public String apply(DField<String> f) {
                        return f.getValue();
                    }
                }).getOrElse(new Function0<String>() {

                    @Override
                    public String apply() {
                        // ... since none is present return the first arbitrary title
                        return Schema.getFirst(titles, dfltString);
                    }
                });
            }

            @Override
            public String getDcSubject() {
                return Schema.getFirst(Schema.getDcSubject(doc), dfltString);
            }

            @Override
            public String getDcDescription() {
                return Schema.getFirst(Schema.getDcDescription(doc), dfltString);
            }

            @Override
            public String getDcCreator() {
                return Schema.getFirst(Schema.getDcCreator(doc), dfltString);
            }

            @Override
            public String getDcPublisher() {
                return Schema.getFirst(Schema.getDcPublisher(doc), dfltString);
            }

            @Override
            public String getDcContributor() {
                return Schema.getFirst(Schema.getDcContributor(doc), dfltString);
            }

            @Override
            public String getDcAbstract() {
                return null;
            }

            @Override
            public Date getDcCreated() {
                return Schema.getDcCreated(doc);
            }

            @Override
            public Date getDcAvailableFrom() {
                return Schema.getDcAvailableFrom(doc);
            }

            @Override
            public Date getDcAvailableTo() {
                return Schema.getDcAvailableTo(doc);
            }

            @Override
            public String getDcLanguage() {
                return Schema.getDcLanguage(doc);
            }

            @Override
            public String getDcRightsHolder() {
                return Schema.getFirst(Schema.getDcRightsHolder(doc), dfltString);
            }

            @Override
            public String getDcSpatial() {
                return Schema.getFirst(Schema.getDcSpatial(doc), dfltString);
            }

            @Override
            public String getDcTemporal() {
                return null;
            }

            @Override
            public String getDcIsPartOf() {
                return Schema.getDcIsPartOf(doc);
            }

            @Override
            public String getDcReplaces() {
                return Schema.getDcReplaces(doc);
            }

            @Override
            public String getDcType() {
                return Schema.getDcType(doc);
            }

            @Override
            public String getDcAccessRights() {
                return Schema.getFirst(Schema.getDcAccessRights(doc), dfltString);
            }

            @Override
            public String getDcLicense() {
                return Schema.getFirst(Schema.getDcLicense(doc), dfltString);
            }

            @Override
            public String getOcMediapackage() {
                return Schema.getOcMediapackage(doc);
            }

            @Override
            public SearchResultItemType getType() {
                String t = Schema.getOcMediatype(doc);
                return t != null ? SearchResultItemType.valueOf(t) : null;
            }

            @Override
            public String[] getKeywords() {
                if (getType().equals(SearchResultItemType.AudioVisual)) {
                    String k = Schema.getOcKeywords(doc);
                    return k != null ? k.split(" ") : new String[0];
                } else
                    return new String[0];
            }

            @Override
            public String getCover() {
                return Schema.getOcCover(doc);
            }

            @Override
            public Date getModified() {
                return Schema.getOcModified(doc);
            }

            @Override
            public double getScore() {
                return Schema.getScore(doc);
            }

            @Override
            public MediaSegment[] getSegments() {
                if (SearchResultItemType.AudioVisual.equals(getType()))
                    return createSearchResultSegments(doc, query).toArray(new MediaSegmentImpl[0]);
                else
                    return new MediaSegmentImpl[0];
            }
        });
        // Add the item to the result set
        result.addItem(item);
    }
    return result;
}
Also used : SearchResultItemImpl(org.opencastproject.search.api.SearchResultItemImpl) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SearchResultItem(org.opencastproject.search.api.SearchResultItem) Function0(org.opencastproject.util.data.Function0) SearchResultItemType(org.opencastproject.search.api.SearchResultItem.SearchResultItemType) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) Date(java.util.Date) SolrDocument(org.apache.solr.common.SolrDocument) MediaPackageBuilder(org.opencastproject.mediapackage.MediaPackageBuilder) SearchResultImpl(org.opencastproject.search.api.SearchResultImpl) MediaSegmentImpl(org.opencastproject.search.api.MediaSegmentImpl) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) MediaPackage(org.opencastproject.mediapackage.MediaPackage) ArrayList(java.util.ArrayList) List(java.util.List)

Example 60 with SolrServerException

use of org.apache.solr.client.solrj.SolrServerException in project opencast by opencast.

the class WorkflowServiceSolrIndex method getStatistics.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.impl.WorkflowServiceIndex#getStatistics()
 */
@Override
public WorkflowStatistics getStatistics() throws WorkflowDatabaseException {
    long total = 0;
    long paused = 0;
    long failed = 0;
    long failing = 0;
    long instantiated = 0;
    long running = 0;
    long stopped = 0;
    long succeeded = 0;
    WorkflowStatistics stats = new WorkflowStatistics();
    // Get all definitions and then query for the numbers and the current operation per definition
    try {
        String orgId = securityService.getOrganization().getId();
        StringBuilder queryString = new StringBuilder().append(ORG_KEY).append(":").append(escapeQueryChars(orgId));
        appendSolrAuthFragment(queryString, Permissions.Action.WRITE.toString());
        SolrQuery solrQuery = new SolrQuery(queryString.toString());
        solrQuery.addFacetField(WORKFLOW_DEFINITION_KEY);
        solrQuery.addFacetField(OPERATION_KEY);
        solrQuery.setFacetMinCount(0);
        solrQuery.setFacet(true);
        QueryResponse response = solrServer.query(solrQuery);
        FacetField templateFacet = response.getFacetField(WORKFLOW_DEFINITION_KEY);
        FacetField operationFacet = response.getFacetField(OPERATION_KEY);
        // For every template and every operation
        if (templateFacet != null && templateFacet.getValues() != null) {
            for (Count template : templateFacet.getValues()) {
                WorkflowDefinitionReport templateReport = new WorkflowDefinitionReport();
                templateReport.setId(template.getName());
                long templateTotal = 0;
                long templatePaused = 0;
                long templateFailed = 0;
                long templateFailing = 0;
                long templateInstantiated = 0;
                long templateRunning = 0;
                long templateStopped = 0;
                long templateSucceeded = 0;
                if (operationFacet != null && operationFacet.getValues() != null) {
                    for (Count operation : operationFacet.getValues()) {
                        OperationReport operationReport = new OperationReport();
                        operationReport.setId(operation.getName());
                        StringBuilder baseSolrQuery = new StringBuilder().append(ORG_KEY).append(":").append(escapeQueryChars(orgId));
                        appendSolrAuthFragment(baseSolrQuery, Permissions.Action.WRITE.toString());
                        solrQuery = new SolrQuery(baseSolrQuery.toString());
                        solrQuery.addFacetField(STATE_KEY);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.FAILED);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.FAILING);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.INSTANTIATED);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.PAUSED);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.RUNNING);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.STOPPED);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.SUCCEEDED);
                        solrQuery.addFilterQuery(WORKFLOW_DEFINITION_KEY + ":" + template.getName());
                        solrQuery.addFilterQuery(OPERATION_KEY + ":" + operation.getName());
                        solrQuery.setFacetMinCount(0);
                        solrQuery.setFacet(true);
                        response = solrServer.query(solrQuery);
                        // Add the states
                        FacetField stateFacet = response.getFacetField(STATE_KEY);
                        for (Count stateValue : stateFacet.getValues()) {
                            WorkflowState state = WorkflowState.valueOf(stateValue.getName().toUpperCase());
                            templateTotal += stateValue.getCount();
                            total += stateValue.getCount();
                            switch(state) {
                                case FAILED:
                                    operationReport.setFailed(stateValue.getCount());
                                    templateFailed += stateValue.getCount();
                                    failed += stateValue.getCount();
                                    break;
                                case FAILING:
                                    operationReport.setFailing(stateValue.getCount());
                                    templateFailing += stateValue.getCount();
                                    failing += stateValue.getCount();
                                    break;
                                case INSTANTIATED:
                                    operationReport.setInstantiated(stateValue.getCount());
                                    templateInstantiated += stateValue.getCount();
                                    instantiated += stateValue.getCount();
                                    break;
                                case PAUSED:
                                    operationReport.setPaused(stateValue.getCount());
                                    templatePaused += stateValue.getCount();
                                    paused += stateValue.getCount();
                                    break;
                                case RUNNING:
                                    operationReport.setRunning(stateValue.getCount());
                                    templateRunning += stateValue.getCount();
                                    running += stateValue.getCount();
                                    break;
                                case STOPPED:
                                    operationReport.setStopped(stateValue.getCount());
                                    templateStopped += stateValue.getCount();
                                    stopped += stateValue.getCount();
                                    break;
                                case SUCCEEDED:
                                    operationReport.setFinished(stateValue.getCount());
                                    templateSucceeded += stateValue.getCount();
                                    succeeded += stateValue.getCount();
                                    break;
                                default:
                                    throw new IllegalStateException("State '" + state + "' is not handled");
                            }
                        }
                        templateReport.getOperations().add(operationReport);
                    }
                }
                // Update the template statistics
                templateReport.setTotal(templateTotal);
                templateReport.setFailed(templateFailed);
                templateReport.setFailing(templateFailing);
                templateReport.setInstantiated(templateInstantiated);
                templateReport.setPaused(templatePaused);
                templateReport.setRunning(templateRunning);
                templateReport.setStopped(templateStopped);
                templateReport.setFinished(templateSucceeded);
                // Add the definition report to the statistics
                stats.getDefinitions().add(templateReport);
            }
        }
    } catch (SolrServerException e) {
        throw new WorkflowDatabaseException(e);
    }
    stats.setTotal(total);
    stats.setFailed(failed);
    stats.setFailing(failing);
    stats.setInstantiated(instantiated);
    stats.setPaused(paused);
    stats.setRunning(running);
    stats.setStopped(stopped);
    stats.setFinished(succeeded);
    return stats;
}
Also used : WorkflowDefinitionReport(org.opencastproject.workflow.api.WorkflowStatistics.WorkflowDefinitionReport) SolrServerException(org.apache.solr.client.solrj.SolrServerException) FacetField(org.apache.solr.client.solrj.response.FacetField) Count(org.apache.solr.client.solrj.response.FacetField.Count) WorkflowStatistics(org.opencastproject.workflow.api.WorkflowStatistics) SolrQuery(org.apache.solr.client.solrj.SolrQuery) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowState(org.opencastproject.workflow.api.WorkflowInstance.WorkflowState) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) OperationReport(org.opencastproject.workflow.api.WorkflowStatistics.WorkflowDefinitionReport.OperationReport)

Aggregations

SolrServerException (org.apache.solr.client.solrj.SolrServerException)281 IOException (java.io.IOException)210 SolrQuery (org.apache.solr.client.solrj.SolrQuery)101 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)97 ArrayList (java.util.ArrayList)58 SolrException (org.apache.solr.common.SolrException)57 SolrDocument (org.apache.solr.common.SolrDocument)55 SolrInputDocument (org.apache.solr.common.SolrInputDocument)50 SolrDocumentList (org.apache.solr.common.SolrDocumentList)44 HashMap (java.util.HashMap)30 Map (java.util.Map)29 List (java.util.List)27 UpdateResponse (org.apache.solr.client.solrj.response.UpdateResponse)26 SolrClient (org.apache.solr.client.solrj.SolrClient)23 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)23 NamedList (org.apache.solr.common.util.NamedList)22 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)19 Date (java.util.Date)18 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)17 SolrParams (org.apache.solr.common.params.SolrParams)13