signaldb icon indicating copy to clipboard operation
signaldb copied to clipboard

Search text field through a collection

Open obedm503 opened this issue 7 months ago • 2 comments

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?

obedm503 avatar Apr 26 '25 03:04 obedm503

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?

maxfriedmann avatar Apr 26 '25 06:04 maxfriedmann

Im interested too in $text operator with flexsearch index. Can i help somehow?

rougsig avatar Apr 26 '25 21:04 rougsig

@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 avatar Apr 28 '25 11:04 maxfriedmann

@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.

obedm503 avatar Apr 28 '25 14:04 obedm503

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

maxnowack avatar Jun 23 '25 20:06 maxnowack