I have assets which have some metadata. I am performing a full text search on my pages and also on my assets (/content and /content/dam). This result returns me the dam:Asset as well as cq:Page. But instead of the assets, I wanted to have the pages in which these assets are referenced. If there an out of the box way or query i can use to get the pages as well as the pages where these matching assets are referenced?
If you can help me join below two queries -
select * from [dam:Asset] as parent where contains(*, 'abcd') and isdescendantnode(parent, '/content/dam')
select * from [cq:Page] as child where contains(*, {parent asset path})
If the above is achieved i can do a union with below query
select * from [cq:Page] as a where contains(*, 'abcd') and isdescendantnode(a, '/content/')
I am following below approach to achieve this but I face issue with pagination. - setting offset and limit as I am manipulating the search results. - the asset references can either be 0 or 100 and I add this to my search results.
predicates.put("group.p.or", "true"); // combine this group with OR
predicates.put("group.1_group.path", "/content/");
predicates.put("group.1_group.type", "cq:Page");
predicates.put("group.2_group.path", "/content/dam/");
predicates.put("group.2_group.type", "dam:Asset");
predicates.put(LIMIT, String.valueOf(params.getLimit()));
predicates.put(OFFSET, String.valueOf(params.getOffset()));
Predicate fulltextSearch = new Predicate(FulltextPredicateEvaluator.FULLTEXT);
fulltextSearch.set(FulltextPredicateEvaluator.FULLTEXT, params.getSearchText());
query.getPredicates().add(fulltextSearch);
SearchResult results = query.getResult();
List<Node> nodes = new ArrayList<>();
Iterator<Node> it = results.getNodes();
while (it.hasNext()) {
Node result = it.next();
try {
if (result.getPath().contains("/content/dam")){
nodes.addAll(assetReferences.getAssetReferences(result.getPath().replace("/jcr:content/metadata","")));
}
} catch (RepositoryException e) {
LOGGER.error(e.getMessage());
}
}
List<Node> searchResults = Lists.newArrayList(results.getNodes());
searchResults.addAll(nodes);
Asset References
public List<Node> getAssetReferences(final String assetPath){
List<Node> nodes = new ArrayList<>();
ReferenceSearch referenceSearch = new ReferenceSearch();
referenceSearch.setExact(true);
referenceSearch.setHollow(false);
referenceSearch.setMaxReferencesPerPage(-1);
Collection<ReferenceSearch.Info> resultSet = referenceSearch.search(resolver, assetPath).values();
for (ReferenceSearch.Info info: resultSet) {
nodes.add(info.getPage().adaptTo(Node.class));
}
return nodes;
}