Skip to content

Commit

Permalink
global search query parse: consider unknown field clause to be part o…
Browse files Browse the repository at this point in the history
…f the raw search term
  • Loading branch information
tsullivan committed Sep 6, 2024
1 parent 0611d8a commit fbc5028
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ describe('parseSearchParams', () => {
const searchParams = parseSearchParams('tag:((()^invalid');
expect(searchParams).toEqual({
term: 'tag:((()^invalid',
filters: {
unknowns: {},
},
filters: {},
});
});

Expand All @@ -33,7 +31,6 @@ describe('parseSearchParams', () => {
expect(searchParams.filters).toEqual({
tags: undefined,
types: undefined,
unknowns: {},
});
});

Expand All @@ -44,20 +41,16 @@ describe('parseSearchParams', () => {
filters: {
tags: ['foo', 'dolly'],
types: ['bar'],
unknowns: {},
},
});
});

it('handles unknowns field clauses', () => {
it('considers unknown field clauses to be part of the raw search term', () => {
const searchParams = parseSearchParams('tag:foo unknown:bar hello');
expect(searchParams).toEqual({
term: 'hello',
term: 'unknown:bar hello',
filters: {
tags: ['foo'],
unknowns: {
unknown: ['bar'],
},
},
});
});
Expand All @@ -69,7 +62,6 @@ describe('parseSearchParams', () => {
filters: {
tags: ['foo', 'bar'],
types: ['dash', 'board'],
unknowns: {},
},
});
});
Expand All @@ -81,7 +73,6 @@ describe('parseSearchParams', () => {
filters: {
tags: ['42', 'true'],
types: ['69', 'false'],
unknowns: {},
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,24 @@ const aliasMap = {
};

export const parseSearchParams = (term: string): ParsedSearchParams => {
const recognizedFields = knownFilters.concat(...Object.values(aliasMap));
let query: Query;

try {
query = Query.parse(term, {
schema: {
recognizedFields: ['type', 'tag'],
},
schema: { recognizedFields },
});
} catch (e) {
// if the query fails to parse, we just perform the search against the raw search term.
return {
term,
filters: {
unknowns: {},
},
filters: {},
};
}

const searchTerm = getSearchTerm(query);
const filterValues = applyAliases(getFieldValueMap(query), aliasMap);

const unknownFilters = [...filterValues.entries()]
.filter(([key]) => !knownFilters.includes(key))
.reduce((unknowns, [key, value]) => {
return {
...unknowns,
[key]: value,
};
}, {} as Record<string, FilterValues>);

const tags = filterValues.get('tag');
const types = filterValues.get('type');

Expand All @@ -55,7 +43,6 @@ export const parseSearchParams = (term: string): ParsedSearchParams => {
filters: {
tags: tags ? valuesToString(tags) : undefined,
types: types ? valuesToString(types) : undefined,
unknowns: unknownFilters,
},
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,5 @@ export interface ParsedSearchParams {
* Aggregation of `type` and `types` field clauses
*/
types?: FilterValues<string>;
/**
* All unknown field clauses
*/
unknowns: Record<string, FilterValues>;
};
}

0 comments on commit fbc5028

Please sign in to comment.