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;
}
Aggregations