Skip to content

Show literal angle brackets in highlighted search results - #5038

Merged
akira28 merged 4 commits into
mainfrom
feature/escape-search-highlights
Nov 24, 2026
Merged

akira28 merged 4 commits into
mainfrom
feature/escape-search-highlights

Conversation

@akira28

@akira28 akira28 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Search results now show literal text such as <tenant> in titles and descriptions. Before, the result row read it as an HTML tag and dropped it.

Affects: Search, Site UI

Prompt summary: A review comment on #5036 found that a heading such as The <tenant> parameter loses <tenant> in the search row. The author asks to fix that in its own PR, since every search result type has the problem, not only API pages.

Why

The navigation search and the full-page search render each result's title and description as HTML, so the <mark> highlights show. SearchResultProcessor builds those strings from the document text and the Elasticsearch fragments, but it never encodes them. Any <…> in a title, description or body fragment reaches the browser as a tag. DOMPurify removes it, or ResultCard injects it as raw HTML. This already affects operation summaries and descriptions with placeholders as well.

What

Encoded highlights

Both highlight requests set encoder: html, so Elasticsearch encodes the document text and returns only its own <mark> tags raw. A literal <mark> in a document therefore never pairs with a real highlight. ReencodeHighlight decodes the text between the marks and encodes it again with Encode, which touches only &, <, > and ". A title or description without a fragment is encoded whole.

Plain text without highlighting

SearchResultProcessor.ProcessHit returns HTML only when the caller passes highlight options. Without them, the result keeps the document's plain text, so the MCP search tools, which turn highlighting off, see no entities.

Token marking on encoded text

The post-pass that marks query tokens looks up synonyms by the typed token, then encodes each candidate before matching it (TokenMatching.Encoded). So a typed <tenant> marks &lt;tenant&gt;, and a synonym such as R&D still marks. A match that starts or ends inside an entity is skipped, so typing lt or amp never breaks &lt; or &amp;.

AI answer panel

AIAnswerPanel reads the plain text of each result for the model context and for its Sources links, so neither shows entities or <mark> tags.

Verify

dotnet test --project tests/Elastic.Documentation.Api.Tests --treenode-filter "/*/*/HighlightEncodingTests/*"
./build.sh unit-test
mise run site:test

🤖 Generated with Claude Code

@akira28
akira28 requested a review from a team as a code owner November 24, 2026 15:56
@akira28 akira28 added the bug label Nov 24, 2026
@akira28
akira28 requested a review from reakaleek November 24, 2026 15:56
akira28 and others added 4 commits November 24, 2026 18:46
The navigation and full-page search render a result's title and description
as HTML, but the processor passed literal text through unencoded. A title
such as `The <tenant> parameter` lost `<tenant>` as a stripped tag. With
highlight options, the processor now encodes the text outside its `<mark>`
tags, encodes typed tokens the same way, and never marks inside an entity.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`IsInsideEntity` computes its search window once, and the highlight loop calls
it for both ends of a match directly, without a wrapper.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Elasticsearch now encodes the highlighted text, so a literal `<mark>` in a
document stays text and never pairs with a real highlight. The processor
normalizes those fragments, encodes a fallback title or description whole,
and looks up synonyms by the typed token before encoding each candidate.
The AI answer panel reads the plain text of a result for its context and
its source links, so they show no entities.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Autocomplete and full search are tested at the request, so a search path
that stops asking Elasticsearch for the HTML encoder fails a test. The AI
answer panel is rendered with a highlighted result, and the test reads the
message it streams and the text of its source link.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@akira28
akira28 force-pushed the feature/escape-search-highlights branch from 53eb2c6 to c934f61 Compare November 24, 2026 17:47
@akira28
akira28 changed the base branch from feature/searchable-api-pages to main November 24, 2026 17:47
@github-actions

Copy link
Copy Markdown
Contributor

@akira28
akira28 merged commit 70d35ff into main Nov 24, 2026
62 of 75 checks passed
@akira28
akira28 deleted the feature/escape-search-highlights branch November 24, 2026 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants