Skip to main content

How to Force Joomla's Search Type to Any Exact Word or Phrase

| Gianluca Gabella | Joomla!
share on facebook share on twitter share on linkedin

Index:


When using the standard Joomla search module, the search is done automatically with the 'all words' mode. It is possible to automate the search type with a simple override.

How does search work on Joomla?

The whole 'search' system revolves around a component, one or more plugins and (possibly) a module:

  • Component: we are talking about the 'com_search' component, which takes care of the management and display of results;
  • Plugins: These are one or more 'search' type plugins that contain instructions on what to look for in the database regarding the different Joomla components. Natively, there are search plugins for the "com_content", but if we install Virtuemart, for example, a search component will also be installed for the ecommerce component, which will search within the individual products on sale;
  • Modules: It is possible to configure a module with an input for generic searches. This is not mandatory, as the 'com_search' component can also be called up in other ways.

So, in a nutshell, how does search work on Joomla?

  1. Through the 'search' module I enter one or more words in a form;
  2. These words are sent to the 'com_search' component, which checks all activated 'search' plugins;
  3. Each enabled 'search' plugin searches the database of the component to which it is connected;
  4. "com_search" collects the results passed to it by each 'search' plugin and returns the totality of these results to the user.

It is a very clever way of handling searches, because it allows you to filter results according to content (e.g. search only in static pages, or only in Virtuemart products for sale).

What types of research can I do?

Joomla allows 3 types of search:

  • "all words': is the default search type. If more than one word is searched for, the component will return results where all the words searched for are present, in any order (even non-consecutive).
  • "any word": if more than one word is searched for, the component will return results where at least one of the searched words is present.
  • "exact phrase": if more than one word is searched for, the component will return results where all the searched words are present, in that specific order.

The search types are therefore distinct and give different results depending on the type selected. The problem is that the standard Joomla module does not allow you to choose the search type, but always uses the first one ('all words') as the default. Once you get to the results page you can change the type, but this system forces the user to go through one more step.

How can we solve it?

There are many ways to solve this problem by acting on the component or plugins. But these would require advanced programming skills and there would be a high risk of losing the changes made when the core is next updated.

The solution I propose therefore only acts on the module: it is not exhaustive (it does not solve the problem in its entirety), but it is a very simple trick that immediately solves the problem without having to modify Joomla core files.

Let's get started!

The first thing to do is to create an override of the standard Joomla search module.

So let's go to the Joomla control panel and click on

Top Menu -> Extensions -> Templates -> Template List

Click on the name of the default template we use on our site. For example 'protostar'.

nome del template

Warning: if you use another template, click on the title of that template!

Now click on the tab: 'create override'.

.

override mod search

We will find a list of installed modules and components. We click on 'mod_search'.

If everything was successful, a message will appear on a green background saying:

Message

Override creato in /templates/protostar/html/mod_search
Override creato.
 

From the menu on the left, we now click on "html -> mod_search -> default".
On the right-hand side, the contents of the file will appear, which is basically PHP code.

We go to the bottom of the file, where we find these lines:

<input type="hidden" name="task" value="search" />
<input type="hidden" name="option" value="com_search" />
<input type="hidden" name="Itemid" value="<?php echo $mitemid; ?>" />

These few lines are hidden parameters that are sent together with the form input in order to handle the search.

All we have to do is add, immediately afterwards, this line of code:

<input type="hidden" name="searchphrase" value="any" />

What does it mean?

We are indicating to the "com_search" component that will receive the input to be searched that we want to use "any" (i.e. "any word") as the search type.

We have the option of putting all three search types within the "value" property:

  1. all : means 'search all words', we can omit it because it is the standard search type
  2. any: search for at least one of the words
  3. exact: search for the exact phrase

Conclusions

It is not a complete solution: it does not allow us to choose the type of search from the frontend and is limited to searches made with the module but it is a great way to achieve the goal, and above all it is safe because it is only an override modification, which does not touch any Joomla core files.


If you liked this article, please share it!

share on facebook share on twitter share on linkedin
condividi su Facebookcondividi su Twittercondividi su LinkedIncondividi su WhatsAppcondividi su Telegram