Skip to content

Commit

Permalink
Prefer exact matches in Link Search results sorting (#67367)
Browse files Browse the repository at this point in the history
* Weight towards exact matches

* Add additional test coverage

Co-authored-by: getdave <[email protected]>
Co-authored-by: draganescu <[email protected]>
Co-authored-by: talldan <[email protected]>
Co-authored-by: jasmussen <[email protected]>
Co-authored-by: kevin940726 <[email protected]>
Co-authored-by: ironprogrammer <[email protected]>
Co-authored-by: annezazu <[email protected]>
  • Loading branch information
8 people authored and michalczaplinski committed Dec 5, 2024
1 parent d0da94d commit f6d161d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,29 @@ export function sortResults( results: SearchResult[], search: string ) {
for ( const result of results ) {
if ( result.title ) {
const titleTokens = tokenize( result.title );
const matchingTokens = titleTokens.filter( ( titleToken ) =>
searchTokens.some( ( searchToken ) =>
titleToken.includes( searchToken )
const exactMatchingTokens = titleTokens.filter( ( titleToken ) =>
searchTokens.some(
( searchToken ) => titleToken === searchToken
)
);
scores[ result.id ] = matchingTokens.length / titleTokens.length;
const subMatchingTokens = titleTokens.filter( ( titleToken ) =>
searchTokens.some(
( searchToken ) =>
titleToken !== searchToken &&
titleToken.includes( searchToken )
)
);

// 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,43 @@ describe( 'sortResults', () => {
6,
] );
} );

it( 'orders results to prefer direct matches over sub matches', () => {
const results = [
{
id: 1,
title: 'News',
url: 'http://wordpress.local/news/',
type: 'page',
kind: 'post-type',
},
{
id: 2,
title: 'Newspaper',
url: 'http://wordpress.local/newspaper/',
type: 'page',
kind: 'post-type',
},
{
id: 3,
title: 'News Flash News',
url: 'http://wordpress.local/news-flash-news/',
type: 'page',
kind: 'post-type',
},
{
id: 4,
title: 'News',
url: 'http://wordpress.local/news-2/',
type: 'page',
kind: 'post-type',
},
];
const order = sortResults( results, 'News' ).map(
( result ) => result.id
);
expect( order ).toEqual( [ 1, 4, 3, 2 ] );
} );
} );

describe( 'tokenize', () => {
Expand Down

0 comments on commit f6d161d

Please sign in to comment.