Reputation: 146
I'm building an app with expressjs and mongoose, I'm trying to construct a search endpoint like the specified herein Spotify api https://developer.spotify.com/documentation/web-api/reference/search/search/
I have a problem figuring out how to convert the given query parameters into a regex to match the required results.
my endpoint looks like this example.com/search?q=searchText
Requirements:
q=roadhouse blues
is required to match all the results contains roadhouse and blues in its name. ex: matches both “Blues Roadhouse” and “Roadhouse of the Blues”.q="roadhouse blues"
if quotations are provided it has to match the exact word. ex: matches “My Roadhouse Blues” but not “Roadhouse of the Blues”q=roadhouse NOT blues
matches all the results that have roadhouse in its name but not blues. q=roadhouse OR blues
matches all the results that have either roadhouse or blues in their name.q=bob year:2014
matches the results that contain name bob and in the year 2014q=bob year:2014-2020
matches the results that contain name bob and in the year between 2014 and 2020Upvotes: 1
Views: 229
Reputation: 5615
Here are the regexes for each of your queries:-
NOTE: All regexes use global, multiline and case insensitive modifiers
q=roadhouse blues
=> ^.*?(?:roadhouse.*?blues|blues.*?roadhouse).*$
-> demoq="roadhouse blues"
=> ^.*?(?:roadhouse blues).*$
-> demoq=roadhouse NOT blues
=> ^(?!.*?blues).*?roadhouse.*$
-> demoq=roadhouse OR blues
=> ^.*?(?:roadhouse|blues).*$
-> demoq=bob year:2014
=> ^.*?bob.*?in the year 2014.*$
-> demoq=bob year:2014-2020
=> ^.*?bob.*?in the year (?:201[4-9]|2020).*$
-> demoMost of these follow a very specific pattern and you should be able to convert queries into these regexes. The last 2 might require some finesse but you've to explain the specific use case of those 2 by examples.
Upvotes: 1