AEM 6460 - Query Builder Predicate Evaluator for searching assets by metadata property ignore case

Goal


Add a Query Builder predicate evaluator to search for assets by Metadata property ignoring case...

Query generated by this code is something like...

/jcr:root/content/dam/experience-aem//element(*, dam:Asset)[jcr:contains(jcr:content/metadata/@eaem:assetDescription,  'vIdEo')] 
order by jcr:content/@jcr:lastModified descending

Avoid using fn:lower-case() ; it might result in slowness depending on repo size...

/jcr:root/content/dam/experience-aem//element(*, dam:Asset)[((fn:lower-case(jcr:content/metadata/@eaem:assetDescription) =  'video'))] 
order by jcr:content/@jcr:lastModified descending


Solution


1) Create a Predicate evaluator IgnoreCaseJcrPropertyPredicateEvaluator extending com.day.cq.search.eval.JcrPropertyPredicateEvaluator

package com.experienceaem.assets;

import com.day.cq.search.Predicate;
import com.day.cq.search.eval.EvaluationContext;
import com.day.cq.search.eval.JcrPropertyPredicateEvaluator;
import org.apache.commons.lang3.StringUtils;
import org.osgi.service.component.annotations.Component;
import com.day.cq.search.eval.XPath;
@Component(
        factory = "com.day.cq.search.eval.PredicateEvaluator/property"
)
public class IgnoreCaseJcrPropertyPredicateEvaluator extends JcrPropertyPredicateEvaluator {
    private String JCR_METADATA_PREFIX = "(jcr:content/metadata/";

    public String getXPathExpression(Predicate p, EvaluationContext context) {
        String xPathExpr = super.getXPathExpression(p, context);

        if(StringUtils.isEmpty(xPathExpr)){
            return xPathExpr;
        }

        if(xPathExpr.startsWith(JCR_METADATA_PREFIX)){
            String value = xPathExpr.substring(xPathExpr.indexOf("=") + 1);

            value = value.toLowerCase();
            xPathExpr = "jcr:contains" + xPathExpr.substring(0, xPathExpr.indexOf("=")) + "," + value;
        }

        return xPathExpr;
    }
}

No comments:

Post a Comment