Develop a search engine

Last update by Elxis Team

Develop engines to extend Elxis search to anything you can imagine

A search engine is an extension of component Search. Search engines files can be found under this path components/com_search/engines/. Each engine extends Elxis search into new areas. Search can be performed on local database (i.e. Content items), on local filesystem (i.e. Images) and on remote systems using XML, JSON or other available connectivity system (i.e. YouTube).

Search engine files

A search engine named sample has the following required files:

components/com_search/engines/sample/
components/com_search/engines/sample/sample.engine.php
components/com_search/engines/sample/sample.engine.xml
components/com_search/engines/sample/sample.png

Other files like installer, css, javascript and language files can be used as for any other Elxis extension type (see Modules development guide). The XML file is a standard Elxis parameters file. The png file is a 16x16 pixels image that it is being displayed on search module as a identifier for the search engine. The php file is the file that performs the search and contains an implementation of the searchEngine interface class.

components/com_search/engines/engine.interface.php

A search engine can contain one more optional file to provider search suggestions to the user (open search from browser search box). If you want to add this functionality create a file named sample.suggest.php. Inside that file you must get the q parameter from GET request, perform a search, and return search suggestions JSON formatted with the content type application/x-suggestions+json. Note that you dont return the actual results, only search suggestions. See existing search engines as samples.

 

Engine methods

As a search engine is an implementation of the searchEngine interface it must implement these methods.

<?php
class sampleEngine implements searchEngine {
public function __construct($params) { }
public function engineInfo() { }
public function searchForm() { }
public function search($page=1) {}
public function getTotal() { }
public function getLimit() { }
public function getLimitStart() { }
public function getPage() { }
public function getMaxPage() { }
public function getOptions() { }
public function getResults() { }
public function showResults() { }
}
?>

__construct($params)

Class constructor. Receives as input engine XML parameters (if any) as an object (generated by elxisParameters library). A good practice is to create a private function (i.e. named setOptions) that catches the GET parameters requested (if any) and call this method in the constructor. This way you will know later what the user requested to perform the search.

engineInfo

Returns an array giving information and meta data for the engine. Array keys are title (string), description (string) and metakeys (array).

searchForm

Display to the browser the detailed search form for the specific engine. The forms method should be get and the typical search keyword field should have name q. You can add any search option you want. For consistency between various search engines try to use the same names for the input elements as the other search engines. This way Elxis will be able to switch search engines without loosing the user information.

http://www.example.com/search/content.html?q=test&ordering=dd
http://www.example.com/search/sample.html?q=test&ordering=dd
http://www.example.com/search/youtube.html?q=test&ordering=dd

search($page = 1)

In this function we actually perform the search and return the total number of results found (integer). As input the method accepts the number of page the user is.

getTotal

Return the total number of results found.

getLimit

Return the number of items to display per page (parameter limit).

getLimitStart

Return the number of entry to search from. It is used for pagination. For the first results this value should be 0 (dynamic parameter limitstart).

getPage

Return the number of page we are.

getMaxPage

Return the maximum number of page for the total results found.

/*
Validate and find pagination data
$total: the number of total results found
$page: the number of page user requested
$limit: the number of items to display per page
*/
if ($page < 1) { $page = 1; }
$maxpage = ($total == 0) ? 1 : ceil($total/$limit);
if ($maxpage < 1) { $maxpage = 1; }
if ($page > $maxpage) { $page = $maxpage; }
$limitstart = (($page - 1) * $limit);
//Now we can safely use $page and $limitstart values.

 

getOptions

Return an array with all public search options used.

getResults

Return an array of results found (for current page).

showResults

Generate HTML and display results found (for current page). This method allows you to have custom style on the search results.
 

How do I perform search on third party system?

You need the third party system to provide you a way to communicate with it. The most common method are the XML and JSON APIs which are platform independent and easy to handle. You can perform remote requests using PHPs CURL library. See the built-in YouTube search engine as example.

It has been read 5413 times
Multiple onload events
Next article
Multiple onload events

You are free to copy, distribute and transmit the articles in this site for non-commercial purposes.
Creative Commons 3.0