Skip to content

Commit

Permalink
Merge pull request #513 from MHRA/timlee/fix-product-licences-search
Browse files Browse the repository at this point in the history
Fix fuzzy search for product licences
  • Loading branch information
TimboTambo authored Mar 16, 2020
2 parents bc5e3a3 + 2934901 commit ebb4346
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 12 additions & 0 deletions medicines/web/src/services/search-query-normalizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@ describe(buildFuzzyQuery, () => {
'K~1 K^4 L~1 L^4 POULTICE~1 POULTICE^4 KAOLIN~1 KAOLIN^4 POULTICE~1 POULTICE^4 BP~1 BP^4',
);
});

it('normalizes product licence', () => {
const fuzzyQuery = buildFuzzyQuery('pl 30464/0140');
expect(fuzzyQuery).toBe('PL304640140~1 PL304640140^4');
});

it('extracts and normalizes product licence', () => {
const fuzzyQuery = buildFuzzyQuery('amlodipine pl 30464/0140');
expect(fuzzyQuery).toBe(
'amlodipine~1 amlodipine^4 PL304640140~1 PL304640140^4',
);
});
});
7 changes: 4 additions & 3 deletions medicines/web/src/services/search-query-normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const escapeSpecialWords = (word: string): string =>
const preferExactMatchButSupportFuzzyMatch = (word: string): string =>
`${word}~${searchWordFuzziness} ${word}^${searchExactnessBoost}`;

const addNormalizedProductLicenses = (q: string): string => {
const extractNormalizedProductLicenses = (q: string): string => {
const normalizedProductLicences = q
.match(extractProductLicenseRegExp)
?.map(match => match.replace(extractProductLicenseRegExp, 'PL$3$5'));
Expand All @@ -21,7 +21,8 @@ const addNormalizedProductLicenses = (q: string): string => {
const normalizedProductLicencesString: string = normalizedProductLicences.join(
' ',
);
return `${q} ${normalizedProductLicencesString}`;
const qWithoutProductLicences = q.replace(extractProductLicenseRegExp, '');
return `${qWithoutProductLicences} ${normalizedProductLicencesString}`;
}

return `${q}`;
Expand All @@ -31,7 +32,7 @@ const splitByNonSearchableCharacters = (query: string) =>
query.split(/(?:[,+\-!(){}\[\]^~*?:\/]|\s+)/gi);

export const buildFuzzyQuery = (query: string): string => {
return splitByNonSearchableCharacters(addNormalizedProductLicenses(query))
return splitByNonSearchableCharacters(extractNormalizedProductLicenses(query))
.filter(x => x.length > 0)
.map(word => escapeSpecialWords(word))
.map(word => preferExactMatchButSupportFuzzyMatch(word))
Expand Down

0 comments on commit ebb4346

Please sign in to comment.