Enhancing AEM Lucene Search: Advanced Techniques for Improved Search Functionality : Part 3

Kiran Mayee N
Activate AEM
Published in
5 min readJul 30, 2024

Hey there, AEM users!

You’ve conquered the initial search hurdle — you’ve found the relevant content! But sifting through lengthy content previews can still be time-consuming. What if you could instantly identify the information you need?

This article dives into the world of crafting compelling search results on AEM. We’ll explore features like keyword highlighting and controlled excerpts, designed to make your search experience efficient and user-friendly. Imagine finding the exact answer you seek within seconds, thanks to highlighted keywords that pinpoint the most relevant sections of the content.

Building on a Strong Foundation

This article assumes you’ve read the previous installments in our AEM search optimization series. If you haven’t yet, we recommend starting with “Optimizing Search with AEM Indexing Management” to ensure your search engine has a solid foundation of well-indexed content.

Upcoming Article in this Series!

  • Empower User Searches: Synonyms, Filters & Spell Check : Expanding search capabilities to include synonyms and filters, and helping users out with those pesky typos.

Previously Covered in this series:

Now, let’s get started on crafting search results that truly impress!

Craft Compelling Search Results: Highlighting Keywords & Controlled Excerpts

Imagine searching for a specific topic within your AEM website. Highlighting the search terms within the results is helpful, but wouldn’t it be even better to see a surrounding snippet of text providing context? This is where excerpts come in.

Excerpts and Keyword Highlighting within

Excerpts are brief text passages extracted from the content around the matched keywords. By displaying excerpts alongside search results, you offer users a glimpse into the content’s relevance without requiring them to visit every single page. This can significantly improve the search experience by:

  • Providing Context: Users can quickly understand how the search term is used within the content, improving their ability to judge relevance at a glance.
  • Saving Time: By previewing the content, users can determine if a specific result meets their needs without needing to navigate to the full page.
  • Enhancing Engagement: A more informative search experience keeps users engaged and focused on finding the information they seek.

Enabling Excerpts in AEM

Adobe Experience Manager (AEM) provides two primary methods for utilizing excerpts in search results:

1. p.excerpt in Query Predicates (Limited Functionality)

  • p.excerpt refers to the ability to include excerpts of matching content in search results.
  • However, in current versions of AEM, enabling p.excerpt in query predicates might not directly display excerpts within the search results themselves until hit.getExcerpt is used to extract the Excerpt.
  • The documented behavior might be outdated or specific to certain AEM configurations or debugging tools like the Query Builder debugger shown below.

Disclaimer:

While my testing suggests limitations with p.excerpt in conjunction with hit.getExcerpt led to redundant result hits programmatically and slower server response times.

It’s important to acknowledge that others might have different experiences or AEM configurations that yield different results. If you have a different perspective on p.excerpt functionality, please share your insights!

2. hit.getExcerpt Method (Recommended Approach):

  • When reading search results programmatically, developers can use the hit.getExcerpt() method to retrieve the excerpt of the matched content directly from the search result object.
  • This method allows developers to programmatically access and display excerpts in custom search result templates or components.

Example 1 using p.excerpt=true

2_group.fulltext = keyword
2_group.p.or = true
9_orderby = orderbydate
9_orderby.sort = desc
group.1_group.property = sling:resourceType
group.1_group.property.1_value = generic-company/components/content/article
group.1_group.type = nt:unstructured
group.2_group.property = jcr:content/sling:resourceType
group.2_group.property.value = generic-company/components/structure/page/basepage
group.2_group.type = cq:Page
group.p.or = true
p.facets = true
p.limit = 10
p.offset = 0
p.excerpt = true
path = /content/generic-company/content/

Example 2 using hit.getExcerpt

private static Map<String, Object> addOtherData(final Hit hit, Map<String, Object> map)
throws RepositoryException {
final Resource resource = hit.getResource();
final ValueMap properties = resource.adaptTo(ValueMap.class);
map.put(CF_PATH, resource.getPath());
map.put(CF_NAME, resource.getName());
map.put(CF_TITLE, properties.get("jcr:title", resource.getName()));
map.put(CF_EXCERPT, hit.getExcerpt());
map.put(CF_LAST_MODIFIED, getLastModified(resource));
map.put(CF_TYPE, "Data");
return map;
}

Results:

Explanation:

The excerpt generation process in AEM involves highlighting the matched keywords within the retrieved content. AEM automatically generates excerpts for search results, and it includes HTML markup to highlight the matched keywords, typically using <strong> tags. This highlighting method visually distinguishes the matched keywords within the excerpts, making it easier for users to identify relevant content snippets.

Ever searched for something online and ended up with results that felt incomplete or irrelevant? One reason for this can be generic excerpts that don’t accurately reflect the content’s core message. Thankfully, AEM’s Controlled Excerpt Properties offer a solution.

Controlled Excerpt Properties:

What are Controlled Excerpt Properties?

This feature empowers you to customize the properties used to generate excerpts displayed alongside search results. By specifying which properties to consider — like title, description, or custom fields — you can significantly improve the relevance and user experience of your AEM search.

Benefits of Controlled Excerpt Properties:

  • Enhanced Relevance: Users can quickly grasp the content’s focus by seeing relevant excerpts that highlight the search term within the context of specific properties. This helps them determine if a result is truly what they’re looking for, saving them valuable time and clicks.
  • Improved User Experience: More informative excerpts lead to a more engaging search experience. Users can make informed decisions about which results to explore further based on the preview provided by the excerpt.
  • Greater Control: You have the power to define which properties best represent the content and showcase the search term’s relevance. This level of control ensures excerpts accurately reflect your content and user needs.

How to Define Excerpt Properties:

AEM utilizes the com.day.cq.search.impl.builder.QueryBuilderImpl.cfg.json file to configure various search functionalities. Within this file, you can define the properties you want to use for generating excerpts by specifying them under the “excerpt.properties” key. Here’s an example:

{
"excerpt.properties": [
"jcr:description",
"text",
"jcr:title",
"bodyText"
]
}

In this example, the excerpt will be generated by pulling text from the jcr:description, text, jcr:title, and bodyText properties of the content items.

Happy searching! Excerpts and highlighted keywords are a powerful combo for user-friendly AEM search results. Stay tuned for the last article in this series “Empower User Searches: Synonyms, Filters & Spell Check”!

--

--