Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

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

Avatar

Level 2

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

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@Aakarsh For CF with tags via Properties screen

 

aanchalsikka_0-1703759590994.png

 

property in CRXDE:

aanchalsikka_1-1703759632554.png

 

Query:

 

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

 

 

Result:

aanchalsikka_2-1703759672704.png

 


Aanchal Sikka

View solution in original post

8 Replies

Avatar

Community Advisor

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

Avatar

Level 2

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. 

Avatar

Community Advisor

@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

Avatar

Level 2
.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.

 

Avatar

Community Advisor

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

Avatar

Correct answer by
Community Advisor

@Aakarsh For CF with tags via Properties screen

 

aanchalsikka_0-1703759590994.png

 

property in CRXDE:

aanchalsikka_1-1703759632554.png

 

Query:

 

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

 

 

Result:

aanchalsikka_2-1703759672704.png

 


Aanchal Sikka

Avatar

Administrator

@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

Avatar

Level 2

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