Brian's Blog Homepage
Joomla search result bugs

It has bugged me for a long time that the search on this site is broken. I've never had the time to work out why but today I did so let me share it with you so that you can fix it on your site as well.

We all know that for google and other search engines the most important search term will be in your heading. So if you have two articles that match the search term the article with the search term in the title will appear first.

Now try to use the search module on this site for the search term replace.

If you scroll down and down and down you will find an article called "Search and Replace" at about position 18. That is the only article with the search term in the title so why is it so far down the search results?

I had assumed that this was a bug in the core Joomla code. That the search plugin was perhaps not looking in the title field or perhaps it was looking at the title field after the content field. But that turns out not to be the case.

foreach ($words as $word)
$word = $db->quote('%' . $db->escape($word, true) . '%', false);
$wheres2 = array();
$wheres2[] = 'LOWER(a.title) LIKE LOWER(' . $word . ')';
$wheres2[] = 'LOWER(a.introtext) LIKE LOWER(' . $word . ')';
$wheres2[] = 'LOWER(a.fulltext) LIKE LOWER(' . $word . ')';
$wheres2[] = 'LOWER(a.metakey) LIKE LOWER(' . $word . ')';
$wheres2[] = 'LOWER(a.metadesc) LIKE LOWER(' . $word . ')';
$wheres2[] = 'LOWER(fv.value) LIKE LOWER(' . $word . ')';
$wheres[] = implode(' OR ', $wheres2);

As you can see in the code above it is searching in the title and it is searching there for the search term before anywhere else.

So what was the problem?

Looking a ittle further down the content search plugin I saw this code.

switch ($ordering)
case 'oldest':
$order = 'a.created ASC';

case 'popular':
$order = 'a.hits DESC';

case 'alpha':
$order = 'a.title ASC';

case 'category':
$order = 'c.title ASC, a.title ASC';

case 'newest':
$order = 'a.created DESC';

So here you can see that by default the search results are ordered by the creation date of the article so that any article created yesterday will always display in the search results before an older article EVEN if the search term is in the title and therefore more important. That is exactly the problem I am facing. The "Search and Replace" article is quite old. But for me I think it still should display first as the term is in the title and so it is more important than an article where the term is six paragraphs in.

Setting the Sort Options

Now comes the interesting part. When I saw the code above I assumed that this meant there was a parameter in the plugin where I could set the default ordering to be used. So in my case I would change from the default "newest" to the "alpha" option and then I would have search working exactly the way that I want it to work. But guess what? There is no such option in the plugin at all.

search plugin options

Could this option really not be used at all? Surely not?

So if its not in the content-search plugin options it must be in the search module options. That would be the next most logical place for it to be wouldn't it. After all - there is no point in having the content-search plugin enabled if you dont have a search module on the site.

No joy here either - the module doesnt have any ordering options either. Wow - all this code in the plugin and it's just not used at all. Buit wait that can't be right can it. No one writes code that can never be used - that would just be stupid.

There is one last place that we can setup search on a joomla site. From a menu item. Perhaps it is there. Well it turns out that it is used - just not where I expected it to be and probably not somewhere you even have on your site. It's in the options for the search menu item.

search menu option


So now my search results will be ordered how I expect them to be and the article with the search term in the title will be first on the search results? The non-developers amongst us would expect that wouldn't we? We have conducted a search and said to sort the results by title.

Well that is exactly what is happening - the results are being sorted alphabeticaly by title. Just not how I was expecting. the code says to sort the results by the order of the title. My article begins with the letter S so it is sorted near the end and not at the top - it actually moves up about one place on this site to about 17 but still way below where I was expecting it. Try it yourself here and set the ordering to Alphabetical.

I was expecting it sort the results by matches in the title and then by matches in the content. But that's not what the code says or does.


So how can I get Joomla to give me search results that follow established practices of making matches in the title more important? With the current code in the Joomla default search component it's just not possible. Even the hidden and hard to use options don't help you.

So what can you do?

Stop using Search and Use Smart Search instead

I had never used the alternative search component provided with Joomla as I believed that Smart Search (aka Finder) was really only useful on sites with lots and lots of content or where I wanted to do something really, really smart. 

But as the regular search component wasn't doing what I expected I thought it was going to be worth a try to check it out - otherwise I was going to have to try and write some code to "fix" the code.

So after indexing the content and publishing the Smart Search module guess what - the search results are exactly what I expected. Just try it out for yourself. The search module below is the Smart Search module and you will see that a search for the term "replace" produces the resullts I was expecting all along.




At some point in the next few weeks I will change the default search module on this site to use the Smart Search module so everything works how I want it to work. I am just leaving it broken so you can all see the problem yourselves.

J o o m l a !

Brian Teeman

Brian Teeman

Who is Brian?

As a co-founder of Joomla! and OpenSourceMatters Inc I've never been known to be lacking an opinion or being too afraid to express it.

Despite what some people might think I'm a shy and modest man who doesn't like to blow his own trumpet or boast about achievements.

Where is Brian?