DEV Community

Pacharapol Withayasakpunt
Pacharapol Withayasakpunt

Posted on

Letting users search the database with a simple one-liner string (and let user decide which field to search)

Which is obviously not JSON, YAML or SQL.

I recently did this for https://learn-unicode.now.sh, which does come from this topic.

The solution is inspired from lunr.js which for some reasons, support searching by field; but is rare in other search engines. Also, for some reasons, lunr.js, by default, join the fields with OR, instead of AND, which is unintuitive.

GitHub logo patarapolw / qsearch

Search a database with a string. Designed for end-users.

Generating JSON for usage in search in NoSQL's is relatively easy, tested for MongoDB, LokiJS, NeDB and Array.prototype.filter. I also noticed some differences.

LokiJS doesn't allow

{
  'data.a': { $regex: /b/i }
}
Enter fullscreen mode Exit fullscreen mode

Or its equivalent data.a:b.

Joining with AND or OR, and allow quotes is not as complex as it seems, due to shlex and /^[-+?]/ prefixes. However, I don't allow brackets this time, because it would get complex. I have tried to do it before, though.

If you are kind, I am looking for library testers. See if you can break it.

https://q2search.herokuapp.com/

Top comments (0)