I´m trying to have certain DataObjects (News) displayed in the default SearchResult Page. So the result should display normal Pages and News.
Is there an easy way to accomplish that in Silverstripe 3? Or is it recommended to code it completely custom - I mean a custom controller/action which handles the search request and creates a result list, which I display then in a custom template?
I found this, but obviously search is disabled right now: https://github.com/arambalakjian/DataObjects-as-Pages
Thx and regards, Florian
You'll need to substantially overwrite
SearchForm->getResults()
. It usesDatabase->searchEngine()
, but those are tailored towardsSiteTree
andPage
classes.The "proper" solution is to feed the data into a search engine like Solr or Sphinx. We have the SS3-compatible "fulltextsearch" module for this purpose: https://github.com/silverstripe-labs/silverstripe-fulltextsearch It's going to take some upfront setup, and is only feasible if you can either host Solr yourself, or are prepared to pay for a SaaS provider. Once you've got it running though, the possibilities are endless, its a great tool!
I have adapted @colymba's solution into a silverstripe module: https://github.com/burnbright/silverstripe-pagesearch
It allows setting the pagetype in the url.
I usually but together a custom search function after enabling
FulltextSearchable
. So in_config.php
I would havereplacing Name and Content with whatever
DBField
you want to be searchable. And each searchableDataObject
have this in their class to enable search indexes (pretty sure this needs to be added and run dev/build before enabling the extension, and only works on MySQL DB).then in my
PageController
I have my custom searchForm and results functions.Here is the
search
function that returns the search form, called with$search
in the template:and here the custom
results
function to handle the queries...I add all the Page classes I want to be searched and that extend
SiteTree
in the$siteTreeClasses
array, and the News parts can be pretty much copied for any otherDataObject
I need searchable.I am not saying this is the best solution and this can definitely be improved on, but it works for me and this might be a good stating point.