Issues with XPath Queries
I have a servlet that I use to search the JCR based on a user-provided query. I log the string that is to be used as the query before running it. However, I get a parse error when running the query and the error shows a differently formatted query than the string logged. Am I doing something wrong or is there a bug in the stack that parses/formats the query? Below is a servlet that should produce the error and the log messages I get. The query used is "ma'a salama". req below is the object from SlingHttpServletRequest in the servlet's doGet method.
RequestParameter queryString = req.getRequestParameterMap().getValue("q");
String replace = org.apache.jackrabbit.util.Text.escapeIllegalXpathSearchChars(queryString.getString()).toLowerCase().replace(' ', '%');
String query = String.format(
"/jcr:root/content/sample/en/home//*[@sling:resourceType='sample/components/page/person-bio' and (jcr:like(fn:lower-case(title), '%%%s%%') or jcr:like(fn:lower-case(bioinfo), '%%%s%%'))]",
replace,
replace);
LOG.info(query);
Iterator<Resource> resources = req.getResourceResolver().findResources(query, "xpath");
The logs look like this:
05.02.2019 10:09:56.697 *INFO* [0:0:0:0:0:0:0:1 [1549346996695] GET /bin/servlets/person/ HTTP/1.1] com.sample.servlet.PersonFinderServlet /jcr:root/content/sample/en/home/academics/divisions//*[@sling:resourceType='sample/components/page/person-bio' and (jcr:like(fn:lower-case(title), '%ma'a%salama%') or jcr:like(fn:lower-case(bioinfo), '%ma'a%salama%'))]
05.02.2019 10:09:56.699 *ERROR* [0:0:0:0:0:0:0:1 [1549346996695] GET /bin/servlets/person/ HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught SlingException
javax.jcr.query.InvalidQueryException: java.text.ParseException: Query:
/jcr:root/content/sample/en/home/academics/divisions//*[@sling:resourceType='sample/components/page/person-bio' and (jcr:like(fn:lower-case(title), '%ma'a(*)%salama%') or jcr:like(fn:lower-case(bioinfo), '%ma'a%salama%'))]; expected: )
Notice how in the query in the error statement has (*) inserted in the middle. Can anyone shed some light on this? I am using AEM 6.2 with CFP 18.