Score:1

Search API Solr: How to highlight search term in indexed HTML?

in flag

How can I configure Solr to index a search result view mode, and render that indexed content with the search terms highlighted without highlighting text in attributes?

We are indexing a "Search Result" view mode for every node and then using views to render that indexed content on a search results page. This view mode is very simple (see markup below). The problem is that if a search term appears in an attribute, e.g. the href of the link to the page, the highlighter attempts to highlight that search term and breaks the output.

What's really throwing me is that I have a D9 site with Solr 8.8, the same module versions, and the same config and this process works without trying to highlight attributes.

Things I've tried:

  1. On the server, there is an option to Retrieve highlighted snippets from the server - I really think this should be enabled, but enabling it fixes nothing and my D9 site does not (erroneously, I believe) have this enabled.
  2. In the view, I've tried disabling the "Use highlighted field data" option for the field. Unbelievably, this returns the rendered search result content with all HTML, except for the <a> tag, stripped (Huh??)

Search Result markup

This is this HTML that gets indexed by Solr.

<article>
  <h3><a href="/path/to/page">My Page Title</a></h3>
  <p>Lorem ipsum my page summary</p>
</article>

Broken example, e.g. a search for "page" This is the HTML that gets rendered with the keywords highlighted. Notice how the <a> tag gets clobbered.

<article>
  <h3><a>page">My <span class="highlight">Page Title</span></a></h3>
  <p>Lorem ipsum my <span class="highlight">page</span> summary</p>
</article>

Expected example, e.g. a search for "page": The search term gets highlighted in text, but not attributes.

<article>
  <h3><a href="/path/to/page">My <span class="highlight">Page</span> Title</a></h3>
  <p>Lorem ipsum my <span class="highlight">page</span> summary</p>
</article>

Search Page

We are using views to create a search page. The view's relevant config is:

  • Source is the Search Index Content (indexed as Fulltext)
  • Displaying fields, just the single "Rendered Search Result" view mode indexed field, configured with the "Use highlighted field data" checkbox checked.
  • An exposed filter to search several other indexed fields.

Search API configuration

  • Sever configured with "Retrieve result data from Solr" enabled
  • Indexing a "Rendered Item" (specifically node:search_result view mode)
  • Highlight Processor enabled with "Highlight field data" set to "Always" and "Highlight partial matches" enabled.

Versions

  • Drupal core: 10.0.3
  • Search API: 8.x-1.29
  • Search API Solr: 4.2.9
  • Solr: 8.11.1
id flag
Hello. As written I do not clearly understand what each code sample is exhibiting. Can you clarify that?
sonfd avatar
in flag
@cilefen - thanks, I think I cleared it up.
id flag
So the link is actually broken by an output filter?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.