signaldb
signaldb copied to clipboard
Search text field through a collection
I need to search through a collection by filtering on a certain field. This is the collection I'm working with:
const collection = new Collection<{ id: string; name: string; }>()
First, I tried using the $text.$search operator. This is supported by the types, but fails with an unknown query operator $text error.
It's worth mentioning that mingo does not implement the $text operator.
const filtered = collection.find({ name: { $text: { $search: 'search text' } } }).fetch()
Then I tried using the $where operator as a function. This is also supported by the types, but doesn't work because the item parameter is undefined.
const filtered = collection.find({ name: { $where: (item) => item.includes("search text") } }).fetch()
Do you have any suggestions on filtering text using operators?
Have you tried $regex? Here's an example: https://stackblitz.com/edit/signaldb-playground-regex-search?file=main.js
It fails for number fields though. Unfortunately, $regexMatch, which would support an inline $toString, is not supported in mingo.
Have you tried using an external search index? I did find fusejs.io easy to use, but it doubles the memory consumption. Am implementing a signaldb hooks based version for https://github.com/nextapps-de/flexsearch right now. If you want to I could share the current progress?
Im interested too in $text operator with flexsearch index. Can i help somehow?
@rougsig I would rather create a separate search index. How could an implementation with the $text operator work? I started a discussion here: https://github.com/maxnowack/signaldb/discussions/1678
@maxfriedmann I've done it externally my letting uFuzzy do the filtering external to the collection but I was just wondering how to do a simple filter using the built-in operators.
@rougsig maybe you could write a guide to add to the documentation on how to create a custom $text operator that uses either simple String.contains filter or fuzzy matching.
I've removed the $text operator from the selector type (see #1914)
The $where operator should work, but there was an issue with the type (also addressed in #1914). The item isn't passed as a parameter, but is accessible via this. Not really convenient, but consistent with mongodb.
I'm closing this as the discussion is ongoing in #1678