使用querybuilder做忽略大小写查询的例子

2019/04/11 15:56
阅读数 112

自定义Predicate:

 

import com.day.cq.search.Predicate;
import com.day.cq.search.eval.AbstractPredicateEvaluator;
import com.day.cq.search.eval.EvaluationContext;
import org.apache.felix.scr.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.query.Row;

@Component(
        metatype = true,
        factory = "com.day.cq.search.eval.PredicateEvaluator/caseinsensitive"
)
public class CaseInsensitiveLikePredicate extends AbstractPredicateEvaluator {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public static final String PROPERTY = "property";
    public static final String VALUE = "value";
    public static final String WILDCARD = "%";

    @Override
    public boolean includes(Predicate predicate, Row row, EvaluationContext context) {
        if (predicate.hasNonEmptyValue(PROPERTY)) {
            return true;
        }
        return super.includes(predicate, row, context);
    }

    @Override
    public String getXPathExpression(Predicate predicate, EvaluationContext context) {
        if (!predicate.hasNonEmptyValue(PROPERTY)) {
            return null;
        }
        if (predicate.hasNonEmptyValue(PROPERTY) && null == predicate.get(VALUE)) {
            return super.getXPathExpression(predicate, context);
        }
        if (predicate.hasNonEmptyValue(PROPERTY)) {
            predicate.get(VALUE);
            if (WILDCARD.equals(predicate.get(VALUE))) {
                logger.info("Case Insensitive Query only has wildcard, ignoring predicate");
                return "";
            }
            logger.info("jcr:like(fn:lower-case(" + predicate.get(PROPERTY) + "), '" + predicate.get(VALUE).toLowerCase() + "')");
            return "jcr:like(fn:lower-case(" + predicate.get(PROPERTY) + "),'" + predicate.get(VALUE).toLowerCase() + "')";
        }
        return null;
    }
}

 

使用predicate:

if (!StringUtils.isBlank(q)) {
            map.put("group.p.or", "true");
            map.put("group.1_caseinsensitive.value", "%" + q + "%");
            map.put("group.1_caseinsensitive.property", "jcr:title");
            map.put("group.2_caseinsensitive.value", "%" + q + "%");
            map.put("group.2_caseinsensitive.property", "text1");
            map.put("group.3_caseinsensitive.value", "%" + q + "%");
            map.put("group.3_caseinsensitive.property", "text2");
        }

参考 https://forums.adobe.com/thread/2326696

 

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部