I am trying to do "search for text in a content fragment or tags associated with content fragments".
I was trying to understand how this https://github.com/Adobe-Marketing-Cloud/aem-guides/blob/master/simple-search-guide/core/src/main/ja...
code doing search but could not get around how predicateresolver, searchprovider adding any predicates.
I assume below code is doing "any property contains search text" return the result node.
private void initModel() {
final long start = System.currentTimeMillis();
final Map<String, String> searchPredicates = predicateResolver.getRequestPredicates(request);
if (isSearchable()) {
com.day.cq.search.result.SearchResult result = searchProvider.search(resourceResolver, searchPredicates);
pagination = searchProvider.buildPagination(result, "Previous", "Next");
searchResults = searchProvider.buildSearchResults(result);
totalResults = computeTotalMatches(result);
timeTaken = result.getExecutionTimeMillis();
}
}
Any pointers on this. There are no predicateresolvers or factories given, so not sure how this code even constructs any predicates.
I am looking to see if there is a search api alredy build in aem or write query my self using aem predicates , query builder api to do "search text containing in any property or associated tags in content fragments"
Thanks.
Sri
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @sreenu539 ,
You can use fulltext search in content fragments to achieve this requirement, as shown below.
Here we are getting search text from a request in a servlet.
String fullSearchText = request.getParameter("searchText");
Map<String, Object> param = new HashMap<>();
param.put(ResourceResolverFactory.SUBSERVICE, SYSTEM_USER);
ResourceResolver resourceResolver = resolverFactory.getServiceResourceResolver(param);
session = resourceResolver.adaptTo(Session.class);
HashMap<String, String> hashMap = new HashMap<>();
// Add primary predicate to the HashMap
hashMap.put("path", "/content/dam/learning");
hashMap.put("type", "cq:ContentFragment");
// Add the full-text predicate to the HashMap
hashMap.put("fulltext", fullSearchText);
// Add the orderby parameter to the HashMap
hashMap.put("orderby", "@jcr:content/cq:lastModified");
hashMap.put("orderby.sort", "desc");
//Fetch all the result
hashMap.put("p.limit", "-1");
// Create the Query object from the HashMap
Query query = queryBuilder.createQuery(PredicateGroup.create(hashMap), session);
// Execute the query and get the results
query.setStart(0);
SearchResult result = query.getResult();
List<Hit> hits = result.getHits();
// Iterate through the results and do something with each hit
arrayList = new ArrayList<>();
for (Hit hit : hits) {
String path = hit.getPath();
//Custom code using result path
}
Hope this could help you !!!
Regards
Shiv
Hi @sreenu539 ,
You can use fulltext search in content fragments to achieve this requirement, as shown below.
Here we are getting search text from a request in a servlet.
String fullSearchText = request.getParameter("searchText");
Map<String, Object> param = new HashMap<>();
param.put(ResourceResolverFactory.SUBSERVICE, SYSTEM_USER);
ResourceResolver resourceResolver = resolverFactory.getServiceResourceResolver(param);
session = resourceResolver.adaptTo(Session.class);
HashMap<String, String> hashMap = new HashMap<>();
// Add primary predicate to the HashMap
hashMap.put("path", "/content/dam/learning");
hashMap.put("type", "cq:ContentFragment");
// Add the full-text predicate to the HashMap
hashMap.put("fulltext", fullSearchText);
// Add the orderby parameter to the HashMap
hashMap.put("orderby", "@jcr:content/cq:lastModified");
hashMap.put("orderby.sort", "desc");
//Fetch all the result
hashMap.put("p.limit", "-1");
// Create the Query object from the HashMap
Query query = queryBuilder.createQuery(PredicateGroup.create(hashMap), session);
// Execute the query and get the results
query.setStart(0);
SearchResult result = query.getResult();
List<Hit> hits = result.getHits();
// Iterate through the results and do something with each hit
arrayList = new ArrayList<>();
for (Hit hit : hits) {
String path = hit.getPath();
//Custom code using result path
}
Hope this could help you !!!
Regards
Shiv
@sreenu539 - You can create your custom query builder and look for the search text under the specific CF path.
Views
Like
Replies