Search Adapters
Search adapters generate search results for the frontend website search. They can pull data in from third party search engines and display those results in the website's templates.
Creating a search adapter class
Create a PHP class that implements Jadu\Service\Search\AdapterInterface
and
extends Jadu\Service\Search\Adapter\AbstractAdapter
. An example search adapter
is provided in the developer training GitHub repository.
The search()
method on the adapter is invoked when a user submits a search query.
It should retrieve results, format them and return them to the search template
controller. The object returned should be an instance of RupaSearchResult
.
For example:
public function search()
{
$input = ServiceContainer::getInstance()->getInput();
$searchResults = $this->getSearchService()->search(
$input->request('q'),
$input->get('quoteQuery', null),
$input->get('orQuery', null),
$input->get('excludeWords', null),
intval($input->get('startNum', 1)),
intval($input->get('numToShow', 10))
);
$results = new RupaSearchResult();
$results->documentsFound = $searchResults->getTotal();
$results->time = $searchResults->getTime();
$results->startNumber = $searchResults->getStart();
$results->endNumber = $searchResults->getEnd();
foreach ($searchResults->getItems() as $item) {
// $results->resultItems[] = ...
}
return $results;
}
Each result item should be created as an instance of RupaResultItem
.
For example:
$resultItem = new RupaResultItem();
$resultItem->mime = '';
$resultItem->setTitle('Result one');
$resultItem->setSnippet('A short description or snippet from the result.');
$resultItem->setUrl('https://www.bbc.co.uk');
$results->resultItems[] = $resultItem;
Some search providers return alternative search term suggestions - such as when a
user has misspelt a word in their query. These can be processed and included in
the result set by creating a new instance of RupaResultsSpellingSuggestion
) for
each term.
For example:
$spelling = new RupaResultSpellingSuggestion();
$spelling->setQuery('tst');
$spelling->setSuggestion('test');
$results->spellingSuggestions[] = $spelling;
For full compatibility with the search templates, the following RupaSearchResult
properties should be set by the search adapter class:
Property | Description |
---|---|
documentsFound | The number of results being returned by this search |
time | The time taken to complete the search |
startNumber | The number of the first item returned in this result object |
endNumber | The number of the last item returned in this result object |
spellingSuggestions | An array of spelling suggestion objects |
resultItems | An array of search result objects |
Enabling the search adapter
The active search adapter is controlled by the value of the SEARCH_ADAPTER
constant.
If the constant is an empty string, the application falls back to native Jadu Central
search adapter
To use your own search adapter, the constant should be set to the fully qualified class name of the search adapter class.