Search code examples
searchviewxpagesfuzzy

XPages sorting of view after fuzzy search


I have a view in my XPage which has "search" property bound to a viewScope variable. Now per our customer's request I have enabled the "fuzzy search" option "true" since they wanted the view to show similar search results (they couldn't find what they were looking for if the search term didn't match exactly to be specific).

This created another problem which is if the view has the perfect match or even contains the search variable, it is not listed at the top but rather listed far away below the view.

To give an example, the view has product codes varying from "EGSK" to "EGSY" (the list contains EGSS, EGSC, EGST etc.). Now if they search for "EGSC" it is included in the view but not at the top. Since the other product codes differ by 1 character mostly, they are all included in the results.

How can change this so that if the viewscope variable is matched perfectly, it is listed at the top when its searched when fuzzy search is enabled?


Solution

  • The search functionality doesn’t have a facility to sort by preciseness of match. So this is what I would do: - decouple your UI from the view. E.g. use a managed bean and a repeat to build the UI and a method in the bean that gives you a collection of a custom object for the rows - build the method into the bean that queries the view in whatever way you need. Loop through the view entries and build a collection of custom objects - sort those before you return them.

    The trick here is to use Collections.sort which allows you to specify your own sorting mechanism. You could compute the percentage of search term match and sort first for that, then by string - all in one function.

    Caveat: if your search returns a lot of values you might run out of time and/or memory.

    Hope that helps