Merge pull request #216 from creative-commoners/pulls/solr_search_form

Replace default CMS SearchForm with Solr version
This commit is contained in:
Dylan Wagstaff 2018-06-11 10:51:41 +12:00 committed by GitHub
commit aacbd3d303
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 230 additions and 3 deletions

View File

@ -4,3 +4,6 @@ Name: fulltextsearchconfig
SilverStripe\ORM\DataObject:
extensions:
- SilverStripe\FullTextSearch\Search\Extensions\SearchUpdater_ObjectHandler
SilverStripe\CMS\Controllers\ContentController:
extensions:
- SilverStripe\FullTextSearch\Solr\Control\ContentControllerExtension

View File

@ -46,7 +46,13 @@ SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater:
## Basic usage
Basic usage is a four step process:
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:
1). Define an index in SilverStripe (Note: The specific connector index instance - that's what defines which engine gets used)

View File

@ -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;
}

View File

@ -0,0 +1,60 @@
<?php
namespace SilverStripe\FullTextSearch\Solr\Control;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\TextField;
use SilverStripe\FullTextSearch\Solr\Forms\SearchForm;
use SilverStripe\ORM\FieldType\DBField;
class ContentControllerExtension extends Extension
{
private static $allowed_actions = array(
'SearchForm',
'results',
);
/**
* Site search form
*
* @return SearchForm
*/
public function SearchForm()
{
$searchText = _t('SilverStripe\\CMS\\Search\\SearchForm.SEARCH', 'Search');
/** @var HTTPRequest $currentRequest */
$currentRequest = $this->owner->getRequest();
if ($currentRequest && $currentRequest->getVar('Search')) {
$searchText = $currentRequest->getVar('Search');
}
$fields = FieldList::create(
TextField::create('Search', false, $searchText)
);
$actions = FieldList::create(
FormAction::create('results', _t('SilverStripe\\CMS\\Search\\SearchForm.GO', 'Go'))
);
return SearchForm::create($this->owner, 'SearchForm', $fields, $actions);
}
/**
* Process and render search results.
*
* @param array $data The raw request data submitted by user
* @param SearchForm $form The form instance that was submitted
* @param HTTPRequest $request Request generated for this action
*/
public function results($data, $form, $request)
{
$data = [
'Results' => $form->getResults(),
'Query' => DBField::create_field('Text', $form->getSearchQuery()),
'Title' => _t('SilverStripe\\CMS\\Search\\SearchForm.SearchResults', 'Search Results')
];
return $this->owner->customise($data)->renderWith(['Page_results_solr', 'Page_results', 'Page']);
}
}

View File

@ -0,0 +1,102 @@
<?php
namespace SilverStripe\FullTextSearch\Solr\Forms;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\TextField;
use SilverStripe\FullTextSearch\Search\FullTextSearch;
use SilverStripe\FullTextSearch\Search\Queries\SearchQuery;
use SilverStripe\FullTextSearch\Solr\SolrIndex;
use SilverStripe\ORM\DataObject;
use SilverStripe\View\ArrayData;
class SearchForm extends Form
{
private static $casting = array(
'SearchQuery' => 'Text'
);
/**
* @param RequestHandler $controller
* @param string $name The name of the form (used in URL addressing)
* @param FieldList $fields Optional, defaults to a single field named "Search". Search logic needs to be customized
* if fields are added to the form.
* @param FieldList $actions Optional, defaults to a single field named "Go".
*/
public function __construct(
RequestHandler $controller = null,
$name = 'SearchForm',
FieldList $fields = null,
FieldList $actions = null
) {
if (!$fields) {
$fields = FieldList::create(
TextField::create('Search', _t(__CLASS__.'.SEARCH', 'Search'))
);
}
if (!$actions) {
$actions = FieldList::create(
FormAction::create("results", _t(__CLASS__.'.GO', 'Go'))
);
}
parent::__construct($controller, $name, $fields, $actions);
$this->setFormMethod('get');
$this->disableSecurityToken();
}
/**
* Return dataObjectSet of the results using current request to get info from form.
* Simplest implementation loops over all Solr indexes
*
* @return ArrayData
*/
public function getResults()
{
// Get request data from request handler
$request = $this->getRequestHandler()->getRequest();
$searchTerms = $request->requestVar('Search');
$query = SearchQuery::create()->addSearchTerm($searchTerms);
$params = [
'spellcheck' => 'true',
'spellcheck.collate' => 'true',
];
// Get the first index
$indexClasses = FullTextSearch::get_indexes(SolrIndex::class);
$indexClass = reset($indexClasses);
/** @var SolrIndex $index */
$index = $indexClass::singleton();
$results = $index->search($query, -1, -1, $params);
// filter by permission
if ($results) {
foreach ($results->Matches as $match) {
/** @var DataObject $match */
if (!$match->canView()) {
$results->Matches->remove($match);
}
}
}
return $results;
}
/**
* Get the search query for display in a "You searched for ..." sentence.
*
* @return string
*/
public function getSearchQuery()
{
return $this->getRequestHandler()->getRequest()->requestVar('Search');
}
}

View File

@ -0,0 +1,56 @@
<div id="Content" class="searchResults">
<h1>$Title</h1>
<% if $Query %>
<p class="searchQuery">You searched for &quot;{$Query}&quot;</p>
<% end_if %>
<% if $Results.Suggestion %>
<p class="spellCheck">Did you mean <a href="{$Link}SearchForm?Search=$Results.SuggestionQueryString">$Results.SuggestionNice</a>?</p>
<% end_if %>
<% if $Results.Matches %>
<ul id="SearchResults">
<% loop $Results.Matches %>
<li>
<h4>
<a href="$Link">
<% if $MenuTitle %>
$MenuTitle
<% else %>
$Title
<% end_if %>
</a>
</h4>
<p><% if $Abstract %>$Abstract.XML<% else %>$Content.ContextSummary<% end_if %></p>
<a class="readMoreLink" href="$Link" title="Read more about &quot;{$Title}&quot;">Read more about &quot;{$Title}&quot;...</a>
</li>
<% end_loop %>
</ul>
<% else %>
<p>Sorry, your search query did not return any results.</p>
<% end_if %>
<% if $Results.Matches.MoreThanOnePage %>
<div id="PageNumbers">
<div class="pagination">
<% if $Results.Matches.NotFirstPage %>
<a class="prev" href="$Results.Matches.PrevLink" title="View the previous page">&larr;</a>
<% end_if %>
<span>
<% loop $Results.Matches.Pages %>
<% if $CurrentBool %>
$PageNum
<% else %>
<a href="$Link" title="View page number $PageNum" class="go-to-page">$PageNum</a>
<% end_if %>
<% end_loop %>
</span>
<% if $Results.Matches.NotLastPage %>
<a class="next" href="$Results.Matches.NextLink" title="View the next page">&rarr;</a>
<% end_if %>
</div>
<p>Page $Results.Matches.CurrentPage of $Results.Matches.TotalPages</p>
</div>
<% end_if %>
</div>