search for text in content fragments ("search text containing in any property or associated tags in content fragments") | Community
Skip to main content
sreenu539
Level 7
April 28, 2023
Solved

search for text in content fragments ("search text containing in any property or associated tags in content fragments")

  • April 28, 2023
  • 2 replies
  • 1492 views

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/java/com/adobe/aem/sample/simple/search/impl/SearchResultsImpl.java#L68

 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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Shiv_Prakash_Patel

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

 

2 replies

Shiv_Prakash_Patel
Community Advisor
Shiv_Prakash_PatelCommunity AdvisorAccepted solution
Community Advisor
April 29, 2023

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

 

Shiv Prakash
Nikhil-Kumar
Community Advisor
Community Advisor
May 2, 2023

@sreenu539 - You can create your custom query builder and look for the search text under the specific CF path.