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

Scoring items higher / grouping results #24

Open
ghost opened this issue Oct 21, 2019 · 2 comments
Open

Scoring items higher / grouping results #24

ghost opened this issue Oct 21, 2019 · 2 comments

Comments

@ghost
Copy link

ghost commented Oct 21, 2019

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
Copy link
Owner

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)

function compareResults(a, b) {

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
Copy link
Owner

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

GroupingFunction:

  • Setup dictionary of group key => group object.

  • Setup list of ungrouped items.

  • For each result, get group key.

  • If no grouping key set, apply output transform and add to ungrouped list (in a way that preserve item ordering).

  • If group key is defined for the item:

    • Check if Groups[key] exist, if not Groups[key] = {key:key, score:item.score, items:[], isGroup=true }
    • Transform current result into it's output item, add to group items (in a way that preserve item ordering).
    • Get current result score, test if better than group score, if so update group score. (This migth not be needed as result are already from best to worst)
  • Copy the dictionary into an array, (sort the array? or already sorted?)

  • Append the ungrouped items at the end of the group array ?

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

No branches or pull requests

1 participant