【问题标题】:Retrieve AEM Page Properties via Search/QueryBuilder API通过 Search/QueryBuilder API 检索 AEM 页面属性
【发布时间】:2013-12-16 17:24:22
【问题描述】:

有没有办法通过 QueryBuilder API 检索作为页面元数据(页面属性)存储在单独 JCR 节点中的数据?

例子:

搜索结果应包含 ~/article-1/jcr:content/thumbnail 下的数据。但是,我得到的唯一结果是 ~article-1/jcr:content/content(模板中包含的 parsys)下的数据。

一个示例查询:

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

结果(sn-p):

{
   "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"
}

搜索配置是开箱即用的默认配置。

编辑:数据以 JSON 格式返回,但是无法在 API 中访问:

结果:

{
   "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"
            }
         }
      }
   ]
}

实现代码:

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));
    }
}

【问题讨论】:

    标签: aem


    【解决方案1】:

    在查询中设置好路径后,再设置一个或多个属性过滤器,如本例:

    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
    

    您可以在 /libs/cq/search/content/querydebug.html 上设置它们,然后也可以使用它来获取同一查询的 JSON URL。

    查看其他示例:http://dev.day.com/docs/en/cq/5-5/dam/customizing_and_extendingcq5dam/query_builder.html

    【讨论】:

    • 我确实需要使用 p.hits=full 和 p.nodedepth=2 来获取缩略图属性。使用 p.hits=selective 从 JSON 中的点击中删除所有数据。
    【解决方案2】:

    您可以使用CURLHTML/ JSON/ XML 格式检索节点属性。您所要做的就是安装下载 CURL 并从您的终端从与 curl 的 .exe 文件相同的目录运行您的 curl 命令。

    HTML 示例:

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

    JSON 示例:

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

    注意:上述查询中的infinity 确保您以递归方式获取指定路径下每个节点的每个属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      相关资源
      最近更新 更多