Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weight Link UI search results slightly towards Posts #67563

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,12 @@ export default async function fetchLinkSuggestions(
* 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.
* 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives us a score between 0 and 1, where 1 is a perfect match.

I don't fully understand the scoring logic yet, but the score can be greater than 1, right? Is the actual maximum score 10?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall i can now be greater than 1. We could tweak the whole thing to keep scores below 1 if that helps?

*
* 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
Expand All @@ -268,6 +271,7 @@ export function sortResults( results: SearchResult[], search: string ) {

const scores = {};
for ( const result of results ) {
let score = 0;
if ( result.title ) {
const titleTokens = tokenize( result.title );
const exactMatchingTokens = titleTokens.filter( ( titleToken ) =>
Expand All @@ -285,17 +289,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 *= 1.1;
}

scores[ result.id ] = score;
}

return results.sort( ( a, b ) => scores[ b.id ] - scores[ a.id ] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Loading