使用查询生成器(http://localhost:4502/libs/cq/search/content/querydebug.html
),我想要获得没有jcr:content
子节点的页面列表。
我尝试使用节点,项目名称等,但无法找到正确的查询。谢谢你的帮助。
path=/content/products
type=cq:Page
node=jcr:content
node.operation=exists
node.operation=not
p.limit=-1
CQ5 Query Builder将提供的查询转换为Jackrabbit XPath查询。后者不支持测试孩子的存在性。下面的XPath理论上应该可以工作:
/jcr:root/content//element(*, cq:Page)[not(jcr:content)]
,但结果为空。有一个JIRA改进来添加这样的功能,但它看起来被放弃了。
所以,我们必须手动检查。因为CQ谓词不提供这样的功能(没有您在查询中使用的node
谓词),我们需要编写一个新的谓词:
@Component(metatype = false, factory = "com.day.cq.search.eval.PredicateEvaluator/child")
public class ChildrenPredicateEvaluator extends AbstractPredicateEvaluator {
public boolean includes(Predicate p, Row row, EvaluationContext context) {
final Resource resource = context.getResource(row);
final String name = p.get("name", "");
final boolean childExists;
if (name.isEmpty()) {
childExists = resource.hasChildren();
} else {
childExists = resource.getChild(name) != null;
}
final String operator = p.get("operator", "exists");
if ("not_exists".equals(operator)) {
return !childExists;
} else {
return childExists;
}
}
public boolean canXpath(Predicate predicate, EvaluationContext context) {
return false;
}
public boolean canFilter(Predicate predicate, EvaluationContext context) {
return true;
}
}
我们可以这样使用:
child.name=wantedChild
child.operator=exists
// or
child.name=unwantedChild
child.operator=not_exists
您也可以跳过child.name
行来检查是否存在子节点。
因此,使用此谓词的查询看起来像这样:
path=/content/products
type=cq:Page
child.name=jcr:content
child.operator=not_exists
p.limit=-1