matcornic / hugo-theme-learn

Porting Grav Learn theme to Hugo
https://learn.netlify.com/en/
MIT License
1.6k stars 1.28k forks source link

invalid search results #340

Open coliff opened 4 years ago

coliff commented 4 years ago

If you go to: https://learn.netlify.com/en/ and search for 'beta' one of the search results is 'page 2' which doesn't have the word 'beta' on it. There are lots of other examples where the search results display pages which don't contain the search term. I don't know what causes this but I hope it can be fixed.

ZNicoZ commented 4 years ago

Somehow the theme displays all the chapters in a search, even if the chapter does not contain a hit. We built a workaround for that but moreover the search does not seem to be case-insensitive...

mgmalheiros commented 3 years ago

Had the same issue of matching too many pages, and found that it is caused by fuzzy matching, which is a feature of Lunr:

https://lunrjs.com/guides/searching.html#fuzzy-matches

This can be disabled by placing a copy of themes/hugo-theme-learn/static/js/search.js into the local site folder static/js/search.js and then altering the following line from the latter search.js from

return lunrIndex.search(queryTerm+"^100"+" "+queryTerm+"*^10"+" "+"*"+queryTerm+"^10"+" "+queryTerm+"~2^1").map(function(result) {

to:

return lunrIndex.search(queryTerm+"^100"+" "+queryTerm+"*^10"+" "+"*"+queryTerm+"^10").map(function(result) {

Hope this helps!

mgmalheiros commented 3 years ago

Just to share my other fixes related to search.

First, currently Lunr is not case insensitive, but there is a feature request for it as a configurable option (and some kind of work-around, which I have not tested): https://github.com/olivernn/lunr.js/issues/331

Second, currently the "live" search starts as soon as the first character is typed, however the completion of the search box needs at least 3 characters. I think 3 chars is a good compromise and far better then highlighting all the instances of the first letter typed. This can be fixed by copying the file themes/hugo-theme-learn/static/js/learn.js into a local copy at static/js/learn.js. Then you should alter the local copy, changing the following line

if (!value.length) {

to

if (value.length < 3) {

Another fix I did is to enable that collapsed sections (using the expand shortcode) to expand automatically when there is a match inside them. The change is in the same file as before, adding the following lines:

         sessionStorage.setItem('search-value', value);
         $(".highlightable").unhighlight({ element: 'mark' }).highlight(value, { element: 'mark' });
+        
+        // explicitly open all matching expandable text sections
+        $("mark").closest(".expand-content").css("display", "block");

         if (ajax && ajax.abort) ajax.abort();