Introduction
Many applications highlight snippets of text from each search result so the user can see why the document matched the query. Hybris, out of the box, cannot highlight words in search results.
Hybris also does not use full-text search for product attributes from the classification catalog. This post explains how to make these attributes searchable and how to highlight search results.

Complexity
Hybris does not support highlighting. However, Apache Solr, which is part of Hybris, does. So the solution—and the complexity—lies in the integration.
Solution
In my solution:
Solr needs a designated field for highlighting. This field should contain text from all textual attributes of the product.
Additional parameters should be added to the Solr query.
The highlighted fragments are returned separately from the products in the response.
Technical details
- I added the custom field Product.allFields to the Product model.
Each time the product attributes are changed, the PrepareInterceptor rebuilds this field.
For example, in product #1934406 from the demo shop, the field is populated with the following data (CSV):
HDR-CX105E Red, – 1920x1080i HD video recording using AVCHD format<br/>-Records up to 3 hours HD video on 8 GB internal flash memory plus HYBRID recording on optional Memory Stick™<br/>-Exmor™ CMOS sensor for brilliant picture quality with high sensitivity and low noise<, 4905524596595, Sony, 2.36 Megapixel CCD (1/5″”), 8 GB Flash Memory, Memory Stick, MPEG2 (1920 × 1080), JPEG (max.2304 × 1728 Pixel), 10x Zoom, 120x Digitaler Zoom, SteadyShot image stabilization, 2.7” Wide LCD, PictBridge, Memory Stick (Duo/Pro Duo) Slot, USB 2.0 (Out), Video (Out), Audio (Out), S-Video (Out), Component Video (out), HDMI (out),Li-Ion, Camcorder tape type: Flash memory., Camera shutter speed: 1/2 – 1/1000s, Minimum illumination: 5.0lx, Source data-sheet: ICEcat.biz., Power consumption: 3.5W, Recording speed: HD, FH (1920x1080i, 16Mbps) / HQ (9Mbps) / SP (7Mbps) / LP (5Mbps) , SD., Internal memory: 8000.0MB, Display: LCD., Display diagonal: 2.7″, Display resolution: 211200.0pixels, Digital zoom: 120.0x, Aperture range: 1.8., 35 mm camera lens equivalent: 42.0mm, Focal length: 3.2mm, Filter size: 30.0mm, Megapixel: 2.36MP, Optical zoom: 10.0x, Lens system: Carl Zeiss Vario-Sonnar T*., USB 2.0 ports quantity: 1.0., Video out: 1.0., Audio output: 1.0., HDMI ports quantity: 1.0., S-Video out: 1.0., Depth: 107.0mm, Weight: 280.0g, Height: 60.0mm, Width: 55.0mm, Battery type: NP-FH60., Battery life: 1.5h, Audio system: Dolby Digital 2ch., Colour of product: Red., Compatible memory cards: Memory Stick., Optical sensor resolution: 2360000.0pixels, Effective sensor resolution: 1990000.0pixels, Optical sensor size: 1/5.0″, Sensor type: Exmor CMOS Sensor., Video capture resolution: 1920 × 1080pixels, Still image capture resolutions: 2304 × 1728%, Bundled software: Picture Motion Browser.,- I implemented a custom
FacetSearchStrategythat extendsDefaultFacetSearchStrategy.
In the middle of the search method, before solrClient.query(solrQuery), I injected the configuration for the Solr highlighter.
solrQuery.setHighlight(true);
solrQuery.addHighlightField("allFields_text_en");
solrQuery.setHighlightSimplePre("<em>");
solrQuery.setHighlightSimplePost("</em>");
solrQuery.setHighlightFragsize(100);
solrQuery.setHighlightSnippets(3);
solrQuery.setHighlightRequireFieldMatch(true);- I parse the response from Solr. With the configuration above, the Solr response will have an additional block for highlighting.
<lst name="highlighting">
<lst name="electronicsProductCatalog/Online/816379">
<arr name="allFields_text_en">
<str>
SAL-1680Z - DT 16-80mm F3.5-4.5 ZA <em>Carl</em> <em>Zeiss</em> Vario-Sonnar T* zoom lens, Minimum aperture: 22 - 29., Viewing
</str>
</arr>
</lst>
<lst name="electronicsProductCatalog/Online/23231">
<arr name="allFields_text_en">
<str>
and get better results. Armed with high-quality <em>Carl</em> <em>Zeiss</em> optics, ISO sensitivities of 100/200/400/800
</str>
<str>
5.2Mpix 8MB <em>Carl</em> <em>Zeiss</em> Lens Zoom ring High speed USB 10xdig.Zoom PEG, Display: LCD TFT 1.8"" color".,
</str>
</arr>
</lst>
...
</lst>I extended SearchResponseResultsPopulator to parse this information and replace product.summary with the snippet if it exists. Otherwise, the standard summary data is displayed.
© Rauf Aliev, July 2016