Search in sources :

Example 36 with SearchIndexException

use of org.opencastproject.matterhorn.search.SearchIndexException in project opencast by opencast.

the class AbstractSearchIndex method executeQuery.

/**
 * Execute a query on the index.
 *
 * @param query
 *          The query to use to find the results
 * @param requestBuilder
 *          The builder to use to create the query.
 * @param toSearchResult
 *          The function to convert the results to a {@link SearchResult}
 * @return A {@link SearchResult} containing the relevant objects.
 * @throws SearchIndexException
 */
protected <T> SearchResult<T> executeQuery(SearchQuery query, SearchRequestBuilder requestBuilder, Fn<SearchMetadataCollection, T> toSearchResult) throws SearchIndexException {
    // Execute the query and try to get hold of a query response
    SearchResponse response = null;
    try {
        response = getSearchClient().search(requestBuilder.request()).actionGet();
    } catch (Throwable t) {
        throw new SearchIndexException(t);
    }
    // Create and configure the query result
    long hits = response.getHits().getTotalHits();
    long size = response.getHits().getHits().length;
    SearchResultImpl<T> result = new SearchResultImpl<>(query, hits, size);
    result.setSearchTime(response.getTookInMillis());
    // Walk through response and create new items with title, creator, etc:
    for (SearchHit doc : response.getHits()) {
        // Wrap the search resulting metadata
        SearchMetadataCollection metadata = new SearchMetadataCollection(doc.getType());
        metadata.setIdentifier(doc.getId());
        for (SearchHitField field : doc.getFields().values()) {
            String name = field.getName();
            SearchMetadata<Object> m = new SearchMetadataImpl<>(name);
            // Add the field values
            if (field.getValues().size() > 1) {
                for (Object v : field.getValues()) {
                    m.addValue(v);
                }
            } else {
                m.addValue(field.getValue());
            }
            // Add the metadata
            metadata.add(m);
        }
        // Get the score for this item
        float score = doc.getScore();
        // item
        try {
            T document = toSearchResult.apply(metadata);
            SearchResultItem<T> item = new SearchResultItemImpl<>(score, document);
            result.addResultItem(item);
        } catch (Throwable t) {
            logger.warn("Error during search result serialization: '{}'. Skipping this search result.", t.getMessage());
            size--;
            continue;
        }
    }
    // Set the number of resulting documents
    result.setDocumentCount(size);
    return result;
}
Also used : SearchResultItemImpl(org.opencastproject.matterhorn.search.impl.SearchResultItemImpl) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchMetadataImpl(org.opencastproject.matterhorn.search.impl.SearchMetadataImpl) SearchMetadataCollection(org.opencastproject.matterhorn.search.impl.SearchMetadataCollection) SearchResultImpl(org.opencastproject.matterhorn.search.impl.SearchResultImpl) SearchHitField(org.elasticsearch.search.SearchHitField) IndexRecreateObject(org.opencastproject.message.broker.api.index.IndexRecreateObject)

Example 37 with SearchIndexException

use of org.opencastproject.matterhorn.search.SearchIndexException in project opencast by opencast.

the class IndexServiceImpl method updateCommonEventMetadata.

@Override
public MetadataList updateCommonEventMetadata(String id, String metadataJSON, AbstractSearchIndex index) throws IllegalArgumentException, IndexServiceException, SearchIndexException, NotFoundException, UnauthorizedException {
    MetadataList metadataList;
    try {
        metadataList = getMetadataListWithCommonEventCatalogUIAdapter();
        metadataList.fromJSON(metadataJSON);
    } catch (Exception e) {
        logger.warn("Not able to parse the event metadata {}: {}", metadataJSON, getStackTrace(e));
        throw new IllegalArgumentException("Not able to parse the event metadata " + metadataJSON, e);
    }
    return updateEventMetadata(id, metadataList, index);
}
Also used : MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) IngestException(org.opencastproject.ingest.api.IngestException) WebApplicationException(javax.ws.rs.WebApplicationException) MetadataParsingException(org.opencastproject.metadata.dublincore.MetadataParsingException) EventCommentException(org.opencastproject.event.comment.EventCommentException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) ParseException(java.text.ParseException) SeriesException(org.opencastproject.series.api.SeriesException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException)

Example 38 with SearchIndexException

use of org.opencastproject.matterhorn.search.SearchIndexException in project opencast by opencast.

the class SeriesMessageReceiverImpl method execute.

