Skip to main content
Level 2
Dec 27, 2023 7:06 AM UTC
Solved

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

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

AakarshAccepted solution
Level 2
Oct 24, 2024 8:27 AM UTC

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();

8 replies

arunpatidar
Community Advisor
Community Advisor
Dec 27, 2023 9:29 AM UTC

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 

 

Translate
Arun Patidar
AakarshAuthor
Level 2
Dec 27, 2023 9:32 AM UTC

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. 

Translate
aanchal-sikka
Community Advisor
Community Advisor
Dec 27, 2023 10:19 AM UTC

@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(); } });
Translate
Aanchal Sikka
AakarshAuthor
Level 2
Dec 28, 2023 9:32 AM UTC
.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.

 

Translate
aanchal-sikka
Community Advisor
Community Advisor
Dec 28, 2023 10:19 AM UTC

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

Translate
Aanchal Sikka
kautuk_sahni
Level 9
Jan 2, 2024 12:43 PM UTC

@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.

Translate
Kautuk Sahni
AakarshAuthorAccepted solution
Level 2
Oct 24, 2024 8:27 AM UTC

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();

Translate