-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Global Search] Add multiword type handling in global search #196087
Changes from 1 commit
3cf06fa
4bba4ee
6a9c5ba
6ae8f62
9f7cafd
a931256
cf4fbbf
a2f468d
450b58e
11c502d
c61e511
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,12 +16,40 @@ const aliasMap = { | |
type: ['types'], | ||
}; | ||
|
||
export const parseSearchParams = (term: string): ParsedSearchParams => { | ||
// Converts multi word types to phrases by wrapping them in quotes. Example: type:canvas workpad -> type:"canvas workpad" | ||
const convertMultiwordTypesToPhrases = (term: string, multiWordTypes: string[]): string => { | ||
if (multiWordTypes.length === 0) { | ||
return term; | ||
} | ||
|
||
const typesPattern = multiWordTypes.join('|'); | ||
const canvasWorkpadRegex = new RegExp(`(type:|types:)(\\s*[^"']*?)\\b(${typesPattern})\\b`, 'gi'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can we use a generic name, e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, thanks for noticing this. This variable name is something I used initially, before I made this solution more generic. I'll change that. |
||
|
||
const modifiedTerm = term.replace( | ||
canvasWorkpadRegex, | ||
(match, typePrefix, additionalTextInBetween, matchedPhrase) => | ||
`${typePrefix}${additionalTextInBetween}"${matchedPhrase}"` | ||
); | ||
|
||
return modifiedTerm; | ||
}; | ||
|
||
export const parseSearchParams = (term: string, searchableTypes: string[]): ParsedSearchParams => { | ||
const recognizedFields = knownFilters.concat(...Object.values(aliasMap)); | ||
let query: Query; | ||
|
||
// Finds all multiword types that are separated by whitespace or hyphens | ||
const multiWordSearchableTypesWhitespaceSeperated = searchableTypes | ||
.filter((item) => /[ -]/.test(item)) | ||
.map((item) => item.replace(/-/g, ' ')); | ||
|
||
const modifiedTerm = convertMultiwordTypesToPhrases( | ||
term, | ||
multiWordSearchableTypesWhitespaceSeperated | ||
); | ||
|
||
try { | ||
query = Query.parse(term, { | ||
query = Query.parse(modifiedTerm, { | ||
schema: { recognizedFields }, | ||
}); | ||
} catch (e) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add
type:"canvas workpad"
to the list so we make sure it handles types with existing quotes?Also by doing so, I realized that we pass twice the same types in the array, could we avoid dedupes when returning the array? (I know it's not part of fixing the issue but we could use the momentum 😊). Thanks 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I didn't think about checking for already existing quotes. It should work but I'll cover it in the test.
As for deduping, I wasn't really sure if this is what we want (although it's makes logical sense) but sure, I'll add that.