For example, if we should find a banner which has given keyword and it's visible and it's also belongs to the certain site. By the way banner has a priority - zero is the highest.
So we should use code like below for this kind of lookup:
private static final String PRIORITY_PROPERTY_NAME = "priority";
private RepositoryItem findByKeyword(final String currentTerm) throws RepositoryException {
RepositoryItem result = null;
RepositoryView view = getCatalogTools().getCatalog().getItemDescriptor(getBannerItemDescriptorName()).getRepositoryView();
QueryBuilder qb = view.getQueryBuilder();
String descriptionPropName = getCatalogTools().getPropertyManager().getDescriptionPropertyName();
QueryExpression descriptionPropExpression = qb.createPropertyQueryExpression(descriptionPropName);
QueryExpression termExpression = qb.createConstantQueryExpression(currentTerm);
QueryExpression isVisiblePropExpression = qb.createPropertyQueryExpression(getCatalogTools().getPropertyManager().getIsVisiblePropertyName());
QueryExpression trueExpression = qb.createConstantQueryExpression(Boolean.TRUE);
QueryExpression siteIdExpression = qb.createPropertyQueryExpression("siteIds");
QueryExpression currentSiteExpression = qb.createConstantQueryExpression(SiteContextManager.getCurrentSite());
Query isVisibleQuery = qb.createComparisonQuery(isVisiblePropExpression, trueExpression, QueryBuilder.EQUALS);
Query teaserQuery = qb.createPatternMatchQuery(descriptionPropExpression, termExpression, QueryBuilder.CONTAINS, true);
Query siteQuery = qb.createIncludesQuery(siteIdExpression, currentSiteExpression);
Query andQuery = qb.createAndQuery(new Query[] { teaserQuery, isVisibleQuery, siteQuery });
String[] precachedPropertyNames = { PRIORITY_PROPERTY_NAME };
SortDirectives sortDirectives = new SortDirectives();
sortDirectives.addDirective(new SortDirective(PRIORITY_PROPERTY_NAME, SortDirective.DIR_ASCENDING));
RepositoryItem[] repositoryItems = view.executeQuery(andQuery, new QueryOptions(0, 1, sortDirectives,
precachedPropertyNames));
if ((repositoryItems != null) && (repositoryItems.length > 0)) {
result = repositoryItems[0];
}
return result;
}