How to Implement Search with Query Builder API for Partial Search Text in CQ / AEM
I have a requirement to get search results based on a partial text match. For example, if the products have a node, say "apple-iphone-6" and the user enters the text "iphone" in the search box, I should still get the result.
I tried the following query on querybuilder and it worked:
http://localhost:4502/bin/querybuilder.json?path=/etc/commerce/products&type=nt:unstructured&nodename=*iphone*
But how to implement this programmatically for a part *iphone*
? I am creating a query using predicates as follows
String searchTerm = "iphone";
map.put("path", "/etc/commerce/products");
map.put("type", "nt:unstructured");
map.put("nodename", searchTerm);
Query query = queryBuilder.createQuery(PredicateGroup.create(map), session);
SearchResult result = query.getResult();
But I am not getting any results, the reason is that the node name (apple-iphone-6) does not exactly match the search term (iphone). But the same works great in case I add * to the nodename value, which then implements partial text search in the querybuilder example. What change should I make in the code to get results based on partial node name match?
source to share
You've already found the solution yourself, NodenamePredicateEvaluator takes wildcards, so you need to surround your search query with wildcards, like this:
String searchTerm = "iphone";
...
map.put("nodename", "*" + searchTerm + "*");
source to share