Hi @bhroadking
To query web pages and PDF files together and order the results by relevance in AEM 6.5, you can use the QueryBuilder API and leverage the full-text search capabilities of Apache Jackrabbit Oak
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.Query;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.result.SearchResult;
// Inject the QueryBuilder service
@Reference
private QueryBuilder queryBuilder;
// Perform the search
String searchTerm = "your search term";
int limit = 10; // Number of results to retrieve
Map<String, String> predicates = new HashMap<>();
predicates.put("path", "/content"); // Path to search within
predicates.put("type", "cq:PageContent"); // Type of content to search (e.g., cq:PageContent, dam:Asset)
predicates.put("group.p.or", "true"); // Combine predicates with OR operator
// Add full-text search condition for web pages
predicates.put("group.1_fulltext", searchTerm);
predicates.put("group.1_fulltext.relPath", "jcr:content");
// Add full-text search condition for PDF files
predicates.put("group.2_fulltext", searchTerm);
predicates.put("group.2_fulltext.relPath", "jcr:content/metadata");
Query query = queryBuilder.createQuery(PredicateGroup.create(predicates), session);
query.setHitsPerPage(limit);
SearchResult result = query.getResult();
// Process the search results
for (Hit hit : result.getHits()) {
// Process each search result
}
we use the QueryBuilder service to create a query with multiple predicates. We specify the path to search within (/content in this case) and the type of content to search (e.g., cq:PageContent for web pages, dam:Asset for PDF files).
To perform a full-text search on web pages, we add a predicate with the fulltext property set to the search term and the relPath property set to jcr:content. This searches for the search term within the content of the web pages.
Similarly, to perform a full-text search on PDF files, we add a predicate with the fulltext property set to the search term and the relPath property set to jcr:content/metadata. This searches for the search term within the metadata of the PDF files.
By combining these predicates with the group.p.or property set to true, we ensure that the search results include both web pages and PDF files and are ordered by rele
vance.