K2 Search Not Working for 3 Letter Words? Here’s How to Fix!

One of the projects we are currently working on is converting a website from using Joomla’s content to K2’s content. In other words, all the articles, categories, images, are migrated to K2 and the website must display all its content from K2. Additionally, all the modules that are using Joomla’s content must be modified to use K2. While it might seem an easy project, it is not…

One of the smarter modules that didn’t need to be converted was the RokAjaxSearch module. This module (for those of you who don’t know) adds an Ajax powered search functionality to a Joomla website. What was really impressive about it is that all we needed to do to make it work with K2 was to disable Joomla’s specific search plugins (specifically Search – Content and Search – Categories) and enable K2’s specific search plugin (Search – K2).

However, we noticed a small quirk in the search behavior. The module was not returning any results from search queries of 3 characters or less. For example, search for the word the returned nothing, although the site had many thousand articles (all in English), which meant that it definitely had the word the somewhere.

After spending some time investigating the issue, we’ve zeroed in on the cause. It was the following line in the k2.php plugin file located right under the plugins/search/k2 directory:

$query .= "MATCH(i.title, i.introtext, i.`fulltext, i.extra_fields_search, i.image_caption, i.image_credits, i.video_caption, i.video_credits, i.metadesc, i.metakey) AGAINST ({$text} IN BOOLEAN MODE))";

Let us explain… The above line is part of the search query that runs every time the user searches for something. For those of you who are technical, the MATCH…AGAINST pattern is an efficient mechanism to perform a full text search, and it is much better (and more efficient) than using LIKE. On the flip side, however, MATCH…AGAINST cannot work on 3 letter words (unless MySQL is compiled in a non-standard way – which is something that only very advanced system administrators can do), but LIKE can. So, we had 2 options to fix the problem:

  1. Recompile MySQL
  2. Use LIKE instead of MATCH…AGAINST for 3 letter words

Obviously, we went with the second option, and so we changed the above line to the following:

if (strlen($searchText) == 3)
	$query .= " i.title LIKE '%$searchText%' OR i.introtext LIKE '%$searchText%' OR i.`fulltext` LIKE '%$searchText%')";
else
	$query .= "MATCH(i.title, i.introtext, i.`fulltext`, i.extra_fields_search, i.image_caption, i.image_credits, i.video_caption, i.video_credits, i.metadesc, i.metakey) AGAINST ({$text} IN BOOLEAN MODE)

This has worked beautifully!

Two things to note in our fixed code: 1) $searchText is the unaltered search query that was entered by the user (after filtering, of course), and 2) we did == 3 instead of <= 3 because we wanted search queries of less than 3 characters to be ignored.

If you’re having problems with K2 search and need help with it, or if you’re having another Joomla problem, then why not contact us? We are unbelievably fast, we are hard workers, we are experts in Joomla, and our prices are very, very affordable!

One Response to “K2 Search Not Working for 3 Letter Words? Here’s How to Fix!”
  1. Comment by Deb — February 26, 2014 @ 7:58 am

    I was wondering why searching k2 with 3 letters was showing only 1-2 results. After I applied the suggested changes, search now works OK.

    Thanks for this useful tutorial.

Leave a comment