Highlighting in Hybris Search
Introduction
Many applications like to highlight snippets of text from each search result so the user can see why the document matched the query. Hybris out-of-the-box isn’t able to highlight words in the search results. Hybris also doesn’t use a free 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, a part of hybris, does. So the solution, and the complexity, is in the integration.Solution
In my solution: (1) SOLR needs a designated field for highlighting. This field should contain the texts from all textual attributes of the product. (2) Additional parameters should be added to the SOLR query. (3) The highlighted fragments go separately from the products in the response.Technical details
(1) 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 the 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., (2) I implemented the custom FacetSearchStrategy (that extends DefaultFacetSearchStrategy) 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);
solrQuery.addHighlightField("allFields_text_en");
solrQuery.setHighlightSimplePre("<em>");
solrQuery.setHighlightSimplePost("</em>");
solrQuery.setHighlightFragsize(100);
solrQuery.setHighlightSnippets(3);
solrQuery.setHighlightRequireFieldMatch(true);
<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>
<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>
© Rauf Aliev, July 2016
Boufnichel Mohamed
1 September 2016 at 13:51
Great job !
Mikhail
29 June 2017 at 11:51
We’ve done something similar but had to turn it off because request time in solr increased significantly.
Aliaksei
21 September 2017 at 07:58
Finally, it’s a part of 6.5