Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fix for issue with Fluent search in SilverStripe 4 (thanks @hubertusanton)
---
Name: fluentsearchfix
---
SilverStripe\Core\Injector\Injector:
SilverStripe\CMS\Search\SearchForm:
class: Hamaka\Extensions\SearchFormFluentFixExt
<?php
namespace Hamaka\Extensions;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\CMS\Search\SearchForm;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\SS_List;
use TractorCow\Fluent\State\FluentState;
class SearchFormFluentFixExt extends SearchForm
{
/**
* Return dataObjectSet of the results using current request to get info from form.
* Wraps around {@link searchEngine()}.
*
* @return SS_List
*/
public function getResults()
{
// Get request data from request handler
$request = $this->getRequestHandler()->getRequest();
$keywords = $request->requestVar('Search');
$andProcessor = function ($matches) {
return ' +' . $matches[2] . ' +' . $matches[4] . ' ';
};
$notProcessor = function ($matches) {
return ' -' . $matches[3];
};
$keywords = preg_replace_callback('/()("[^()"]+")( and )("[^"()]+")()/i', $andProcessor, $keywords);
$keywords = preg_replace_callback('/(^| )([^() ]+)( and )([^ ()]+)( |$)/i', $andProcessor, $keywords);
$keywords = preg_replace_callback('/(^| )(not )("[^"()]+")/i', $notProcessor, $keywords);
$keywords = preg_replace_callback('/(^| )(not )([^() ]+)( |$)/i', $notProcessor, $keywords);
$keywords = $this->addStarsToKeywords($keywords);
// $results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength, "\"Relevance\" DESC", "", $booleanSearch);
// replace with hard query (todo: pagination)
$keywords = DB::get_conn()->escapeString($keywords);
$htmlEntityKeywords = htmlentities($keywords, ENT_NOQUOTES, 'UTF-8');
$current_locale = FluentState::singleton()->getLocale();
$sitetree_table_localised = 'SiteTree_Localised_' . $current_locale;
$sitetreetables = 'SiteTree_Live.Title, SiteTree_Live.MenuTitle, SiteTree_Live.Content, SiteTree_Live.MetaDescription';
$customResults = DB::query('SELECT DISTINCT SiteTree_Live.ID, SiteTree_Live.Title,
MATCH ('.$sitetreetables.') AGAINST (\'' . str_replace(array('*', '+', '-'), '', $keywords) . '\') AS Relevance
FROM SiteTree_Live
LEFT JOIN SiteTree_Localised AS ' . $sitetree_table_localised . '
ON SiteTree_Live.ID = ' . $sitetree_table_localised . '.RecordID AND ' . $sitetree_table_localised . '.Locale = \'' . $current_locale . '\' AND ((' . $sitetree_table_localised . '.ID IS NOT NULL))
WHERE (
MATCH ('.$sitetreetables.') AGAINST (\'' . $keywords . '\' IN BOOLEAN MODE)
+ MATCH ('.$sitetreetables.') AGAINST (\'' . $htmlEntityKeywords . '\' IN BOOLEAN MODE)
AND SiteTree_Live.ShowInSearch <> 0
)
ORDER BY Relevance DESC
LIMIT 20;');
$results = new ArrayList();
foreach ($customResults as $record) {
$st = SiteTree::get_by_id($record['ID']);
if ( ! empty($st) && $st->canView()) {
$results->push($st);
}
}
return $results;
}
}
@guyvanbael

This comment has been minimized.

Copy link

@guyvanbael guyvanbael commented Feb 10, 2021

Thanks for this... However, when testing this with SS4.7.1 and fluent 5.0.0, my searchbar has a few odd characters. (screenshot). Is there a way to fix this?

Schermafbeelding 2021-02-10 om 10 51 28

@baukezwaan

This comment has been minimized.

Copy link
Owner Author

@baukezwaan baukezwaan commented Feb 10, 2021

Hmm... that's odd.
Seems not quite related to the fix mentioned above.

Maybe paste the code building this search-form, so more context is given on your specific problem. Or maybe try the https://forum.silverstripe.org/, more people can look at it there...

@guyvanbael

This comment has been minimized.

Copy link

@guyvanbael guyvanbael commented Feb 10, 2021

It's the default that comes with silverstripe. Namely: SilverStripe\CMS\Search\SearchForm.php

@guyvanbael

This comment has been minimized.

Copy link

@guyvanbael guyvanbael commented Mar 15, 2021

can i add dataobjects to the search too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment