lucaong / minisearch

Tiny and powerful JavaScript full-text search engine for browser and Node
https://lucaong.github.io/minisearch/
MIT License
4.9k stars 137 forks source link

Filter Auto-Suggestions? #18

Closed jaredloson closed 5 years ago

jaredloson commented 5 years ago

Hello,

I'm wondering about the possibility/best practice for filtering autosuggestions. Sorry in advance if this question is better suited for StackOverflow or a different forum.

In my app, I have a search input field and several filter selects. These filters are applied to the results returned from MiniSearch. I'm displaying search term autosuggestions as users type into the search input. So in some scenarios, autosuggestions are displayed for search results that will get filtered out.

I'm wondering about the best way to handle this . I could re-create the MiniSearch index each time a filter is changed, but wanted to see if there was a different approach.

Thanks for the plugin!

lucaong commented 5 years ago

Hi @jaredloson, This is an interesting problem. If I understand correctly, the filters that you apply to the search results are additional logic that filters the result list, correct?

Currently, there is no good way to add this filtering to the auto suggestions, so re-creating the index is probably the best way.

That said, I will think if this is something that can be added as a feature.

jaredloson commented 5 years ago

Yes, that's correct, the filters are extra logic applied after I get the search results. I've gone ahead with re-creating the index, and it's still behaving very snappy, so I'm content for my current use case. I'll leave the issue open in case you're still considering transitioning this into a feature request. Thanks again!

lucaong commented 5 years ago

Yes, it might still be a good feature for use cases where categories or other filters are involved. I'll definitely put thoughts in it.

Thanks for reporting this!

lucaong commented 5 years ago

I have a new feature (ready but not yet merged to master, see https://github.com/lucaong/minisearch/pull/19) that would help you in this use case. It allows storing specific fields, and filtering results of both search(...) and autoSuggest(...):

// First, mark your filter fields as "stored", so they are
// returned as part of the search results:
const miniSearch = new MiniSearch({
  fields: ['title', 'text', 'author'],
  storeFields: ['category']
})

miniSearch.addAll([...])

// Then, filter results on the basis of the stored field:
miniSearch.autoSuggest('democr', {
  filter: (item) => item.category === 'philosophy'
})

Technically, the filter option can be any function returning a boolean, so you don't strictly need to use stored fields, and can implement any custom logic. Using it with stored fields just makes it more convenient.

@jaredloson what do you think about it? Would it solve your case? I might be able to merge this feature and create a new release soon.

lucaong commented 5 years ago

I merged https://github.com/lucaong/minisearch/pull/19, adding this feature, and released version v2.1.0, so I will close the issue 🙂

jaredloson commented 5 years ago

Thanks @lucaong, updated to the latest and I'm able to filter autosuggestions with the new filter function. Cheers!