Is there a way to for search to find exact matches as first priority as well as words out of order?
Is there a way to for search to find exact matches as first priority as well as words out of order?
I read about smart searching here:
https://datatables.net/reference/api/search()
What I understand is, with smart search enabled (which it is by default), matches are brought up in the order they are found, and not based on relevancy. So if a search for two words had an exact match somewhere further down in the data, that would show up later than partial matches which come up earlier in the data.
And by turning smart search off, I am able to search for exact matches, which are the most relevant, yet I can no longer match partially for words out of order.
My question is, is it possible to have both functionalities? A search that takes into account relevancy, bringing up exact matches first, followed by partial out of order matches second? A sort of priority based filter?
Answers
I assume you are referring to the order the rows are displayed in the table. Searching doesn't affect the row order. Its an interesting idea but there is nothing built into Datatables to order the rows based on the relevancy of the search results. You can customize how the table is order by creating an ordering plugin. I'm not sure how you would do this though. If I think of any ideas I'll post back.
Kevin
Yes, while searching does not affect row order, row order does affect search however. Closer, more exact matches should show up sooner than distant, inexact matches. In a table of thousands of records, relevancy becomes important because it affects how soon you can get to what it is you are looking for.
I think that it would work by taking both search results of smart search turned on as well as off, instead of only one or the other, then showing the 'smart search turned off' exact matches first, then eliminating those repeats from the 'smart search turned on' partial matches, then producing those partial matches afterwards.
Funny, I didn't know it ever did partial results. Is this for multi-word only?
Search is simply a filter, and can't change row-order directly. I suggest maybe making search exact by default, and then have a button (or check box) to Show Partial Matches. When it is clicked it can change the search mode and refresh the table.
You could also maybe make it so if the table ever has zero filtered results, then it automatically switches to Show Partial (switching back without a reload then becomes the issue... maybe switch back only when the search is blank again)
To do exactly what you want, you could have 2 hidden tables, each with one search mode. Then link all the searches together (see code below). Then when the search is changed, loop through the rows of the 2 hidden tables and add them all to a visible table.
Code to link all Search Boxes:
Hi,
DataTables build in search isn't actually technically a search but rather a filter. It simply takes the test to see if the row matches it, if it does then it can be displayed. If it doesn't then it gets dropped.
There is no relevancy calculation it is simply pass or fail. The ordering of the table is determined by the column sorting and the data that remains.
For a "proper" search - have a look at this blog post which introduces a fuzzy search algorithm we have implemented for DataTables. You'll see one of the examples has a relevancy score in a column. If you so wanted, you could force sorting by that column only.
Allan