@Override
protected void execute(SeriesItem seriesItem) {
    Series series = null;
    String organization = getSecurityService().getOrganization().getId();
    User user = getSecurityService().getUser();
    switch(seriesItem.getType()) {
        case UpdateCatalog:
            logger.debug("Received Update Series for index {}", getSearchIndex().getIndexName());
            DublinCoreCatalog dc = seriesItem.getMetadata();
            String seriesId = dc.getFirst(DublinCoreCatalog.PROPERTY_IDENTIFIER);
            // Load or create the corresponding series
            try {
                series = SeriesIndexUtils.getOrCreate(seriesId, organization, user, getSearchIndex());
                series.setCreator(getSecurityService().getUser().getName());
                SeriesIndexUtils.updateSeries(series, dc);
            } catch (SearchIndexException e) {
                logger.error("Error retrieving series {} from the search index: {}", seriesId, ExceptionUtils.getStackTrace(e));
                return;
            }
            // Update the event series titles if they changed
            try {
                SeriesIndexUtils.updateEventSeriesTitles(series, organization, getSecurityService().getUser(), getSearchIndex());
            } catch (SearchIndexException e) {
                logger.error("Error updating the series name of series {} from the associated events: {}", series.getIdentifier(), ExceptionUtils.getStackTrace(e));
            }
            // Persist the series
            update(seriesItem.getSeriesId(), series);
            break;
        case UpdateAcl:
            logger.debug("Received Update Series ACL for index {}", getSearchIndex().getIndexName());
            // Load or create the corresponding series
            try {
                series = SeriesIndexUtils.getOrCreate(seriesItem.getSeriesId(), organization, user, getSearchIndex());
                List<ManagedAcl> acls = aclServiceFactory.serviceFor(getSecurityService().getOrganization()).getAcls();
                Option<ManagedAcl> managedAcl = AccessInformationUtil.matchAcls(acls, seriesItem.getAcl());
                if (managedAcl.isSome())
                    series.setManagedAcl(managedAcl.get().getName());
                series.setAccessPolicy(AccessControlParser.toJsonSilent(seriesItem.getAcl()));
            } catch (SearchIndexException e) {
                logger.error("Error retrieving series {} from the search index: {}", seriesItem.getSeriesId(), ExceptionUtils.getStackTrace(e));
                return;
            }
            // Persist the updated series
            update(seriesItem.getSeriesId(), series);
            break;
        case UpdateOptOut:
            logger.debug("Received update opt out status of series {} for index {}", seriesItem.getSeriesId(), getSearchIndex().getIndexName());
            // Load or create the corresponding series
            try {
                series = SeriesIndexUtils.getOrCreate(seriesItem.getSeriesId(), organization, user, getSearchIndex());
                series.setOptOut(seriesItem.getOptOut());
            } catch (SearchIndexException e) {
                logger.error("Error retrieving series {} from the search index: {}", seriesItem.getSeriesId(), ExceptionUtils.getStackTrace(e));
                return;
            }
            // Persist the updated series
            update(seriesItem.getSeriesId(), series);
            break;
        case UpdateProperty:
            logger.debug("Received update property of series {} for index {}", seriesItem.getSeriesId(), getSearchIndex().getIndexName());
            if (!THEME_PROPERTY_NAME.equals(seriesItem.getPropertyName()))
                break;
            // Load or create the corresponding series
            try {
                series = SeriesIndexUtils.getOrCreate(seriesItem.getSeriesId(), organization, user, getSearchIndex());
                series.setTheme(Opt.nul(seriesItem.getPropertyValue()).bind(Strings.toLong).orNull());
            } catch (SearchIndexException e) {
                logger.error("Error retrieving series {} from the search index: {}", seriesItem.getSeriesId(), ExceptionUtils.getStackTrace(e));
                return;
            }
            // Persist the updated series
            update(seriesItem.getSeriesId(), series);
            break;
        case Delete:
            logger.debug("Received Delete Series Event {} for index {}", seriesItem.getSeriesId(), getSearchIndex().getIndexName());
            // Remove the series from the search index
            try {
                getSearchIndex().delete(Series.DOCUMENT_TYPE, seriesItem.getSeriesId().concat(organization));
                logger.debug("Series {} removed from search index", seriesItem.getSeriesId());
            } catch (SearchIndexException e) {
                logger.error("Error deleting the series {} from the search index: {}", seriesItem.getSeriesId(), ExceptionUtils.getStackTrace(e));
                return;
            }
            return;
        case UpdateElement:
            // nothing to do
            break;
        default:
            throw new IllegalArgumentException("Unhandled type of SeriesItem");
    }
}
Also used : Series(org.opencastproject.index.service.impl.index.series.Series) User(org.opencastproject.security.api.User) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) ManagedAcl(org.opencastproject.authorization.xacml.manager.api.ManagedAcl) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog)

