3
votes

Is there a way to retrieve data stored as page metadata (Page Properties) stored in a separate JCR node via the QueryBuilder API?

Example:

JCR Tree

The search results should include the data under ~/article-1/jcr:content/thumbnail. However, the only results I am getting are data under the ~article-1/jcr:content/content (a parsys included on the template).

An example query:

http://localhost:4502/bin/querybuilder.json?p.hits=full&path=/content/path/to/articles

Results in (snippet):

{
   "jcr:path":"/content/path/to/articles/article-1",
   "jcr:createdBy":"admin",
   "jcr:created":"Tue Dec 03 2013 16:26:51 GMT-0500",
   "jcr:primaryType":"cq:Page"
},
{
   "jcr:path":"/content/path/to/articles/article-1/jcr:content",
   "sling:resourceType":"myapp/components/global/page/productdetail",
   "jcr:lockIsDeep":true,
   "jcr:uuid":"4ddebe08-82e1-44e9-9197-4241dca65bdf",
   "jcr:title":"Article 1",
   "jcr:mixinTypes":[
      "mix:lockable",
      "mix:versionable"
   ],
   "jcr:created":"Tue Dec 03 2013 16:26:51 GMT-0500",
   "jcr:baseVersion":"24cabbda-1e56-4d37-bfba-d0d52aba1c00",
   "cq:lastReplicationAction":"Activate",
   "jcr:isCheckedOut":true,
   "cq:template":"/apps/myapp/templates/global/productdetail",
   "cq:lastModifiedBy":"admin",
   "jcr:primaryType":"cq:PageContent",
   "jcr:predecessors":[
      "24cabbda-1e56-4d37-bfba-d0d52aba1c00"
   ],
   "cq:tags":[
      "mysite:mytag"
   ],
   "jcr:createdBy":"admin",
   "jcr:versionHistory":"9dcd41d4-2e10-4d52-b0c0-1ea20e102e68",
   "cq:lastReplicatedBy":"admin",
   "cq:lastModified":"Mon Dec 09 2013 17:57:59 GMT-0500",
   "cq:lastReplicated":"Mon Dec 16 2013 11:42:54 GMT-0500",
   "jcr:lockOwner":"admin"
}

Search configuration is the out-of-the-box default.

EDIT: Data is returning in JSON, however, is not accessable in the API:

Result:

{
   "success":true,
   "results":2,
   "total":2,
   "offset":0,
   "hits":[
      {
         "jcr:path":"/content/path/to/articles/article-a",
         "jcr:createdBy":"admin",
         "jcr:created":"Tue Dec 03 2013 16:27:01 GMT-0500",
         "jcr:primaryType":"cq:Page",
         "jcr:content":{
            "sling:resourceType":"path/to/components/global/page/productdetail",
            "_comment":"// ***SNIP*** //",
            "thumbnail":{
               "jcr:lastModifiedBy":"admin",
               "imageRotate":"0",
               "jcr:lastModified":"Wed Dec 04 2013 12:10:47 GMT-0500",
               "jcr:primaryType":"nt:unstructured"
            }
         }
      },
      {
         "jcr:path":"/content/path/to/articles/article-1",
         "jcr:createdBy":"admin",
         "jcr:created":"Tue Dec 03 2013 16:26:51 GMT-0500",
         "jcr:primaryType":"cq:Page",
         "jcr:content":{
            "sling:resourceType":"path/to/components/global/page/productdetail",
            "_comment":"// ***SNIP*** //",
            "thumbnail":{
               "jcr:lastModifiedBy":"admin",
               "imageRotate":"0",
               "fileReference":"/content/dam/path/to/IBMDemo/apparel/women/wsh005_shoes/WSH005_0533_is_main.jpg",
               "jcr:lastModified":"Mon Dec 09 2013 17:57:58 GMT-0500",
               "jcr:primaryType":"nt:unstructured"
            }
         }
      }
   ]
}

Implementation code:

searchCriteria.put("path", path);
searchCriteria.put("type", "cq:Page");

searchCriteria.put("p.offset", offset.toString());
searchCriteria.put("p.limit", limit.toString());
searchCriteria.put("p.hits", "full");
searchCriteria.put("p.properties", "thumbnail");
searchCriteria.put("p.nodedepth", "2");

PredicateGroup predicateGroup = PredicateGroup.create(searchCriteria);
Query query = queryBuilder.createQuery(predicateGroup, session);
SearchResult result = query.getResult();

for (Hit hit : result.getHits()) {
    try {
        ValueMap properties = hit.getProperties();

        VFSearchResult res = new VFSearchResult();

        res.setUrl(hit.getPath());
        res.setImageUrl((String)properties.get("thumbnail"));
        res.setTags((String[])properties.get("cq:tags"));
        res.setTeaserText((String)properties.get("teaserText"));
        res.setTitle((String)properties.get("jcr:title"));

        searchResults.add(res);
    } catch (RepositoryException rex) {
        logger.debug(String.format("could not retrieve node properties: %1s", rex));
    }
}
2

2 Answers

2
votes

After setting the path in the query, then set one or more property filters, such as in this example:

type=cq:Page
path=/content/path/to/articles
property=jcr:content/thumbnail
property.operation=exists
p.hits=selective
p.properties=jcr:content/thumbnail someSpecificThumbnailPropertyToRetrieve
p.limit=100000

You can set those on /libs/cq/search/content/querydebug.html and then also use that to get the JSON URL for the same query.

Check this out for some other examples: http://dev.day.com/docs/en/cq/5-5/dam/customizing_and_extendingcq5dam/query_builder.html

1
votes

You could use CURL to retrieve node properties in the HTML/ JSON/ XML format. All you have to do is install download CURL and run your curl commands from your terminal from the same directory as the curl's .exe file.

HTML example:

C:\Users\****\Desktop>curl -u username:password http://localhost:4502/content/geometrixx-media/en/gadgets/leaps-in-ai.html

JSON example:

**C:\Users\****\Desktop>**curl -u username:password http://localhost:4502/content/geometrixx-media/en/gadgets/leaps-in-ai.html.tidy.infinity.json

Note: infinity in the above query ensures you get every property of every node under your specified path recursively