using QueryBuilder how to get the absolute URLs of the pages of the components found?
Using QueryBuilder we can search for component(s) which contain a property with a specific value (e.g. an ID we are looking for). From this, I need to return some json which includes the properties of the component, the absolute URL of the page this component is on, and the values of some other specific components on the same page, for each hit. E.g. if we have a bunch of game info pages, each having a game info block component which has gameId as a field, I need to search for specific games (e.g. from a list of previously played game ids), and return the URLS of the game pages found, along with all the main block and other block properties as json so it can be rendered on client side using react. Below is some code which gets the properties of each game block.
Q1: How to get the absolute URL of the page the block is on or the other components on the page?
Q2: how to improve the search time by narrowing down the predicate? Eg. how to find published only, and only for the required component type or page template type?
Q3: is there a better way than lots of hand manipulation, e.g. can something like sling model exporters be used?
Q4: I tried to do gson.toJson(searchResult), so I can see what data it holds, but this fails due to duplicate fields. Is there another way to dump the entire searchResult so we can figure out whats in it, rather than manually printing every value in various loops? I am using eclipse, but haven't seen anything on getting cloud SDK to work in debugger so the vars can be inspected.
predicate.put("path", "/content/ourweb/en/");
predicate.put("type", "cq:component");
predicate.put("property", "ourId");
predicate.put("property.value", "1234");
Query query = builder.createQuery(PredicateGroup.create(predicate), session);
SearchResult searchResult = query.getResult();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String result = "";
for(Hit hit : searchResult.getHits()) {
String path = hit.getPath();
result += " hits: " + gson.toJson(hit.getProperties());
}
There was a great reply, but it seems to have been deleted.
It had this in it:
final Resource hitResource = hit.getResource();
if (hitResource != null) {
final Page page = getPage(hitResource);
if (page != null) {
final Resource contentResource = page.getContentResource();
getResultList(resourceResolver, searchResultsLists, hit, hitResource, contentResource);
}
getPage looks useful, is this a built in function?
what does page.get|ContentResource() return exaclty?