Getting Page path from SearchResult of QueryBuilder | Community
Skip to main content
New Participant
December 16, 2023
Solved

Getting Page path from SearchResult of QueryBuilder

  • December 16, 2023
  • 1 reply
  • 796 views

Hello Team,

 

import com.day.cq.search.result.SearchResult;
 
List<MyPOJO> pojoObj = new ArrayList<>();
 
SearchResult results = query.getResult();
Iterator<Resource> resources = results.getResources();
            pojoObj = StreamSupport.stream(((Iterable<Resource>) () -> resources).spliterator(), false)
                    .map(resource -> resource.adaptTo(MyPOJO.class)).filter(Objects::nonNull)
                    .collect(Collectors.toList());
 
MyPOJO is a sling Model class which is having necessary fields
@ValueMapValue(name = "jcr:title")
private String title;
 
@ValueMapValue(name="jcr:description")
private String description;
   
@ValueMapValue(name = "property1")
private String prop;
 
Now, here how can I get the page path? 
  
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 SwetaB

In that case you can try using

 

@Self

private Resource resource;

 

 

resource.getPath();

1 reply

New Participant
December 16, 2023

Hi Mahesh,

 

Is there a particular reason why you would adapt it to a POJO class?

Instead you can adapt it to Page  class and get all the fields required.

 

Page page = resource.adaptTo(Page.class);

            if (page != null) {

                String title = page.getTitle();

                String path = page.getPath();

            }

 

New Participant
December 16, 2023

I have 15 to 20 custom properties under each page. So, if I adapt to Page.class, then I need to write several lines of code to get each properties.

 

String title = page.getTitle();

String path = page.getPath();

 

ValueMap values = page.getProperties();

then, get each property values from ValueMap object.  

Also, Need to iterate through SearchResult object using while or for loop.

 

 

SwetaBAccepted solution
New Participant
December 16, 2023

In that case you can try using

 

@Self

private Resource resource;

 

 

resource.getPath();