Example 39 with SearchIndexException

use of org.opencastproject.matterhorn.search.SearchIndexException in project opencast by opencast.

the class WorkflowMessageReceiverImpl method execute.

@Override
protected void execute(WorkflowItem workflowItem) {
    String organization = getSecurityService().getOrganization().getId();
    User user = getSecurityService().getUser();
    String eventId = null;
    switch(workflowItem.getType()) {
        case UpdateInstance:
            logger.debug("Received Update Workflow instance Entry for index {}", getSearchIndex().getIndexName());
            WorkflowInstance wf = workflowItem.getWorkflowInstance();
            MediaPackage mp = wf.getMediaPackage();
            eventId = mp.getIdentifier().toString();
            // Load or create the corresponding recording event
            Event event = null;
            try {
                event = getOrCreateEvent(eventId, organization, user, getSearchIndex());
                event.setCreator(getSecurityService().getUser().getName());
                event.setWorkflowId(wf.getId());
                event.setWorkflowDefinitionId(wf.getTemplate());
                event.setWorkflowState(wf.getState());
                WorkflowInstance.WorkflowState state = wf.getState();
                if (!(WorkflowInstance.WorkflowState.SUCCEEDED.equals(state) || WorkflowInstance.WorkflowState.FAILED.equals(state) || WorkflowInstance.WorkflowState.STOPPED.equals(state))) {
                    Tuple<AccessControlList, AclScope> activeAcl = authorizationService.getActiveAcl(mp);
                    List<ManagedAcl> acls = aclServiceFactory.serviceFor(getSecurityService().getOrganization()).getAcls();
                    Option<ManagedAcl> managedAcl = AccessInformationUtil.matchAcls(acls, activeAcl.getA());
                    if (managedAcl.isSome()) {
                        event.setManagedAcl(managedAcl.get().getName());
                    }
                    event.setAccessPolicy(AccessControlParser.toJsonSilent(activeAcl.getA()));
                    try {
                        Opt<DublinCoreCatalog> loadedDC = DublinCoreUtil.loadEpisodeDublinCore(workspace, mp);
                        if (loadedDC.isSome())
                            updateEvent(event, loadedDC.get());
                    } catch (Throwable t) {
                        logger.warn("Unable to load dublincore catalog for the workflow {}", wf.getId(), t);
                    }
                }
                updateEvent(event, mp);
            } catch (SearchIndexException e) {
                logger.error("Error retrieving the recording event from the search index: {}", e.getMessage());
                return;
            }
            // Update series name if not already done
            try {
                EventIndexUtils.updateSeriesName(event, organization, user, getSearchIndex());
            } catch (SearchIndexException e) {
                logger.error("Error updating the series name of the event to index: {}", ExceptionUtils.getStackTrace(e));
            }
            // Persist the scheduling event
            try {
                getSearchIndex().addOrUpdate(event);
                logger.debug("Workflow instance {} updated in the search index", event.getIdentifier());
            } catch (SearchIndexException e) {
                logger.error("Error retrieving the recording event from the search index: {}", e.getMessage());
                return;
            }
            return;
        case DeleteInstance:
            logger.debug("Received Delete Workflow instance Entry {}", eventId);
            eventId = workflowItem.getWorkflowInstance().getMediaPackage().getIdentifier().toString();
            // Remove the Workflow instance entry from the search index
            try {
                getSearchIndex().deleteWorkflow(organization, user, eventId, workflowItem.getWorkflowInstanceId());
                logger.debug("Workflow instance mediapackage {} removed from search index", eventId);
            } catch (NotFoundException e) {
                logger.warn("Workflow instance mediapackage {} not found for deletion", eventId);
            } catch (SearchIndexException e) {
                logger.error("Error deleting the Workflow instance entry {} from the search index: {}", eventId, ExceptionUtils.getStackTrace(e));
            }
            return;
        case AddDefinition:
            // TODO: Update the index with it as soon as the definition are part of it
            return;
        case DeleteDefinition:
            // TODO: Update the index with it as soon as the definition are part of it
            return;
        default:
            throw new IllegalArgumentException("Unhandled type of WorkflowItem");
    }
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) User(org.opencastproject.security.api.User) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) ManagedAcl(org.opencastproject.authorization.xacml.manager.api.ManagedAcl) NotFoundException(org.opencastproject.util.NotFoundException) AclScope(org.opencastproject.security.api.AclScope) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Event(org.opencastproject.index.service.impl.index.event.Event) EventIndexUtils.getOrCreateEvent(org.opencastproject.index.service.impl.index.event.EventIndexUtils.getOrCreateEvent) EventIndexUtils.updateEvent(org.opencastproject.index.service.impl.index.event.EventIndexUtils.updateEvent) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog)

