use of org.opencastproject.search.api.SearchQuery in project opencast by opencast.
the class SearchServiceImplTest method testGetMediaPackage.
/**
* Adds a simple media package that has a dublin core for the episode only.
*/
@Test
public void testGetMediaPackage() throws Exception {
MediaPackage mediaPackage = getMediaPackage("/manifest-simple.xml");
// Make sure our mocked ACL has the read and write permission
acl.getEntries().add(new AccessControlEntry(ROLE_STUDENT, READ.toString(), true));
acl.getEntries().add(new AccessControlEntry(ROLE_STUDENT, WRITE.toString(), true));
// Add the media package to the search index
Job job = service.add(mediaPackage);
JobBarrier barrier = new JobBarrier(null, serviceRegistry, 1000, job);
barrier.waitForJobs();
assertEquals("Job to add mediapckage did not finish", Job.Status.FINISHED, job.getStatus());
// Make sure it's properly indexed and returned for authorized users
SearchQuery q = new SearchQuery();
q.includeEpisodes(true);
q.includeSeries(false);
q.withId("10.0000/1");
assertEquals(1, service.getByQuery(q).size());
acl.getEntries().clear();
acl.getEntries().add(new AccessControlEntry("ROLE_UNKNOWN", READ.toString(), true));
acl.getEntries().add(new AccessControlEntry(ROLE_STUDENT, WRITE.toString(), true));
// Add the media package to the search index
job = service.add(mediaPackage);
barrier = new JobBarrier(null, serviceRegistry, 1000, job);
barrier.waitForJobs();
assertEquals("Job to add mediapckage did not finish", Job.Status.FINISHED, job.getStatus());
// This mediapackage should not be readable by the current user (due to the lack of role ROLE_UNKNOWN)
q = new SearchQuery();
q.includeEpisodes(true);
q.includeSeries(false);
q.withId("10.0000/1");
assertEquals(0, service.getByQuery(q).size());
}
use of org.opencastproject.search.api.SearchQuery in project opencast by opencast.
the class SearchServiceImplTest method testSorting.
@Test
public void testSorting() throws Exception {
MediaPackage mediaPackageNewer = getMediaPackage("/manifest-full.xml");
MediaPackage mediaPackageOlder = getMediaPackage("/manifest-full-older.xml");
// MH-10573, ensure first job finishes publishing before job2
Job job = service.add(mediaPackageNewer);
JobBarrier barrier = new JobBarrier(null, serviceRegistry, 1000, job);
barrier.waitForJobs();
Job job2 = service.add(mediaPackageOlder);
JobBarrier barrier2 = new JobBarrier(null, serviceRegistry, 1000, job2);
barrier2.waitForJobs();
String olderTitle = "Older Recording";
String newerTitle = "Land and Vegetation: Key players on the Climate Scene";
SearchQuery query = new SearchQuery();
query.withSort(SearchQuery.Sort.DATE_CREATED);
assertEquals(2, service.getByQuery(query).size());
assertEquals(olderTitle, service.getByQuery(query).getItems()[0].getDcTitle());
query.withSort(SearchQuery.Sort.DATE_CREATED, false);
assertEquals(newerTitle, service.getByQuery(query).getItems()[0].getDcTitle());
// FYI: DATE_PUBLISHED is the time of Search update, not DC modified (MH-10573)
query.withSort(SearchQuery.Sort.DATE_PUBLISHED);
assertEquals(newerTitle, service.getByQuery(query).getItems()[0].getDcTitle());
query.withSort(SearchQuery.Sort.DATE_PUBLISHED, false);
assertEquals(olderTitle, service.getByQuery(query).getItems()[0].getDcTitle());
SearchQuery q = new SearchQuery();
q.withSort(SearchQuery.Sort.TITLE);
assertEquals(newerTitle, service.getByQuery(q).getItems()[0].getDcTitle());
query.withSort(SearchQuery.Sort.TITLE, false);
assertEquals(2, service.getByQuery(q).size());
assertEquals(olderTitle, service.getByQuery(query).getItems()[0].getDcTitle());
query.withSort(SearchQuery.Sort.LICENSE);
// Just checking that the search index works for this field
assertEquals(2, service.getByQuery(query).size());
query.withSort(SearchQuery.Sort.SERIES_ID);
// Just checking that the search index works for this field
assertEquals(2, service.getByQuery(query).size());
query.withSort(SearchQuery.Sort.MEDIA_PACKAGE_ID);
// Just checking that the search index works for this field
assertEquals(2, service.getByQuery(query).size());
query.withSort(SearchQuery.Sort.CONTRIBUTOR);
// Just checking that the search index works for this field
assertEquals(2, service.getByQuery(query).size());
query.withSort(SearchQuery.Sort.CREATOR);
// Just checking that the search index works for this field
assertEquals(2, service.getByQuery(query).size());
query.withSort(SearchQuery.Sort.LANGUAGE);
// Just checking that the search index works for this field
assertEquals(2, service.getByQuery(query).size());
query.withSort(SearchQuery.Sort.SUBJECT);
// Just checking that the search index works for this field
assertEquals(2, service.getByQuery(query).size());
}
use of org.opencastproject.search.api.SearchQuery in project opencast by opencast.
the class SearchServiceImpl method deleteSynchronously.
/**
* Immediately removes the given mediapackage from the search service.
*
* @param mediaPackageId
* the mediapackage
* @return <code>true</code> if the mediapackage was deleted
* @throws SearchException
* if deletion failed
* @throws UnauthorizedException
* if the user did not have access to the media package
* @throws NotFoundException
* if the mediapackage did not exist
*/
public boolean deleteSynchronously(String mediaPackageId) throws SearchException, UnauthorizedException, NotFoundException {
SearchResult result;
try {
result = solrRequester.getForWrite(new SearchQuery().withId(mediaPackageId));
if (result.getItems().length == 0) {
logger.warn("Can not delete mediapackage {}, which is not available for the current user to delete from the search index.", mediaPackageId);
return false;
}
logger.info("Removing mediapackage {} from search index", mediaPackageId);
Date now = new Date();
try {
persistence.deleteMediaPackage(mediaPackageId, now);
logger.info("Removed mediapackage {} from search persistence", mediaPackageId);
} catch (NotFoundException e) {
// even if mp not found in persistence, it might still exist in search index.
logger.info("Could not find mediapackage with id {} in persistence, but will try remove it from index, anyway.", mediaPackageId);
} catch (SearchServiceDatabaseException e) {
logger.error("Could not delete media package with id {} from persistence storage", mediaPackageId);
throw new SearchException(e);
}
return indexManager.delete(mediaPackageId, now);
} catch (SolrServerException e) {
logger.info("Could not delete media package with id {} from search index", mediaPackageId);
throw new SearchException(e);
}
}
use of org.opencastproject.search.api.SearchQuery in project opencast by opencast.
the class SearchRestService method getEpisodeAndSeriesById.
@GET
@Path("series.{format:xml|json}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@RestQuery(name = "series", description = "Search for series matching the query parameters.", pathParameters = { @RestParameter(description = "The output format (json or xml) of the response body.", isRequired = true, name = "format", type = RestParameter.Type.STRING) }, restParameters = { @RestParameter(description = "The series ID. If the additional boolean parameter \"episodes\" is \"true\", " + "the result set will include this series episodes.", isRequired = false, name = "id", type = RestParameter.Type.STRING), @RestParameter(description = "Any series that matches this free-text query. If the additional boolean parameter \"episodes\" is \"true\", " + "the result set will include this series episodes.", isRequired = false, name = "q", type = RestParameter.Type.STRING), @RestParameter(defaultValue = "false", description = "Whether to include this series episodes. This can be used in combination with \"id\" or \"q\".", isRequired = false, name = "episodes", type = RestParameter.Type.STRING), @RestParameter(name = "sort", isRequired = false, description = "The sort order. May include any " + "of the following: DATE_CREATED, DATE_PUBLISHED, TITLE, SERIES_ID, MEDIA_PACKAGE_ID, CREATOR, " + "CONTRIBUTOR, LANGUAGE, LICENSE, SUBJECT, DESCRIPTION, PUBLISHER. Add '_DESC' to reverse the sort order (e.g. TITLE_DESC).", type = RestParameter.Type.STRING), @RestParameter(defaultValue = "20", description = "The maximum number of items to return per page.", isRequired = false, name = "limit", type = RestParameter.Type.STRING), @RestParameter(defaultValue = "0", description = "The page number.", isRequired = false, name = "offset", type = RestParameter.Type.STRING), @RestParameter(defaultValue = "false", description = "Whether this is an administrative query", isRequired = false, name = "admin", type = RestParameter.Type.BOOLEAN) }, reponses = { @RestResponse(description = "The request was processed succesfully.", responseCode = HttpServletResponse.SC_OK) }, returnDescription = "The search results, expressed as xml or json.")
public Response getEpisodeAndSeriesById(@QueryParam("id") String id, @QueryParam("q") String text, @QueryParam("episodes") boolean includeEpisodes, @QueryParam("sort") String sort, @QueryParam("limit") int limit, @QueryParam("offset") int offset, @QueryParam("admin") boolean admin, @PathParam("format") String format) throws SearchException, UnauthorizedException {
SearchQuery query = new SearchQuery();
// If id is specified, do a search based on id
if (StringUtils.isNotBlank(id))
query.withId(id);
// Include series data in the results?
query.includeSeries(true);
// Include episodes in the result?
query.includeEpisodes(includeEpisodes);
// Include free-text search?
if (StringUtils.isNotBlank(text))
query.withText(text);
query.withSort(SearchQuery.Sort.DATE_CREATED, false);
if (StringUtils.isNotBlank(sort)) {
// Parse the sort field and direction
SearchQuery.Sort sortField = null;
if (sort.endsWith(DESCENDING_SUFFIX)) {
String enumKey = sort.substring(0, sort.length() - DESCENDING_SUFFIX.length()).toUpperCase();
try {
sortField = SearchQuery.Sort.valueOf(enumKey);
query.withSort(sortField, false);
} catch (IllegalArgumentException e) {
logger.warn("No sort enum matches '{}'", enumKey);
}
} else {
try {
sortField = SearchQuery.Sort.valueOf(sort);
query.withSort(sortField);
} catch (IllegalArgumentException e) {
logger.warn("No sort enum matches '{}'", sort);
}
}
}
query.withLimit(limit);
query.withOffset(offset);
// Build the response
ResponseBuilder rb = Response.ok();
if (admin) {
rb.entity(searchService.getForAdministrativeRead(query));
} else {
rb.entity(searchService.getByQuery(query));
}
if ("json".equals(format)) {
rb.type(MediaType.APPLICATION_JSON);
} else {
rb.type(MediaType.TEXT_XML);
}
return rb.build();
}
use of org.opencastproject.search.api.SearchQuery in project opencast by opencast.
the class CustomFeedService method loadFeedData.
/**
* {@inheritDoc}
*
* @see org.opencastproject.feed.impl.AbstractFeedGenerator#loadFeedData(org.opencastproject.feed.api.Feed.Type,
* java.lang.String[], int, int)
*/
protected SearchResult loadFeedData(Type type, String[] query, int limit, int offset) {
try {
String q = solrQuery;
if (query != null && query.length > 1) {
Object[] args = new Object[query.length - 1];
for (int i = 1; i < query.length; i++) args[i - 1] = query[i];
q = MessageFormat.format(solrQuery, args);
}
// Make sure there are no remaining arguments
if (q.matches(".*\\{[\\d]+\\}.*")) {
logger.warn("Feed has been called with an insufficient number of parameters");
return null;
}
// Create the query
SearchQuery searchQuery = createBaseQuery(type, limit, offset);
searchQuery.includeEpisodes(true);
searchQuery.includeSeries(false);
searchQuery.withQuery(q);
searchService.getByQuery(searchQuery);
return searchService.getByQuery(searchQuery);
} catch (Exception e) {
logger.error("Cannot retrieve result for aggregated feed", e);
return null;
}
}
Aggregations