diff --git a/docs/en/index.md b/docs/en/index.md index 2ffe35e..072399e 100644 --- a/docs/en/index.md +++ b/docs/en/index.md @@ -46,7 +46,11 @@ SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater: ## Basic usage -If you have the [CMS module](https://github.com/silverstripe/silverstripe-cms) installed, you will be able to simply add `$SearchForm` to your template to add a Solr search form. Default configuration is added via the [`ContentControllerExtension`](/src/Solr/Control/ContentControllerExtension.php) and replacement [`SearchForm`](/src/Solr/Forms/SearchForm.php) +If you have the [CMS module](https://github.com/silverstripe/silverstripe-cms) installed, you will be able to simply add `$SearchForm` to your template to add a Solr search form. Default configuration is added via the [`ContentControllerExtension`](/src/Solr/Control/ContentControllerExtension.php) and alternative [`SearchForm`](/src/Solr/Forms/SearchForm.php). + +Ensure that you _don't_ have `SilverStripe\ORM\Search\FulltextSearchable::enable()` set in `_config.php`, as the `SearchForm` action provided by that class will conflict. + +You can override the default template with a new one at `templates/Layout/Page_results_solr.ss`. Otherwise, basic usage is a four step process: diff --git a/src/Search/Updaters/SearchUpdater.php b/src/Search/Updaters/SearchUpdater.php index 5d3d614..f4e7ca3 100644 --- a/src/Search/Updaters/SearchUpdater.php +++ b/src/Search/Updaters/SearchUpdater.php @@ -105,8 +105,8 @@ class SearchUpdater 'command' => $command, 'fields' => array() ); - } // Otherwise update the class label if it's more specific than the currently recorded one - elseif (is_subclass_of($class, $writes[$key]['class'])) { + // Otherwise update the class label if it's more specific than the currently recorded one + } elseif (is_subclass_of($class, $writes[$key]['class'])) { $writes[$key]['class'] = $class; } diff --git a/src/Solr/Control/ContentControllerExtension.php b/src/Solr/Control/ContentControllerExtension.php index f65f8d1..25fdef2 100644 --- a/src/Solr/Control/ContentControllerExtension.php +++ b/src/Solr/Control/ContentControllerExtension.php @@ -55,6 +55,6 @@ class ContentControllerExtension extends Extension 'Query' => DBField::create_field('Text', $form->getSearchQuery()), 'Title' => _t('SilverStripe\\CMS\\Search\\SearchForm.SearchResults', 'Search Results') ]; - return $this->owner->customise($data)->renderWith(['Page_results', 'Page']); + return $this->owner->customise($data)->renderWith(['Page_results_solr', 'Page_results', 'Page']); } } diff --git a/src/Solr/Forms/SearchForm.php b/src/Solr/Forms/SearchForm.php index 9b37abd..a5a2a91 100644 --- a/src/Solr/Forms/SearchForm.php +++ b/src/Solr/Forms/SearchForm.php @@ -10,8 +10,8 @@ use SilverStripe\Forms\TextField; use SilverStripe\FullTextSearch\Search\FullTextSearch; use SilverStripe\FullTextSearch\Search\Queries\SearchQuery; use SilverStripe\FullTextSearch\Solr\SolrIndex; -use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataObject; +use SilverStripe\View\ArrayData; class SearchForm extends Form { @@ -55,7 +55,7 @@ class SearchForm extends Form * Return dataObjectSet of the results using current request to get info from form. * Simplest implementation loops over all Solr indexes * - * @return ArrayList + * @return ArrayData */ public function getResults() { @@ -65,24 +65,28 @@ class SearchForm extends Form $searchTerms = $request->requestVar('Search'); $query = SearchQuery::create()->addSearchTerm($searchTerms); - $indexes = FullTextSearch::get_indexes(SolrIndex::class); - $results = ArrayList::create(); + $params = [ + 'spellcheck' => 'true', + 'spellcheck.collate' => 'true', + ]; + + // Get the first index + $indexClasses = FullTextSearch::get_indexes(SolrIndex::class); + $indexClass = reset($indexClasses); /** @var SolrIndex $index */ - foreach ($indexes as $index) { - $results->merge($index->search($query)->Matches); - } + $index = $indexClass::singleton(); + $results = $index->search($query, -1, -1, $params); // filter by permission if ($results) { - /** @var DataObject $result */ - foreach ($results as $result) { - if (!$result->canView()) { - $results->remove($result); + foreach ($results->Matches as $match) { + /** @var DataObject $match */ + if (!$match->canView()) { + $results->Matches->remove($match); } } } - return $results; } diff --git a/templates/Layout/Page_results_solr.ss b/templates/Layout/Page_results_solr.ss new file mode 100644 index 0000000..d573908 --- /dev/null +++ b/templates/Layout/Page_results_solr.ss @@ -0,0 +1,56 @@ +
+

$Title

+ + <% if $Query %> +

You searched for "{$Query}"

+ <% end_if %> + + <% if $Results.Suggestion %> +

Did you mean $Results.SuggestionNice?

+ <% end_if %> + + <% if $Results.Matches %> + + <% else %> +

Sorry, your search query did not return any results.

+ <% end_if %> + + <% if $Results.Matches.MoreThanOnePage %> +
+ +

Page $Results.Matches.CurrentPage of $Results.Matches.TotalPages

+
+ <% end_if %> +