Expand my Community achievements bar.

How to retrieve all properties of an asset using the QueryBuilder Api

Avatar

Level 3

When i use the below query on http://localhost:4502/libs/cq/search/content/querydebug.html

path=/content/dam/we-retail

type=dam:Asset

p.limit=-1

p.nodedepth=2

p.hits=full

p.guesstotal=true

the formed URL/JSON querybuilder link is : http://localhost:4502/bin/querybuilder.json?p.guesstotal=true&p.hits=full&p.limit=-1&p.nodedepth=2&p...

I can see all the properties for each asset including jcr:content, metadata as below:

Screen Shot 2018-05-22 at 6.41.21 PM.png

I need to return the same result to service/endpoint i'm building on AEM for a customer. When I translate the same above query into Query builder API

queryParamsMap.put("type", "dam:Asset");

queryParamsMap.put("p.limit", "-1");

queryParamsMap.put("p.nodedepth", "2");

queryParamsMap.put("p.hits", "full");

queryParamsMap.put("p.guessTotal", "true");

How can i retrieve all the values?

SearchResult result = query.getResult();

for (final Hit hit : result.getHits()) {

  Resource resource = hit.getResource();

  Asset asset = resource.adaptTo(Asset.class);

If i use asset.getMetadata() , we can see only the properties under "jcr:content/metadata" but not the other properties.

and

if i use ValueMap properties = resource.getValueMap(); we can retrieve all the asset properties (like jcr:path, jcr:primaryType etc) but not "metadata".

Is there any way to get all the values for an Asset node?

1 Reply

Avatar

Level 3

After some research, i found the below solution.

There is no method that will return all the properties of an Asset (or a Node. Asset is also a node) in AEM. We have to do this in a combination.

  1. Get all the top level properties (Node root level). These include jcr:created, jcr:createdBy, etc.
  2. Get all jcr:content level properties. These include cq:name, cq:lastModified, etc
  3. Get all jcr:content\metadata level properties. These include dc:title, any custom metadata etc.
  4. You can add all these to another new ValueMap that can hold all the properties of a particular Node/Asset.

d3hbx.png

Below is a code snippet:

Resource resource;

ValueMap mainProperties;

ValueMap assetMetadataProperties;

Resource metadataResource;

ValueMap jcrProperties;

Resource jcrdataResource;

ValueMap allProperties;

for (Hit hit : result.getHits()) {

        //LOGGER.info("\n********Hit path="+hit.getPath()+", title="+hit.getTitle());

        resource = hit.getResource();

        if(null!=resource){

            mainProperties = resource.getValueMap();

            // Add JCR Properties

            jcrdataResource = resource.getChild("jcr:content");

            jcrProperties = ResourceUtil.getValueMap(jcrdataResource);

            // Add Metadata properties

            metadataResource = resource.getChild("jcr:content/metadata");

            assetMetadataProperties = ResourceUtil.getValueMap(metadataResource);

            // Adding all togethe

            allProperties = new ValueMapDecorator(new HashMap());

            allProperties.putAll(hit.getProperties());

            allProperties.putAll(mainProperties); // Includes jcr:created createdDate etc.

            allProperties.put("jcr:path",hit.getPath()); //Add Path

            allProperties.putAll(jcrProperties);

            allProperties.putAll(assetMetadataProperties);

            //LOGGER.debug("All Asset Properties="+new Gson().toJson(allProperties));              

        }

    }

Note

  1. jcr:path is not returned by any of the above. So i had to explicitly add it using hit.getPath
  2. The name of the node or Asset name can be pulled from hit.getTitle(). Ofcourse this is also returned as part of cq:name.
  3. There are other ways to get the properties as well. One aother way is to get the Node and retrieve the properties. com.day.cq.search.result.Hit has a method getNode() that returns a java.jcr.Node interface and you can use that get to fetch the properties.