jeancroy / FuzzySearch

:mag: Fast autocomplete suggestion engine using approximate string matching
MIT License
194 stars 32 forks source link

Scoring items higher / grouping results #24

Open ghost opened 4 years ago

ghost commented 4 years ago

Hi Jean, thank you for Fuzzysearch, it's excellent. My feed contains categories and products, I'm wondering if...

1) I can give a higher score/priority to specific items, so they appear before anything else. 2) I can group the results, so that matched categories always appear before matched products.

My feed looks like this...

[
{ "type": "P", "id": "123", "title": "Product Name", "descr": "Description", "sku": "ABC123", "price": "19.95", "image": "./images/image.jpg", "category": "Category Name" },
...
{ "type": "C", "id": "456", "title": "Category Name", "descr": "Description" },
...
]

Note, I'm using Twitter typeahead. Grateful for any advice you can give. Phil

jeancroy commented 4 years ago

Hi so to change the order will be relatively easy. One option is to change options.sorter.

Default sorter is here and proceed to sort by score then name (or a custom sortkey) https://github.com/jeancroy/FuzzySearch/blob/6a7d5530d2000087f26071ba4adf7a025ec7ef13/src/output.js#L189

Inside the sorter you can access original item as a.item and b.item

So one option then is to sort by priority first then sort by score (when priority is the same) To do this, you can get inspiration from the logic already here that sort by score first then by name of the item.

You may also be able to create an importance criteria and use something like this. d = b.score * b.item.importance - a.score * a.item.importance.


For the grouping I'm not certain where to go. I imagine you want to score if the group to be best score among it's items. One hint I can give is that this will allow to output full information:

https://github.com/jeancroy/FuzzySearch#get-score-detail-searchresult-object

jeancroy commented 4 years ago

If I had to implement the feature I'd probably do something like this

GroupingFunction: