mirror of
https://github.com/silverstripe/silverstripe-fulltextsearch
synced 2024-10-22 12:05:29 +00:00
Merge pull request #216 from creative-commoners/pulls/solr_search_form
Replace default CMS SearchForm with Solr version
This commit is contained in:
commit
aacbd3d303
@ -4,3 +4,6 @@ Name: fulltextsearchconfig
|
|||||||
SilverStripe\ORM\DataObject:
|
SilverStripe\ORM\DataObject:
|
||||||
extensions:
|
extensions:
|
||||||
- SilverStripe\FullTextSearch\Search\Extensions\SearchUpdater_ObjectHandler
|
- SilverStripe\FullTextSearch\Search\Extensions\SearchUpdater_ObjectHandler
|
||||||
|
SilverStripe\CMS\Controllers\ContentController:
|
||||||
|
extensions:
|
||||||
|
- SilverStripe\FullTextSearch\Solr\Control\ContentControllerExtension
|
||||||
|
@ -46,7 +46,13 @@ SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater:
|
|||||||
|
|
||||||
## Basic usage
|
## 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)
|
1). Define an index in SilverStripe (Note: The specific connector index instance - that's what defines which engine gets used)
|
||||||
|
|
||||||
|
@ -105,8 +105,8 @@ class SearchUpdater
|
|||||||
'command' => $command,
|
'command' => $command,
|
||||||
'fields' => array()
|
'fields' => array()
|
||||||
);
|
);
|
||||||
} // Otherwise update the class label if it's more specific than the currently recorded one
|
// Otherwise update the class label if it's more specific than the currently recorded one
|
||||||
elseif (is_subclass_of($class, $writes[$key]['class'])) {
|
} elseif (is_subclass_of($class, $writes[$key]['class'])) {
|
||||||
$writes[$key]['class'] = $class;
|
$writes[$key]['class'] = $class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
60
src/Solr/Control/ContentControllerExtension.php
Normal file
60
src/Solr/Control/ContentControllerExtension.php
Normal 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']);
|
||||||
|
}
|
||||||
|
}
|
102
src/Solr/Forms/SearchForm.php
Normal file
102
src/Solr/Forms/SearchForm.php
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
56
templates/Layout/Page_results_solr.ss
Normal file
56
templates/Layout/Page_results_solr.ss
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
<div id="Content" class="searchResults">
|
||||||
|
<h1>$Title</h1>
|
||||||
|
|
||||||
|
<% if $Query %>
|
||||||
|
<p class="searchQuery">You searched for "{$Query}"</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 "{$Title}"">Read more about "{$Title}"...</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">←</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">→</a>
|
||||||
|
<% end_if %>
|
||||||
|
</div>
|
||||||
|
<p>Page $Results.Matches.CurrentPage of $Results.Matches.TotalPages</p>
|
||||||
|
</div>
|
||||||
|
<% end_if %>
|
||||||
|
</div>
|
Loading…
x
Reference in New Issue
Block a user