Skip to content

Commit

Permalink
chore(search): value based off token length
Browse files Browse the repository at this point in the history
  This gives longer matches precedence.

chore(search): penalize unmatched tokens

  This ensures we score "most relevant" results higher.
  • Loading branch information
nikku committed Nov 21, 2024
1 parent fa5d242 commit 208d16b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
31 changes: 18 additions & 13 deletions lib/features/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,28 +163,33 @@ function scoreTokens(tokens) {
}

/**
* Score a token.
* Score a token based on its characteristics
* and the length of the matched content.
*
* @param { Token } token
*
* @returns { number }
*/
function scoreToken(token) {
const modifier = Math.log(token.value.length);

if (!token.match) {
return 0;
return -0.07 * modifier;
}

return token.start
? (
token.end
? 131.9
: 7.87
)
: (
token.wordStart
? 2.19
: 1
);
return (
token.start
? (
token.end
? 131.9
: 7.87
)
: (
token.wordStart
? 2.19
: 1
)
) * modifier;
}

/**
Expand Down
30 changes: 28 additions & 2 deletions test/spec/features/search/searchSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ describe('features/search', function() {
title: 'foo bar'
},
{
title: 'foo baz and very long additional text\nalso foo bar'
title: 'foo bar baz'
},
{
title: 'baz and very long foo bar bar bar\nalso foo bar'
},
{
title: 'foo bar baz'
title: 'foo baz and very long additional text\nalso foo bar'
},
{
title: 'baz foo bar'
Expand All @@ -253,6 +253,32 @@ describe('features/search', function() {
}));


it('should prioritize longest match', inject(function(search) {

// given
const items = [
{
title: 'yes foowoo'
},
{
title: 'yeskay foowoo'
}
];

// when
const results = search(items, 'yes foo', {
keys: [
'title'
]
});

// then
expect(results).to.have.length(2);
expect(results[0].item).to.eql(items[0]);
expect(results[1].item).to.eql(items[1]);
}));


it('should prioritize start of term', inject(function(search) {

// given
Expand Down

0 comments on commit 208d16b

Please sign in to comment.