Skip to content
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

Merged
merged 2 commits into from
Nov 21, 2024
Merged

Listen to global filter #282

merged 2 commits into from
Nov 21, 2024

Conversation

indykoning
Copy link
Contributor

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.

@lucaong
Copy link
Owner

lucaong commented Nov 8, 2024

Hi @indykoning,
thanks for your contribution!

It should be already possible to specify a filter "globally", like any other search option:

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.

@indykoning
Copy link
Contributor Author

Yes, the option to define the filter globally is indeed there.
however at
https://github.com/lucaong/minisearch/pull/282/files#diff-2ad78fe99a99053e4b46dc8136b24aecedd1a4ff6c4596409821cbb5f3b1e6deL1347

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.

@lucaong
Copy link
Owner

lucaong commented Nov 10, 2024

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)

Copy link
Owner

@lucaong lucaong left a 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)
})

src/MiniSearch.ts Show resolved Hide resolved
@indykoning
Copy link
Contributor Author

I've added the test as well 🙂

@lucaong lucaong merged commit b11a3b1 into lucaong:master Nov 21, 2024
1 of 2 checks passed
@lucaong
Copy link
Owner

lucaong commented Nov 21, 2024

Thanks a lot @indykoning ! This fix is part of release v7.1.1, which was just published on NPM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants