Solr Search Query API: How to configure highlighting

I’m referring to the Solr Search Query API documentation and try to write own custom Solr queries. This works fine so far.

I’d also like to include highlighting of found keywords in the reponse snippets, as the regular Solr Search does. If I set

#set ($discard = $query.bindValue('hl', 'true'))

the response displayed by

#displaySearchResult($searchResult)

will indeed deliver the relevant text snippets, but without emphasizing the searched term (in the standard Solr Search, it’s bold) and including a lot of fields which I would not all want to see, e.g. “Rendered document content” as well as “Raw document content”, which is mostly redundant (in standard Solr Search, only “Rendered document content” is shown).

I assume I should probably assign some other parameters like ‘fl’, ‘hl.fl’ etc. (as mentioned in the documentation) but how to do that isn’t exactly clear from the XWiki documentation, while the Solr documentation is extensive but uses a more general query syntax and I’m not clear how to translate this into Velocity commands. Tried dozens of variations, all refusing to work. Is it at all possible to influence the highlighting response display from this API? Any help very appreciated.

Just for the record, been able to figure it out myself. Not sure though if this is the recommended way.

This has less to do with the Solr API but more the Velocity rendering code in XWiki. The main macro which renders the highlighting delivered by the Solr engine is called #displaySearchResultHighlighting and is defined in Main.SolrSearchMacros. I wrote a custom version of this macro which I stored in another macro page and invoked it additionally to the Main macro page. In this custom version, I edited the loop over “#foreach ($entry in $highlighting)” to achieve what I want. If anybody is interested I can share more details.

The standard Solr search page is only setting the “hl” parameter to enable highlighting, see xwiki-platform/SolrSearchMacros.xml at xwiki-platform-14.10 · xwiki/xwiki-platform · GitHub , so that should be enough for your also. When enabled, Solr wraps the matches as per configuration xwiki-platform/solrconfig.xml at xwiki-platform-14.10 · xwiki/xwiki-platform · GitHub using a <span class="search-text-highlight"> which has the bold styles applied xwiki-platform/SolrSearch.xml at xwiki-platform-14.10 · xwiki/xwiki-platform · GitHub . All this should work for you also (if you load the styles from Main.SolrSearch of course).

That is not true. Click on “Highlight all matches” and you will see “Raw document content”:

solr-search

The list of matched / highlighted fields is influenced by the query fields parameter (“qf”). If you don’t want to highlight “Raw document content” then:

  • either remove doccontentraw from query fields (but it may affect the search results as it won’t search in this field anymore)
  • or use a custom code to display the highlights, that ignores some fields such as “Raw document content” but then it would be strange to see a search result without no highlighting because it was matched in doccontentraw but not in doccontent.

You shouldn’t need to do this, unless you really want to change how the search results / highlighting is displayed.

Thanks for your reply.

Can it be that the bolding of the highlighted keyword is only available since v14? I’m using 13.10 (the LTS) and there, at least in my Docker install, it’s not the case (standard Iceberg theme). I can however see the span’s with this class when I inspect the element, so ofc it’s easy to add custom CSS to emphasize it.

Sorry my wording was incorrect. I didn’t mean to say the standard Solr search can only show rendered doccontent highlighting, what I meant was that as per default, it does just that and I did not know how to achieve that via the API.

Thanks for the hint using the qf parameter, that helps indeed!

The highlight styles (bold) are pretty old, so you should definitely have them in 13.10. I see them in xwiki-platform/SolrSearch.xml at xwiki-platform-13.10 · xwiki/xwiki-platform · GitHub . Isn’t search-text-highlight the CSS class you see on the SPAN element that wraps the highlighted term? You could edit the Main.SolrSearch page with the object editor to see if the SSX object contains this style, but I don’t see why you wouldn’t have it.

Ah, that was a misunderstanding. As I wrote (maybe not clearly enough), on Main.SolrSearch - the page I referred to when I wrote “standard Solr Search” above - the styling is there. But I’ve been using the API to set up my own search page writing my own custom queries and displaying the results also on this page by calling #displaySearchResult($searchResult). Then, the styling is not there by default. Now I understand why: The styling of search-text-highlight in Main.SolrSearch is not global but only on demand (which makes sense as it’s something more specific) so I have to add the CSS myself or invoke the CSS from Main.SolrSearch.

Exactly.