Querybuilder using stream | Community
Skip to main content
Level 7
July 15, 2024
Solved

Querybuilder using stream

  • July 15, 2024
  • 2 replies
  • 1022 views

Hello Everyone,

 

I have few pages under: /content/abc/us/en  which is having node property:  article value as blog

Now, I need to find out list of pages under:  /content/abc/us/en where  property "article" has the value: blog.

How can I write the query using Java Stream option. Output will be list of pages. i mean List<myPage> = .......

Note: I have sling model: class myPage    which is having the member variables, necessary to hold the page properties.

Can someone, help me to write Querybuilder?

Was searching here: https://kiransg.com/2021/11/22/aem-with-java-streams/

 

Thanks

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by PGURUKRISHNA

Hi @mahesh_gunaje You can use the QueryBuilder API in AEM to achieve this. Here's an example of how you can write the query using Java Stream:

 

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import java.util.List;
import java.util.stream.Stream;

@Component
public class MyQueryBuilder {

@Reference
private ResourceResolverFactory resourceResolverFactory;

public List<myPage> getPagesWithArticleProperty() {
ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
Resource rootResource = resourceResolver.getResource("/content/abc/us/en");

QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
Query query = queryBuilder.createQuery(PredicateGroup.create(
predicate("article", "blog")
), queryBuilder.getResourceFactory());

SearchResult result = query.execute();
Stream<Resource> resourceStream = StreamSupport.stream(result.getResources().spliterator(), false);

return resourceStream
.map(resource -> resource.adaptTo(myPage.class))
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
}

In this example, we first get the ResourceResolver instance and then get the root resource using the path /content/abc/us/en. We then create a QueryBuilder instance and define a query using the PredicateGroup API. In this case, we're searching for resources that have a property named "article" with a value of "blog".

We then execute the query and get the search result. We use the StreamSupport class to convert the Resource iterator to a Stream. We then map each Resource to an instance of myPage using the adaptTo method, filter out any null values, and collect the results into a list.

Note that you'll need to inject the ResourceResolverFactory instance using the @Reference annotation, and also make sure that you have the necessary dependencies in your project's pom file.

2 replies

arunpatidar
Community Advisor
Community Advisor
July 15, 2024

Hi @mahesh_gunaje 
Here is an example of using QueryBuilder in Java to retrieve article pages with a specific property value. You can write the following query, and if needed, you can use Java Streams to filter, map, or transform the results.

Example Query 

path=/content/abc/us/en type=cq:Page property=jcr:content/article property.value=blog p.limit=-1


Sample Java cpde
https://github.com/arunpatidar02/aemaacs-aemlab/blob/master/core/src/main/java/com/community/aemlab/core/servlets/sample/SampleQueryAPISearchServlet.java 


Arun Patidar
PGURUKRISHNA
PGURUKRISHNAAccepted solution
Level 4
July 16, 2024

Hi @mahesh_gunaje You can use the QueryBuilder API in AEM to achieve this. Here's an example of how you can write the query using Java Stream:

 

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import java.util.List;
import java.util.stream.Stream;

@Component
public class MyQueryBuilder {

@Reference
private ResourceResolverFactory resourceResolverFactory;

public List<myPage> getPagesWithArticleProperty() {
ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
Resource rootResource = resourceResolver.getResource("/content/abc/us/en");

QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
Query query = queryBuilder.createQuery(PredicateGroup.create(
predicate("article", "blog")
), queryBuilder.getResourceFactory());

SearchResult result = query.execute();
Stream<Resource> resourceStream = StreamSupport.stream(result.getResources().spliterator(), false);

return resourceStream
.map(resource -> resource.adaptTo(myPage.class))
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
}

In this example, we first get the ResourceResolver instance and then get the root resource using the path /content/abc/us/en. We then create a QueryBuilder instance and define a query using the PredicateGroup API. In this case, we're searching for resources that have a property named "article" with a value of "blog".

We then execute the query and get the search result. We use the StreamSupport class to convert the Resource iterator to a Stream. We then map each Resource to an instance of myPage using the adaptTo method, filter out any null values, and collect the results into a list.

Note that you'll need to inject the ResourceResolverFactory instance using the @Reference annotation, and also make sure that you have the necessary dependencies in your project's pom file.