Example 40 with SearchIndexException

use of org.opencastproject.matterhorn.search.SearchIndexException in project opencast by opencast.

the class ThemesListProvider method getList.

@Override
public Map<String, String> getList(String listName, ResourceListQuery query, Organization organization) throws ListProviderException {
    Map<String, String> list = new HashMap<String, String>();
    if (NAME.equals(listName)) {
        ThemeSearchQuery themeQuery = new ThemeSearchQuery(securityService.getOrganization().getId(), securityService.getUser());
        themeQuery.withOffset(query.getOffset().getOrElse(0));
        int limit = query.getLimit().getOrElse(Integer.MAX_VALUE - themeQuery.getOffset());
        themeQuery.withLimit(limit);
        themeQuery.sortByName(SearchQuery.Order.Ascending);
        SearchResult<Theme> results = null;
        try {
            results = searchIndex.getByQuery(themeQuery);
        } catch (SearchIndexException e) {
            logger.error("The admin UI Search Index was not able to get the themes: {}", ExceptionUtils.getStackTrace(e));
            throw new ListProviderException("No themes list for list name " + listName + " found!");
        }
        for (SearchResultItem<Theme> item : results.getItems()) {
            Theme theme = item.getSource();
            list.put(Long.toString(theme.getIdentifier()), theme.getName());
        }
    } else if (DESCRIPTION.equals(listName)) {
        ThemeSearchQuery themeQuery = new ThemeSearchQuery(securityService.getOrganization().getId(), securityService.getUser());
        themeQuery.withOffset(query.getOffset().getOrElse(0));
        int limit = query.getLimit().getOrElse(Integer.MAX_VALUE - themeQuery.getOffset());
        themeQuery.withLimit(limit);
        themeQuery.sortByName(SearchQuery.Order.Ascending);
        SearchResult<Theme> results = null;
        try {
            results = searchIndex.getByQuery(themeQuery);
        } catch (SearchIndexException e) {
            logger.error("The admin UI Search Index was not able to get the themes: {}", ExceptionUtils.getStackTrace(e));
            throw new ListProviderException("No themes list for list name " + listName + " found!");
        }
        for (SearchResultItem<Theme> item : results.getItems()) {
            Theme theme = item.getSource();
            if (theme.getDescription() == null) {
                theme.setDescription("");
            } else {
                theme.getDescription();
            }
            list.put(Long.toString(theme.getIdentifier()), theme.getDescription());
        }
    }
    return list;
}
Also used : SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) HashMap(java.util.HashMap) ThemeSearchQuery(org.opencastproject.index.service.impl.index.theme.ThemeSearchQuery) SearchResultItem(org.opencastproject.matterhorn.search.SearchResultItem) Theme(org.opencastproject.index.service.impl.index.theme.Theme) ListProviderException(org.opencastproject.index.service.exception.ListProviderException) SearchResult(org.opencastproject.matterhorn.search.SearchResult)

Aggregations

SearchIndexException (org.opencastproject.matterhorn.search.SearchIndexException)43 NotFoundException (org.opencastproject.util.NotFoundException)18 WebApplicationException (javax.ws.rs.WebApplicationException)15 Path (javax.ws.rs.Path)14 RestQuery (org.opencastproject.util.doc.rest.RestQuery)14 Event (org.opencastproject.index.service.impl.index.event.Event)11 IOException (java.io.IOException)10 IndexServiceException (org.opencastproject.index.service.exception.IndexServiceException)10 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)10 SearchMetadataCollection (org.opencastproject.matterhorn.search.impl.SearchMetadataCollection)9 Produces (javax.ws.rs.Produces)8 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)8 User (org.opencastproject.security.api.User)8 GET (javax.ws.rs.GET)7 ParseException (java.text.ParseException)6 ArrayList (java.util.ArrayList)6 JSONException (org.codehaus.jettison.json.JSONException)6 EventCommentException (org.opencastproject.event.comment.EventCommentException)6 Series (org.opencastproject.index.service.impl.index.series.Series)6 Theme (org.opencastproject.index.service.impl.index.theme.Theme)6