Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Case insensetive properties: AEM 6.5: Query Builder API

Avatar

Level 5

How can I implement case insensetive property queries in group?

 

Here's an example:

 

nodename=master
group.p.or=true
group.1_property=title
group.1_property.value=%article
group.1_property.operation=like

 

I want to do something like: `group.1_property.caseSenstivity=false`

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

We just need to deploy the Predicate as part of our core bundle.

Onc deployed, we should be able to use the custom predicates


Aanchal Sikka

View solution in original post

5 Replies

Avatar

Community Advisor

Hi,

 

As far as I know there is no native Query Builder Predicate for doing case insensitive queries. 

You can create a custom predicate for an application. 

 

Add the below code. The predicate works by lower casing the value and then using the XPath fn:lower-case method to compare to the field value in lower case.

 

*
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to 
*/
package com.perficient.adobe.predicates;

import java.util.Locale;
import java.util.Optional;

import com.day.cq.search.Predicate;
import com.day.cq.search.eval.AbstractPredicateEvaluator;
import com.day.cq.search.eval.EvaluationContext;

import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(factory = "com.day.cq.search.eval.PredicateEvaluator/equalsIgnoreCase")
public class CaseInsensitiveEquals extends AbstractPredicateEvaluator {

    private static final Logger log = LoggerFactory.getLogger(CaseInsensitiveEquals.class);

    static final String PREDICATE_PROPERTY = "property";
    static final String PREDICATE_VALUE = "value";
    static final String PREDICATE_LOCALE = "locale";

    @Override
    public String getXPathExpression(Predicate predicate, EvaluationContext context) {
        log.debug("Evaluating predicate: {}", predicate);
        String property = predicate.get(PREDICATE_PROPERTY);
        Locale locale = Optional.ofNullable(predicate.get(PREDICATE_LOCALE)).map(lt -> Locale.forLanguageTag(lt))
                .orElse(Locale.getDefault());
        String value = predicate.get(PREDICATE_VALUE).toLowerCase(locale).replace("'", "''");
        String query = String.format("fn:lower-case(@%s)='%s'", property, value);
        log.debug("Generated query: {}", query);
        return query;
    }
} 

Once the custom predicate is available in your application, it can be used in any Query Builder query as such:

path=/content/test
equalsIgnoreCase.property=test
equalsIgnoreCase.value=test
equalsIgnoreCase.locale=en_US 

Avatar

Level 5

Where is this file supposed to be placed? I am testing in my local host. Also Instead of doing it like this I would like to extend `property` predicate to `caseInsenstiveProperty` predicate can you help me understand the code for that?

Avatar

Community Advisor

@spidey1405 
You can try with "Fulltext" search in querybuilder to get the list of all pages which contains certain string but if you want specific case sensitive then you need to have custom predicate to achieve the requirement
https://blogs.perficient.com/2020/11/11/case-insensitive-queries-with-the-aem-query-builder/

Avatar

Level 5

Hi @BrijeshYadav thanks for the response. Fulltext behaves slightly different, I wish I could restrict it to specific properties.

The URL that you have shared I already have looked into it but I want to understand where am I supposed to add it in aem local file? I have a `crx-quickstart` folder but not sure where is this code going to sit in order for me to be able to include my custom predicate.

Avatar

Correct answer by
Community Advisor

We just need to deploy the Predicate as part of our core bundle.

Onc deployed, we should be able to use the custom predicates


Aanchal Sikka