Querybuilder仅搜索单个标记

时间:2016-09-30 00:53:04

标签: cq5 aem query-builder jcr-sql2

我使用Querybuilder搜索标记有特定标记的网页。问题是Querybuilder正在创建一个xpath查询,用于搜索标记的所有子标记以及主标记。我希望它只搜索父标记而不是子标记。使用PredicateGroup组合所有标签 -

map.put("path", searchPath);
map.put("type", "cq:Page");
map.put("orderby", "@jcr:content/cq:lastModified");
PredicateGroup tagsPredicateGroup = new PredicateGroup();
PredicateGroup searchGroup = PredicateGroup.create(map);
PredicateGroup tagsPredicateGroup = new PredicateGroup();
        tagsPredicateGroup.setAllRequired(false);
        if (tagsArray != null && tagsArray.length > 0) {
            for (Tag tag : tagsArray) {
                tagsPredicateGroup.add(createSingleTagPredicate(tag));
            }
        }
searchGroup.add(tagsPredicateGroup);
Query query = queryBuilder.createQuery(searchGroup, session);
query.setHitsPerPage(10);
query.setStart(0);

private PredicateGroup createSingleTagPredicate(Tag tag) {
        Map<String, String> tagMap = new HashMap<String, String>();
        tagMap.put("tagid.property", "jcr:content/cq:tags");
        tagMap.put("tagid", tag.getTagID());
        return PredicateGroup.create(tagMap);
    }

如果组件配置为搜索geometrixx-outdoors:apparel和geometrixx-media:events标签;然后它还返回那些用geometrixx-outdoors标记的页面:服装/外套和geometrixx-media:事件/节日。如何使用querybuilder使其仅搜索父标记而不搜索子标记?

1 个答案:

答案 0 :(得分:1)

标签谓词旨在将子标签和第二种形式(基于路径)考虑在内。如果您不想要这个并且想要完全匹配,则需要像常规属性一样查询它。你的querymap看起来应该与此类似。

map.put("path","/content/geometrixx-outdoors");
map.put("type","cq:Page");
map.put("property","jcr:content/cq:tags");
map.put("property.value","geometrixx-outdoors:apparel");

如果您需要查询多个标记ID,则应该是OR'd =&gt; (“A”或“B”或“C”)

map.put("path","/content/geometrixx-outdoors");
map.put("type","cq:Page");
map.put("property","jcr:content/cq:tags");
map.put("property.1_value","geometrixx-outdoors:apparel");
map.put("property.2_value","geometrixx-media:events");
map.put("property.3_value","mynamespace:my-tag");

如果您需要查询多个标记ID,则应该是AND'd =&gt; (“A”,“B”和“C”)

map.put("path","/content/geometrixx-outdoors");
map.put("type","cq:Page");
map.put("property","jcr:content/cq:tags");
map.put("property.and","true");
map.put("property.1_value","geometrixx-outdoors:apparel");
map.put("property.2_value","geometrixx-media:events");
map.put("property.3_value","mynamespace:my-tag");

重要提示,我只展示了对id的查询。如果您不想错过任何可能的结果,您还应该将相关标签的路径添加到查询中,以便在相关字段中查询“tag id”和“tag path”,因为标记路径是“标签”的有效值。