我自己没有使用过该SimpleSearch
机制,但是文档指出您可以创建一个包含多个谓词的PredicateGroup
(的子类),并且可以通过调用来指定逻辑运算符(AND/OR) 。Predicate
setAllRequired()
http://dev.day.com/docs/en/cq/5-3/javadoc/com/day/cq/search/PredicateGroup.html
也许您可以尝试:
PredicateGroup group = new PredicateGroup("tags");
// This will set operator between each predicate to OR
group.setAllRequired(false);
Predicate tagPredicate = new Predicate("tags");
tagPredicate.set(JcrPropertyPredicateEvaluator.OPERATION, JcrPropertyPredicateEvaluator.OP_LIKE);
tagPredicate.set(JcrPropertyPredicateEvaluator.PROPERTY, "./jcr:content/cq:tags");
tagPredicate.set(JcrPropertyPredicateEvaluator.VALUE, cookieValue);
group.add(tagPredicate);
Predicate metaTagPredicate = new Predicate("metatags");
metaTagPredicate.set(JcrPropertyPredicateEvaluator.OPERATION, JcrPropertyPredicateEvaluator.OP_LIKE);
metaTagPredicate.set(JcrPropertyPredicateEvaluator.PROPERTY, "./jcr:content/metadata/cq:tags");
metaTagPredicate.set(JcrPropertyPredicateEvaluator.VALUE, cookieValue);
group.add(metaTagPredicate);
search.addPredicate(group);
// To debug and view your predicate tree you can invoke toString() on your predicate group
编辑:忘了提及,使用JcrPropertyPredicateEvaluator
,您可以执行类似于 的操作jcr:like()
。对于jcr:contains()
,您可能希望使用全文评估器。
或者,您可以直接构建并执行 JCR 查询。我发现它比创建谓词和使用QueryBuilder API
. 这是一个非常简单的 XPath 示例:
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("/jcr:root//*[jcr:contains(@cq:tags,");
queryBuilder.append(cookieValue);
queryBuilder.append(")] OR [jcr:contains(@metadata/cq:tags,");
queryBuilder.append(cookieValue);
queryBuilder.append(")]);
QueryManager manager = session.getWorkspace().getQueryManager();
Query query = manager.createQuery(queryBuilder.toString(), "xpath");
QueryResult result = query.execute();
// From the result you can then retrieve a NodeIterator to loop through the result set
http://www.day.com/maven/jsr170/javadocs/jcr-2.0/javax/jcr/query/Query.html