Skip to content

Core Data: Search link suggestions in one ranked, paged request - #83795

Closed
jeryj wants to merge 6 commits into
trunkfrom
add/link-suggestions-endpoint
Closed

jeryj wants to merge 6 commits into
trunkfrom
add/link-suggestions-endpoint

Conversation

@jeryj

@jeryj jeryj commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

What?

Adds GET /wp-block-editor/v1/link-suggestions, which searches posts, terms, post formats and media in one SQL query, ranked and paged. __experimentalFetchLinkSuggestions calls it instead of making four requests and sorting the results in the browser.

Related: #83407 (the browser ranking this replaces), #83408, #83652.

Why?

Four merged requests can't be paged: merging page 2 of each skips and repeats results, and past 20 matches of one type the other types never arrive. One ranked list makes each page a slice of it with a known total, so the link UI can load more.

How?

  • Matching: search terms are parsed as WP_Query parses them (quoted phrases, stopwords, -word). Each term must be in the title, or for posts and media in the content or excerpt. Media visibility follows its parent post, checked in SQL.
  • Ranking: title words found, then whole search in the title, then title begins with the search, then type (pages, other content, terms, post formats, media), then how much of the title word the search covers. The first three are match quality. Within equal quality, each page is shared evenly between post types and taxonomies.
  • Parameters: type, subtype, type_exclude, subtype_exclude, prefer_types (the shape Navigation: Search every entity type from the link UI #83408 uses for preferTypes), page, per_page. The JS options are the camelCase names.
  • Response: the /wp/v2/search fields (id, title, url, type, subtype, self link). fetchLinkSuggestions maps them to the fields it already returns.
  • Safety and extensibility: every value goes through $wpdb->prepare(). A search plugin can supply results through link_suggestions_pre_query. Results are cached.
  • MySQL before 8.0: no page sharing between types; otherwise the same.

Performance

Over HTTP, on 50,000 posts and pages, 200,000 media items and 5,000 terms (MariaDB 12.3, median of 5). "Before" is the editor's four requests, in parallel.

Search Before After Matches
coffee 356 ms 297 ms 13,695
coffee roast 359 ms 257 ms 2,493
mocha latte art 335 ms 247 ms 943
caf 357 ms 301 ms 13,821
the 357 ms 461 ms 41,956
zebra 332 ms 210 ms 0

Only a word found in most of the site is slower, since every match is scored before paging.

Testing Instructions

  1. Create pages, posts, categories, tags and media whose titles share a word, such as "coffee".
  2. Open a post, select some text and add a link.
  3. Type "coffee". Pages, posts, categories, tags and media are all listed, media last.
  4. Type "gear" with titles that begin with it and titles holding it further in. Those beginning with it come first.
  5. Type "coffee -decaf". Nothing mentioning "decaf" is listed.
npm run test:unit:php:base -- --filter Gutenberg_REST_Link_Suggestions_Controller_Test
npm run test:unit -- packages/core-data/src/fetch

Testing Instructions for Keyboard

No interface changes.

Use of AI Tools

Written with Claude Code (Claude Opus 5.5), directed and reviewed by the author.

🤖 Generated with Claude Code

Add a `/wp-block-editor/v1/link-suggestions` endpoint that searches posts,
terms, post formats and media in a single SQL query, ranks them, and pages
the ranked list. `__experimentalFetchLinkSuggestions` now makes one request
to it instead of four, and no longer sorts results in the browser.

Results that match equally well share each page between the post types and
taxonomies they come from. The endpoint also accepts type and subtype
filters, exclusions and preferred types, parses searches as `WP_Query`
does, caches results, and lets a search plugin supply them through the
`link_suggestions_pre_query` filter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

🏷️ Labels

This pull request needs exactly one label indicating its type, and has 0.

  • Required: any label starting with [Type].
  • Found: none.

Read more about Type labels in Gutenberg. If you cannot add labels yourself, a reviewer can do it for you.

📦 Bundle size

Size Change: -253 B (0%)

Total Size: 8.25 MB

📦 View Changed
Filename Size Change
build/scripts/core-data/index.min.js 37.5 kB -253 B (-0.67%)

68591bf Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric bfdebe9 trunk % Change
timeToFirstByte 58.15 ms +11.52% -5.85% 55.9 ms +9.03% -3.94% 4.03%
largestContentfulPaint 92 ms +8.7% -4.35% 92 ms +10.87% -4.35% 0%
lcpMinusTtfb 34.25 ms +5.26% -1.61% 35.4 ms +23.31% -6.21% -3.25%
wpBeforeTemplate 27.48 ms +24.64% -1.31% 27.33 ms +11.27% -2.49% 0.55%
wpTemplate 24.2 ms +6.94% -2.6% 24.23 ms +2.81% -4.95% -0.12%
wpTotal 54.25 ms +10.47% -6.12% 51.9 ms +8.86% -4.2% 4.53%
wpMemoryUsage 7.62 MB +0% -0% 7.58 MB +0% -0% 0.54%
wpDbQueries 17 +0% -0% 17 +0% -0% 0%

front-end-classic-theme

Metric bfdebe9 trunk % Change
timeToFirstByte 41.65 ms +18.97% -3% 44.55 ms +16.39% -0.67% -6.51%
largestContentfulPaint 92 ms +8.7% -4.35% 100 ms +8% -0% -8%
lcpMinusTtfb 50.35 ms +4.47% -5.06% 55.45 ms +0.63% -2.07% -9.2%
wpBeforeTemplate 24.12 ms +21.77% -1.04% 23.79 ms +13.96% -1.01% 1.39%
wpTemplate 13.72 ms +9.26% -1.97% 17.76 ms +8.45% -2.31% -22.75%
wpTotal 38.85 ms +13.26% -3.37% 41.32 ms +16.19% -0.63% -5.98%
wpMemoryUsage 6.11 MB +0% -0% 6.20 MB +0% -0% -1.53%
wpDbQueries 10 +0% -0% 14 +0% -0% -28.57%

media-processing

Metric bfdebe9 trunk % Change
mediaProcessingJpeg 411.01 ms +1.45% -0.48% 413.57 ms +0.58% -0.92% -0.62%
mediaProcessingAvif 6231.52 ms +0.21% -0.24% 6156.34 ms +0.24% -0.09% 1.22%
mediaProcessingJpegToAvif 4303 ms +0.19% -0.11% 4322.63 ms +0.21% -0.08% -0.45%

media-upload

Metric bfdebe9 trunk % Change
jpegUploadProcessing 1683.03 ms +16.93% -16.26% 1418.76 ms +1.44% -0.41% 18.63%
pngUploadProcessing 207.7 ms +9.71% -14.58% 215.68 ms +6.8% -5.37% -3.7%
largeJpegUploadProcessing 1421 ms +1.02% -0.56% 1406.79 ms +0.82% -0.63% 1.01%
multipleImageUploadProcessing 1612.85 ms +7.99% -1.94% 1561.75 ms +0.41% -0.31% 3.27%

post-editor

Metric bfdebe9 trunk % Change
serverResponse 391.22 ms +8.57% -6.48% 402.73 ms +6.64% -6.34% -2.86%
firstPaint 199.38 ms +6.47% -12.95% 230.72 ms +23.1% -18.51% -13.58%
domContentLoaded 1087.02 ms +1.78% -3.38% 1070.51 ms +1.82% -0.7% 1.54%
loaded 1088.24 ms +1.79% -3.38% 1071.74 ms +1.84% -0.67% 1.54%
firstContentfulPaint 456.15 ms +2.7% -4.83% 430.49 ms +6.08% -1.01% 5.96%
firstBlock 3180.51 ms +0.21% -2.17% 3158.27 ms +2.2% -0.37% 0.7%
type 18.43 ms +6.35% -3.47% 18.69 ms +2.51% -1.12% -1.39%
typeWithoutInspector 19.05 ms +7.77% -10.29% 18.18 ms +4.07% -1.27% 4.79%
typeWithTopToolbar 22.93 ms +3.97% -8.42% 23.01 ms +4.39% -2.09% -0.35%
typeContainer 1.25 ms +24% -38.4% 1.28 ms +21.09% -35.16% -2.34%
focus 12.71 ms +8.34% -3.46% 12.35 ms +15.3% -1.3% 2.91%
firstFocus 27.59 ms +0% -0% 27.18 ms +0% -0% 1.51%
selectAll 1491.53 ms +3.74% -0.75% 1467.76 ms +0.45% -0.27% 1.62%
listViewOpen 61.14 ms +4.35% -7.05% 59.57 ms +13.16% -6.7% 2.64%
inserterOpen 23.69 ms +13.51% -8.82% 21.64 ms +15.3% -2.26% 9.47%
inserterHover 2.2 ms +14.55% -8.64% 2.18 ms +12.84% -8.26% 0.92%
inserterSearch 8.02 ms +3.12% -2.12% 7.66 ms +9.66% -4.18% 4.7%
loadPatterns 644.35 ms +1.18% -2.24% 643.48 ms +4.28% -4.06% 0.14%
wpTotal 379.46 ms +8.93% -6.55% 391.33 ms +6.98% -6.44% -3.03%
wpMemoryUsage 13.17 MB +0% -0% 13.12 MB +0% -0% 0.34%
wpDbQueries 54 +0% -0% 54 +0% -0% 0%

site-editor

Metric bfdebe9 trunk % Change
serverResponse 493.55 ms +3.87% -3.03% 502.46 ms +4.35% -2.97% -1.77%
firstPaint 272.74 ms +14.7% -10.34% 257.2 ms +35.38% -10.7% 6.04%
domContentLoaded 1168.1 ms +2.9% -3.74% 1155.43 ms +0.99% -1.1% 1.1%
loaded 1169.39 ms +2.9% -3.75% 1156.75 ms +0.98% -1.07% 1.09%
firstContentfulPaint 463.99 ms +4.76% -4.09% 463.33 ms +0.93% -1.4% 0.14%
firstBlock 4307.78 ms +0.31% -0.88% 4217.06 ms +2.93% -0.55% 2.15%
type 19.51 ms +2.87% -11.64% 17.88 ms +2.57% -5.93% 9.12%
navigate 127.7 ms +6.55% -7.44% 121.96 ms +8.76% -11.99% 4.71%
loadPatterns 1448.03 ms +15.33% -2.78% 1414.26 ms +9.79% -8.8% 2.39%
loadPages 1063.18 ms +1.25% -0.49% 1053.91 ms +2.03% -0.78% 0.88%
wpTotal 484.24 ms +3.74% -3.2% 492.88 ms +4.47% -3.09% -1.75%
wpMemoryUsage 12.15 MB +0% -0% 12.10 MB +0% -0% 0.37%
wpDbQueries 43.5 +1.15% -1.15% 43 +2.33% -0% 1.16%

68591bf Run

@github-actions github-actions Bot added the [Package] Core data /packages/core-data label Sep 29, 2026
jeryj and others added 5 commits September 29, 2026 14:09
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Rank pages above other content that matches as well, since a link is
  more often to a page. Preferring posts lifts them back above pages.
- Match media by its title, caption and description, not its file name.
  Matching the file name joined post meta for every attachment, which was
  most of the cost of searching media, and a file name is rarely more
  than the title it was uploaded with.
- Count the results in the same query as the page, with a window
  function, instead of running the whole search a second time. A second
  count only runs past the last page, or without window functions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Normalize titles inside each branch of the union, which is stored as a
  table, so a title is normalized once instead of again for every score
  that reads it.
- Split titles into words with `REGEXP_REPLACE` where the database has it
  (MySQL 8.0, MariaDB 10.2), at anything but letters and numbers, as
  `tokenize()` in core-data did. Older databases keep splitting at the
  punctuation found in titles.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every value in the query now goes through a placeholder: search terms,
words, post types, taxonomies, statuses, preferred types, ranks, the user
ID and the page. Only table names, the class's own column names and
pieces made with `prepare()` are written into the SQL, and pieces are
joined without being prepared again.

Key the cache on the SQL with the placeholder `prepare()` puts in place
of `%` taken out, since it differs between requests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jeryj jeryj closed this Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Core data /packages/core-data

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant