From 8b3412c2999ccfe2c0f0001dab2f3a587f025cc5 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 4 Dec 2024 10:34:00 +0000 Subject: [PATCH 1/3] Apply new weighting towards post-type results --- .../__experimental-fetch-link-suggestions.ts | 28 +++++--------- .../__experimental-fetch-link-suggestions.js | 37 +++++++++++++++++++ 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.ts b/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.ts index 29012197589035..fb7a7d0730bcb2 100644 --- a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.ts +++ b/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.ts @@ -249,25 +249,12 @@ export default async function fetchLinkSuggestions( return results; } -/** - * Sort search results by relevance to the given query. - * - * Sorting is necessary as we're querying multiple endpoints and merging the results. For example - * a taxonomy title might be more relevant than a post title, but by default taxonomy results will - * be ordered after all the (potentially irrelevant) post results. - * - * We sort by scoring each result, where the score is the number of tokens in the title that are - * also in the search query, divided by the total number of tokens in the title. This gives us a - * score between 0 and 1, where 1 is a perfect match. - * - * @param results - * @param search - */ export function sortResults( results: SearchResult[], search: string ) { const searchTokens = tokenize( search ); const scores = {}; for ( const result of results ) { + let score = 0; if ( result.title ) { const titleTokens = tokenize( result.title ); const exactMatchingTokens = titleTokens.filter( ( titleToken ) => @@ -285,17 +272,20 @@ export function sortResults( results: SearchResult[], search: string ) { // The score is a combination of exact matches and sub-matches. // More weight is given to exact matches, as they are more relevant (e.g. "cat" vs "caterpillar"). - // Diving by the total number of tokens in the title normalizes the score and skews - // the results towards shorter titles. const exactMatchScore = ( exactMatchingTokens.length / titleTokens.length ) * 10; const subMatchScore = subMatchingTokens.length / titleTokens.length; - scores[ result.id ] = exactMatchScore + subMatchScore; - } else { - scores[ result.id ] = 0; + score = exactMatchScore + subMatchScore; + } + + // Add a slight bonus for 'post-type' results + if ( result.kind === 'post-type' ) { + score += 5; } + + scores[ result.id ] = score; } return results.sort( ( a, b ) => scores[ b.id ] - scores[ a.id ] ); diff --git a/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js b/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js index ad0014ff86ecb8..1409192ed80271 100644 --- a/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js +++ b/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js @@ -430,6 +430,43 @@ describe( 'sortResults', () => { ); expect( order ).toEqual( [ 1, 4, 3, 2 ] ); } ); + + it( 'weight results towards Posts even if less relevant', () => { + const results = [ + { + id: 1, + title: 'Newspapers', + url: 'http://wordpress.local/more-relevant-news/', + type: 'page', + kind: 'taxonomy', + }, + { + id: 2, + title: 'News', + url: 'http://wordpress.local/most-relevant-news/', + type: 'page', + kind: 'media', + }, + { + id: 3, + title: 'Less Relevant News because it has a very long title', + url: 'http://wordpress.local/less-relevant-news/', + type: 'page', + kind: 'post-type', + }, + { + id: 4, + title: 'News', + url: 'http://wordpress.local/same-relevant-news/', + type: 'page', + kind: 'post-type', + }, + ]; + const order = sortResults( results, 'News' ).map( + ( result ) => result.id + ); + expect( order ).toEqual( [ 4, 2, 3, 1 ] ); + } ); } ); describe( 'tokenize', () => { From 962e489054c3d24d9ba774e972bd747c232eef9d Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Tue, 10 Dec 2024 11:57:18 +0000 Subject: [PATCH 2/3] Restore and update docblock --- .../__experimental-fetch-link-suggestions.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.ts b/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.ts index fb7a7d0730bcb2..768465e08bdef4 100644 --- a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.ts +++ b/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.ts @@ -249,6 +249,23 @@ export default async function fetchLinkSuggestions( return results; } +/** + * Sort search results by relevance to the given query. + * + * Sorting is necessary as we're querying multiple endpoints and merging the results. For example + * a taxonomy title might be more relevant than a post title, but by default taxonomy results will + * be ordered after all the (potentially irrelevant) post results. + * + * We sort by scoring each result, where the score is a combination of the number of exact matches + * and sub-matches for tokens in the title that are also in the search query, divided by the total + * number of tokens in the title. This gives us a score between 0 and 1, where 1 is a perfect match. + * + * We then apply various "weights" to the score to influence the sorting order with exact matches + * being more important than sub-matches. We also give a slight bonus to post-type results. + * + * @param results + * @param search + */ export function sortResults( results: SearchResult[], search: string ) { const searchTokens = tokenize( search ); From 42b19ebe0edb6f3dd510c9236beb3124fbe47c52 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 11 Dec 2024 09:55:13 +0000 Subject: [PATCH 3/3] Use factor rather than blunt addition --- .../src/fetch/__experimental-fetch-link-suggestions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.ts b/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.ts index 768465e08bdef4..40eccb5e04051e 100644 --- a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.ts +++ b/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.ts @@ -299,7 +299,7 @@ export function sortResults( results: SearchResult[], search: string ) { // Add a slight bonus for 'post-type' results if ( result.kind === 'post-type' ) { - score += 5; + score *= 1.1; } scores[ result.id ] = score;