zoukankan      html  css  js  c++  java
  • 使用querybuilder做忽略大小写查询的例子

    自定义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

     
  • 相关阅读:
    JSP教程(八)—— Servlet实现验证码
    JSP教程(七)—— JSP实现登录界面
    JSP教程(六)—— JSP实现整型加法
    JSP教程(五)—— JSP内置对象(下)
    windows下使用python2.7.6 安装django
    python 旧类中使用property特性的方法
    python 中property函数如何实现
    python 属性 property、getattr()、setattr()详解
    linux下调试使用的 一些shell命令
    shell脚本中处理 路径中中文和空格方法
  • 原文地址:https://www.cnblogs.com/blogkevin/p/10690241.html
Copyright © 2011-2022 走看看