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

     
  • 相关阅读:
    7月17日
    7月16日学习记录
    7月15日学习记录
    git 学习
    投稿相关
    ubuntu16.04 安装以及要做的事情
    python学习使用
    图像相关
    不识别移动硬盘
    深度学习
  • 原文地址:https://www.cnblogs.com/blogkevin/p/10690241.html
Copyright © 2011-2022 走看看