ResourceFilterStream.class for Content Fragment trying to .setChildSelector("[jcr:content/metadata/cq:tags] | Community
Skip to main content
Level 2
December 27, 2023
Solved

ResourceFilterStream.class for Content Fragment trying to .setChildSelector("[jcr:content/metadata/cq:tags]

  • December 27, 2023
  • 3 replies
  • 1592 views

Hi,

 

I am trying to filter out Content Fragment using ResourceFilterStream, but cannot do so using .setChildSelector("[jcr:content/metadata/cq:tags]

 

Doing something like below.

 

Resource resource = resourceResolver.getResource("/content/dam/test-site");
ResourceFilterStream rfs = resource.adaptTo(ResourceFilterStream.class);

rfs.setBranchSelector("[jcr:primaryType] == 'dam:Asset'")
.setChildSelector("[jcr:content/metadata/cq:tags] like 'test-site:test'")
.stream()
.forEach(r-> logger.info("found resource {}",r.getPath()));

what should be the  .setChildSelector

Best answer by Aakarsh

this worked for me 

Map<String, Object> paramMap = new HashMap<>();
paramMap.put("first", Tag);
paramMap.put("second", "true");

Resource resource = resourceResolver.getResource(CONTENT_FRAGMENT_PATH);
ResourceFilterStream resourceFilterStream = resource.adaptTo(ResourceFilterStream.class);
TagCount = (int) resourceFilterStream.setBranchSelector("[jcr:primaryType] == 'dam:Asset'")
.setChildSelector("[jcr:content/metadata/cq:tags] contains $first && [jcr:content/data/master/isActive] like $second")
.addParams(paramMap)
.stream()
.count();

3 replies

arunpatidar
Community Advisor
Community Advisor
December 27, 2023

Hi @aakarsh 
You should be using the JCR queries to find out the fragments. The advantages with JCR queries are you can leverage the index.

Example

 

SELECT * FROM [dam:Asset] AS cf WHERE ISDESCENDANTNODE(cf, '/content/dam/test-site') AND cf.[jcr:content/metadata/cq:tags] IN ('tag1', 'tag2', 'tag3')

 

Note: Please check the property name and path and adjust accordingly 

 

Arun Patidar
AakarshAuthor
Level 2
December 27, 2023

Hi Arun,

 

Thank for the prompt reply, but I am familiar with JCR and SQL2 queries, wanted to use ResourceFilterStream as there are many small queries which get fired on every page load. Not using caching to wanted to use this for optimized tree traversal. 

aanchal-sikka
Community Advisor
Community Advisor
December 27, 2023

@aakarsh 

 

Please try with following code:

 

ResourceFilterStream rfs = resource.adaptTo(ResourceFilterStream.class); rfs .setBranchSelector("[jcr:primaryType] != 'dam:Asset'") .setChildSelector("[jcr:content/metadata/cq:tags] contains $tag").addParam("tag", "properties:orientation/landscape") .stream() .forEach(r -> { try { resp.getWriter().println("Path: " + r.getPath()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } });
Aanchal Sikka
AakarshAuthor
Level 2
December 28, 2023
.setBranchSelector("[jcr:primaryType] != 'dam:Asset'")

this would not pick the content fragments.

.setChildSelector("[jcr:content/metadata/cq:tags] contains $tag").addParam("tag", "properties:orientation/landscape")

 is still not able to pick tags.

 

aanchal-sikka
Community Advisor
Community Advisor
December 28, 2023

Hello @aakarsh 

 

If you are adding the tags via CF properties, then the previous should work.

 

If you are adding it to a variation, via CF Edit, then its more dynamic. Please confirm, it its variation

Aanchal Sikka
kautuk_sahni
Community Manager
Community Manager
January 2, 2024

@aakarsh Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni
AakarshAuthorAccepted solution
Level 2
October 24, 2024

this worked for me 

Map<String, Object> paramMap = new HashMap<>();
paramMap.put("first", Tag);
paramMap.put("second", "true");

Resource resource = resourceResolver.getResource(CONTENT_FRAGMENT_PATH);
ResourceFilterStream resourceFilterStream = resource.adaptTo(ResourceFilterStream.class);
TagCount = (int) resourceFilterStream.setBranchSelector("[jcr:primaryType] == 'dam:Asset'")
.setChildSelector("[jcr:content/metadata/cq:tags] contains $first && [jcr:content/data/master/isActive] like $second")
.addParams(paramMap)
.stream()
.count();