Skip to content

Commit

Permalink
fix(search): prioritize start of word and exact match more
Browse files Browse the repository at this point in the history
  • Loading branch information
nikku committed Nov 21, 2024
1 parent c548079 commit fa5d242
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 31 deletions.
45 changes: 37 additions & 8 deletions lib/features/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,16 @@ function scoreToken(token) {
}

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

/**
Expand Down Expand Up @@ -217,7 +223,12 @@ function matchString(string, words) {
const tokens = [];
const matchedWords = {};

const regexpString = words.map(escapeRegexp).flatMap(str => [ '(?<wordStart>\\b' + str + ')', str ]).join('|');
const wordsEscaped = words.map(escapeRegexp);

const regexpString = [
`(?<all>${wordsEscaped.join('\\s+')})`,
...wordsEscaped
].join('|');

const regexp = new RegExp(regexpString, 'ig');

Expand All @@ -228,6 +239,17 @@ function matchString(string, words) {

const [ value ] = match;

const startIndex = match.index;
const endIndex = match.index + value.length;

const start = startIndex === 0;
const end = endIndex === string.length;

const all = !!match.groups.all;

const wordStart = start || /\s/.test(string.charAt(startIndex - 1));
const wordEnd = end || /\s/.test(string.charAt(endIndex + 1));

if (match.index > lastIndex) {

// add previous token (NO match)
Expand All @@ -242,11 +264,18 @@ function matchString(string, words) {
value,
index: match.index,
match: true,
wordStart: !!match.groups.wordStart,
start: match.index === 0
wordStart,
wordEnd,
start,
end,
all
});

matchedWords[value.toLowerCase()] = true;
const newMatchedWords = all ? words : [ value ];

for (const word of newMatchedWords) {
matchedWords[word.toLowerCase()] = true;
}

lastIndex = match.index + value.length;
}
Expand Down
55 changes: 32 additions & 23 deletions test/spec/features/search/searchSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,13 @@ describe('features/search', function() {
// given
const items = [
{
title: 'bar baz foo',
description: 'bar'
title: 'foobar'
},
{
title: 'foo',
description: 'bar'
title: 'bar baz ofoo foo -+ofoo woofoo foo'
},
{
title: 'baz foo',
description: 'bar'
title: 'baz foo'
}
];

Expand All @@ -212,8 +209,8 @@ describe('features/search', function() {

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

Expand All @@ -223,13 +220,19 @@ describe('features/search', function() {
// given
const items = [
{
title: 'baz foo bar'
title: 'foo bar'
},
{
title: 'foo baz and very long additional text\nalso foo bar'
},
{
title: 'baz and very long foo bar bar bar\nalso foo bar'
},
{
title: 'foo bar baz'
},
{
title: 'foo bar'
title: 'baz foo bar'
}
];

Expand All @@ -241,10 +244,12 @@ describe('features/search', function() {
});

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


Expand All @@ -253,10 +258,13 @@ describe('features/search', function() {
// given
const items = [
{
title: 'yes barfoo'
title: 'yes foowoo'
},
{
title: 'yes foowoo'
title: 'yesfoo woofoo'
},
{
title: 'yes barfoo'
}
];

Expand All @@ -268,9 +276,10 @@ describe('features/search', function() {
});

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


Expand Down Expand Up @@ -398,14 +407,14 @@ describe('features/search', function() {

// given
const items = [
{
title: 'Kafka amess',
description: 'Nope'
},
{
title: 'mess',
description: 'kafka'
},
{
title: 'Kafka amess',
description: 'Nope'
},
{
title: 'mess'
}
Expand Down Expand Up @@ -460,7 +469,7 @@ describe('features/search', function() {
// given
const items = [
{
title: 'bar foo bar'
title: 'bar foo bar'
}
];

Expand Down

0 comments on commit fa5d242

Please sign in to comment.