Search in sources :

Example 1 with SearchMetadataImpl

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

SearchResponse (org.elasticsearch.action.search.SearchResponse)1 SearchHit (org.elasticsearch.search.SearchHit)1 SearchHitField (org.elasticsearch.search.SearchHitField)1 SearchIndexException (org.opencastproject.matterhorn.search.SearchIndexException)1 SearchMetadataCollection (org.opencastproject.matterhorn.search.impl.SearchMetadataCollection)1 SearchMetadataImpl (org.opencastproject.matterhorn.search.impl.SearchMetadataImpl)1 SearchResultImpl (org.opencastproject.matterhorn.search.impl.SearchResultImpl)1 SearchResultItemImpl (org.opencastproject.matterhorn.search.impl.SearchResultItemImpl)1 IndexRecreateObject (org.opencastproject.message.broker.api.index.IndexRecreateObject)1