Search in sources :

Example 6 with SearchMetadataCollection

use of org.opencastproject.matterhorn.search.impl.SearchMetadataCollection in project opencast by opencast.

the class AbstractSearchIndex method getByQuery.

/**
 * @param query
 *          The query to use to retrieve the events that match the query
 * @return {@link SearchResult} collection of {@link Event} from a query.
 * @throws SearchIndexException
 *           Thrown if there is an error getting the results.
 */
public SearchResult<Event> getByQuery(EventSearchQuery query) throws SearchIndexException {
    logger.debug("Searching index using event query '{}'", query);
    // Create the request builder
    SearchRequestBuilder requestBuilder = getSearchRequestBuilder(query, new EventQueryBuilder(query));
    try {
        return executeQuery(query, requestBuilder, new Fn<SearchMetadataCollection, Event>() {

            @Override
            public Event apply(SearchMetadataCollection metadata) {
                try {
                    return EventIndexUtils.toRecordingEvent(metadata);
                } catch (IOException e) {
                    return chuck(e);
                }
            }
        });
    } catch (Throwable t) {
        throw new SearchIndexException("Error querying event index", t);
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchMetadataCollection(org.opencastproject.matterhorn.search.impl.SearchMetadataCollection) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) EventQueryBuilder(org.opencastproject.index.service.impl.index.event.EventQueryBuilder) Event(org.opencastproject.index.service.impl.index.event.Event) IOException(java.io.IOException)

Example 7 with SearchMetadataCollection

use of org.opencastproject.matterhorn.search.impl.SearchMetadataCollection in project opencast by opencast.

the class AbstractSearchIndex method getByQuery.

/**
 * @param query
 *          The query to use to retrieve the series that match the query
 * @return {@link SearchResult} collection of {@link Series} from a query.
 * @throws SearchIndexException
 *           Thrown if there is an error getting the results.
 */
public SearchResult<Series> getByQuery(SeriesSearchQuery query) throws SearchIndexException {
    logger.debug("Searching index using series query '{}'", query);
    // Create the request builder
    SearchRequestBuilder requestBuilder = getSearchRequestBuilder(query, new SeriesQueryBuilder(query));
    try {
        return executeQuery(query, requestBuilder, new Fn<SearchMetadataCollection, Series>() {

            @Override
            public Series apply(SearchMetadataCollection metadata) {
                try {
                    return SeriesIndexUtils.toSeries(metadata);
                } catch (IOException e) {
                    return chuck(e);
                }
            }
        });
    } catch (Throwable t) {
        throw new SearchIndexException("Error querying series index", t);
    }
}
Also used : Series(org.opencastproject.index.service.impl.index.series.Series) SeriesQueryBuilder(org.opencastproject.index.service.impl.index.series.SeriesQueryBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchMetadataCollection(org.opencastproject.matterhorn.search.impl.SearchMetadataCollection) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) IOException(java.io.IOException)

Example 8 with SearchMetadataCollection

use of org.opencastproject.matterhorn.search.impl.SearchMetadataCollection in project opencast by opencast.

the class AbstractSearchIndex method addOrUpdate.

/**
 * Adds the recording event to the search index or updates it accordingly if it is there.
 *
 * @param event
 *          the recording event
 * @throws SearchIndexException
 *           if the event cannot be added or updated
 */
public void addOrUpdate(Event event) throws SearchIndexException {
    logger.debug("Adding resource {} to search index", event);
    // if (!preparedIndices.contains(resource.getURI().getSite().getIdentifier())) {
    // try {
    // createIndex(resource.getURI().getSite());
    // } catch (IOException e) {
    // throw new SearchIndexException(e);
    // }
    // }
    // Add the resource to the index
    SearchMetadataCollection inputDocument = EventIndexUtils.toSearchMetadata(event);
    List<SearchMetadata<?>> resourceMetadata = inputDocument.getMetadata();
    ElasticsearchDocument doc = new ElasticsearchDocument(inputDocument.getIdentifier(), inputDocument.getDocumentType(), resourceMetadata);
    try {
        update(doc);
    } catch (Throwable t) {
        throw new SearchIndexException("Cannot write resource " + event + " to index", t);
    }
}
Also used : SearchMetadataCollection(org.opencastproject.matterhorn.search.impl.SearchMetadataCollection) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) SearchMetadata(org.opencastproject.matterhorn.search.SearchMetadata) ElasticsearchDocument(org.opencastproject.matterhorn.search.impl.ElasticsearchDocument)

Example 9 with SearchMetadataCollection

use of org.opencastproject.matterhorn.search.impl.SearchMetadataCollection in project opencast by opencast.

the class AbstractSearchIndex method addOrUpdate.

/**
 * Adds or updates the group in the search index.
 *
 * @param group
 *          The group to add
 * @throws SearchIndexException
 *           Thrown if unable to add or update the group.
 */
public void addOrUpdate(Group group) throws SearchIndexException {
    logger.debug("Adding resource {} to search index", group);
    // if (!preparedIndices.contains(resource.getURI().getSite().getIdentifier())) {
    // try {
    // createIndex(resource.getURI().getSite());
    // } catch (IOException e) {
    // throw new SearchIndexException(e);
    // }
    // }
    // Add the resource to the index
    SearchMetadataCollection inputDocument = GroupIndexUtils.toSearchMetadata(group);
    List<SearchMetadata<?>> resourceMetadata = inputDocument.getMetadata();
    ElasticsearchDocument doc = new ElasticsearchDocument(inputDocument.getIdentifier(), inputDocument.getDocumentType(), resourceMetadata);
    try {
        update(doc);
    } catch (Throwable t) {
        throw new SearchIndexException("Cannot write resource " + group + " to index", t);
    }
}
Also used : SearchMetadataCollection(org.opencastproject.matterhorn.search.impl.SearchMetadataCollection) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) SearchMetadata(org.opencastproject.matterhorn.search.SearchMetadata) ElasticsearchDocument(org.opencastproject.matterhorn.search.impl.ElasticsearchDocument)

Example 10 with SearchMetadataCollection

use of org.opencastproject.matterhorn.search.impl.SearchMetadataCollection 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)

Aggregations

SearchMetadataCollection (org.opencastproject.matterhorn.search.impl.SearchMetadataCollection)13 SearchIndexException (org.opencastproject.matterhorn.search.SearchIndexException)9 IOException (java.io.IOException)4 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)4 SearchMetadata (org.opencastproject.matterhorn.search.SearchMetadata)4 ElasticsearchDocument (org.opencastproject.matterhorn.search.impl.ElasticsearchDocument)4 HashMap (java.util.HashMap)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 SearchHit (org.elasticsearch.search.SearchHit)1 SearchHitField (org.elasticsearch.search.SearchHitField)1 Event (org.opencastproject.index.service.impl.index.event.Event)1 EventQueryBuilder (org.opencastproject.index.service.impl.index.event.EventQueryBuilder)1 Group (org.opencastproject.index.service.impl.index.group.Group)1 GroupQueryBuilder (org.opencastproject.index.service.impl.index.group.GroupQueryBuilder)1 Series (org.opencastproject.index.service.impl.index.series.Series)1 SeriesQueryBuilder (org.opencastproject.index.service.impl.index.series.SeriesQueryBuilder)1 Theme (org.opencastproject.index.service.impl.index.theme.Theme)1 ThemeQueryBuilder (org.opencastproject.index.service.impl.index.theme.ThemeQueryBuilder)1 SearchMetadataImpl (org.opencastproject.matterhorn.search.impl.SearchMetadataImpl)1 SearchResultImpl (org.opencastproject.matterhorn.search.impl.SearchResultImpl)1