-
Notifications
You must be signed in to change notification settings - Fork 141
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
Listen to global filter #282
Conversation
Hi @indykoning, It should be already possible to specify a const miniSearch = new MiniSearch({
fields: [/* list of fields... */],
searchOptions: {
filter: myFilterFunction
}
})
// This will use the "global" filter specified above
miniSearch.search("something") Is this what your PR meant to do? I prefer to keep search options separate from other options, but they can still be specified globally. |
Yes, the option to define the filter globally is indeed there. only the passed searchOptions are checked, so the global option isn't actually used. This PR currently fixes that by merging the passed options over top of the global options. If that isn't the preferred way I can also update the pr to work the same way as BoostDocument is handled. |
Oh you’re absolutely right, thanks for the bugfix! I will merge it as soon as I have time (I want to add a test to protect from regressions) |
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.
I would also add a test, something like:
// This can go right after it('allows custom filtering of results on the basis of stored fields')
it('allows to define a default filter upon instantiation', () => {
ms = new MiniSearch({
fields: ['title', 'text'],
storeFields: ['category']
searchOptions: {
filter: ({ category }) => category === 'poetry'
}
})
ms.addAll(documents)
const results = ms.search('del')
expect(results.length).toBe(1)
expect(results.every(({ category }) => category === 'poetry')).toBe(true)
})
I've added the test as well 🙂 |
Thanks a lot @indykoning ! This fix is part of release |
the filter function isn't actually being listened to unless it is directly passed to the search function.
This PR updates that, to listen to the global filter function unless it is overwritten in the searchOptions passed to the function.