Expand my Community achievements bar.

AEM Search Pages and Asset References in one result

Avatar

Level 4

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;

}

2 Replies

Avatar

Community Advisor

Hi,

No need to do fulltext search just search for assets based on meatdata.

1. Get all the assets which met metadata criteria, for example

path=/content/dam/AEM63Lab/

type=nt:unstructured

nodename=metadata

property=dc:title

property.value=cisco

2. From the result get the assets path and store in array list or Array.

3. loop through array/arrayList and get all the page references for assets one by one using below code.

ReferenceSearch rr = new ReferenceSearch();

Map map = rr.search(request.getResourceResolver(),assetPath);

If you want to use pagination for assets you can always use limit, offset with guesstotal.

Query Builder API

If you want to use pagination for Pages, you can handle this with Java once you have results in resultSets you can use resultSet however you want.

java - How to implement pagination on a list? - Stack Overflow

Please let us know the exact requirement if it doesn't help.

Thanks

Arun



Arun Patidar

Avatar

Level 4

So my properties are not defined for metadata. it could be multiple properties where the text matches, hence doing a full text search.

Secondly, I am searching for assets and pages together. out of the assets returned, i am fetching their references(pages) and returning these as well. Hence the issue with query builder pagination.

Third, I don't want to create the entire list in one shot as this may be a slow query and implement pagination using java.