flexsearch icon indicating copy to clipboard operation
flexsearch copied to clipboard

Expose a function to control the sort order

Open peterbe opened this issue 2 years ago • 3 comments

What I would like is to control the sort order. I'm guessing, internally, that Flexsearch has an algorithm for determining which matched document came before another. If I can just get in there an affect the sort for very specific business logic. Something like this maybe:

index.search(query, {
  sort: (a, b) => {
    if (someReason(a)) { return 1 }
    if (someOtherReason(b)) { return -1 }
    return 0
  }
})

Ideally, the sort callback would get the document and each documents match score as determined what internals of Flexsearch (e.g. BM25). For example, I could manually boost the score on docs that should be prioritized.

peterbe avatar Oct 25 '21 13:10 peterbe

Another solution would be to expose a function that allows me to control the score of each document. I imagine, you have 1,000 documents and of them 100 have greater-than-zero matching so now I could manipulate the score of the 100 matched. E.g.

index.search(query, {
  adjustScore: (document, score) => {
    if (document.key === 'special')  return score * 2
    if (document.key === 'blaae') return score / 2
    return score
  }
})

peterbe avatar Oct 25 '21 13:10 peterbe

@ts-thomas Perhaps you can help with a temporary solution. Is there any way I can find out the internal score of my search results. Because if I can have the internal scores, I can retroactively recompute them and re-sort.

peterbe avatar Oct 27 '21 17:10 peterbe

@peterbe did you ever find out how to do this?

corey34 avatar Apr 13 '22 16:04 corey34

There is no linear search, the index contains of multiple blocks which will concatenated during search. The best option is to apply custom order on the result. Adding the score value visible to the result is already on my feature request list.

ts-thomas avatar Oct 02 '22 15:10 ts-thomas

Nope. No progress. And I have up.

On Wed, Apr 13, 2022 at 6:03 PM corey34 @.***> wrote:

@peterbe https://github.com/peterbe did you ever find out how to do this?

— Reply to this email directly, view it on GitHub https://github.com/nextapps-de/flexsearch/issues/283#issuecomment-1098227972, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAGQ45LX6NYVWMLJTLSKMDVE3V4HANCNFSM5GVLGMHA . You are receiving this because you were mentioned.Message ID: @.***>

-- GitHub https://github.com/peterbe · Website https://www.peterbe.com/ · Twitter https://twitter.com/peterbe · Side project https://thatsgroce.web.app/

peterbe avatar Oct 11 '22 07:10 peterbe