Handle single-item array filter in Lunr search engine

Signed-off-by: Tomasz Szuba <tszuba@box.com>
This commit is contained in:
Tomasz Szuba
2021-10-21 13:54:44 +02:00
parent 9eefb77ca2
commit 0de67c4d2a
2 changed files with 12 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-search-backend-node': patch
---
Handle special case when filter array has single value optimizing Lunr search behaviour.
@@ -80,11 +80,17 @@ export class LunrSearchEngine implements SearchEngine {
});
if (filters) {
Object.entries(filters).forEach(([field, value]) => {
Object.entries(filters).forEach(([field, fieldValue]) => {
if (!q.allFields.includes(field)) {
// Throw for unknown field, as this will be a non match
throw new Error(`unrecognised field ${field}`);
}
// Arrays are poorly supported, but we can make it better for single-item arrays,
// which should be a common case
const value =
Array.isArray(fieldValue) && fieldValue.length === 1
? fieldValue[0]
: fieldValue;
// Require that the given field has the given value
if (['string', 'number', 'boolean'].includes(typeof value)) {