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?

hm123hm123 Posts: 84Questions: 27Answers: 1

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

  • kthorngrenkthorngren Posts: 21,183Questions: 26Answers: 4,925

    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.

    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

  • hm123hm123 Posts: 84Questions: 27Answers: 1

    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.

  • rpmccormickrpmccormick Posts: 136Questions: 18Answers: 0

    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:


    setTimeout(function() {//Link all 3 search boxes together (super cool!) $( 'input[type=search]').on('keypress', function(e) //note, doesn't work for backspace {$('input[type=search]').val(this.value+e.originalEvent.key); //I like it though $('input[type=search]').trigger('keyup'); //Makes DT update rows from new search value e.preventDefault(); //we already set it, so this is needed to prevent double-typing } ); //Also fix Backspace $( 'input[type=search]').on('keydown', function(e) {if (e.originalEvent.key=='Backspace') {$('input[type=search]').val(this.value.slice(0, -1)); $('input[type=search]').trigger('keyup'); //Makes DT update rows from new search value e.preventDefault(); } } ); //Also fix Enter $( 'input[type=search]').on('keydown', function(e) {if (e.originalEvent.key=='Enter') {$('input[type=search]').val(this.value); $('input[type=search]').trigger('keyup'); //Makes DT update rows from new search value e.preventDefault(); } } ); //And mouse $( 'input[type=search]').on('change', function(e) {$('input[type=search]').val(this.value); $('input[type=search]').trigger('keyup'); //Makes DT update rows from new search value e.preventDefault(); } ); }, 300 );
  • allanallan Posts: 63,234Questions: 1Answers: 10,417 Site admin

    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

Sign In or Register to comment.