Searchable data silverstripe dataobject

I am trying to create specific DataObjects (News) in the default SearchResult page. Therefore, as a result, normal pages and news should be displayed.

Is there an easy way to do this in Silverstripe 3? Or is it recommended that you fully encode it - I mean a user controller / action that processes the search query and creates a list of results, which I then show in the user template?

I found this, but obviously the search is disabled right now: https://github.com/arambalakjian/DataObjects-as-Pages

thanks and believes florian

+4
source share
3 answers

I usually use the custom search function after enabling FulltextSearchable . So in _config.php I would have

 FulltextSearchable::enable(); Object::add_extension('NewsStory', "FulltextSearchable('Name,Content')"); 

replacing the name and contents with what DBField you want to search for. And each DataObject search has it in its class to enable search indexes (pretty sure that you need to add and run dev / build before enabling the extension and work only with MySQL).

 static $create_table_options = array( 'MySQLDatabase' => 'ENGINE=MyISAM' ); 

then in my PageController I have my own functions searchForm and results.

Here is the search function, which returns a search form called $search in the template:

 public function search() { if($this->request && $this->request->requestVar('Search')) { $searchText = $this->request->requestVar('Search'); }else{ $searchText = 'Search'; } $f = new TextField('Search', false, $searchText); $fields = new FieldList( $f ); $actions = new FieldList( new FormAction('results', 'Go') ); $form = new Form( $this, 'search', $fields, $actions ); //$form->disableSecurityToken(); $form->setFormMethod('GET'); $form->setTemplate('SearchForm'); return $form; } 

and here is the user-defined function results for processing requests ...

 function results($data, $form, $request) { $keyword = trim($request->requestVar('Search')); $keyword = Convert::raw2sql($keyword); $keywordHTML = htmlentities($keyword, ENT_NOQUOTES, 'UTF-8'); $pages = new ArrayList(); $news = new ArrayList(); $mode = ' IN BOOLEAN MODE'; //$mode = ' WITH QUERY EXPANSION'; //$mode = ''; $siteTreeClasses = array('Page'); $siteTreeMatch = "MATCH( Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords ) AGAINST ('$keyword'$mode) + MATCH( Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords ) AGAINST ('$keywordHTML'$mode)"; $newsItemMatch = "MATCH( Name, Content ) AGAINST ('$keyword'$mode) + MATCH( Name, Content ) AGAINST ('$keywordHTML'$mode)"; //Standard pages foreach ( $siteTreeClasses as $c ) { $query = DataList::create($c) ->where($siteTreeMatch); $query = $query->dataQuery()->query(); $query->addSelect(array('Relevance' => $siteTreeMatch)); $records = DB::query($query->sql()); $objects = array(); foreach( $records as $record ) { if ( in_array($record['ClassName'], $siteTreeClasses) ) $objects[] = new $record['ClassName']($record); } $pages->merge($objects); } //news $query = DataList::create('NewsStory')->where($newsItemMatch); $query = $query->dataQuery()->query(); $query->addSelect(array('Relevance' => $newsItemMatch)); $records = DB::query($query->sql()); $objects = array(); foreach( $records as $record ) $objects[] = new $record['ClassName']($record); $news->merge($objects); //sorting results $pages->sort(array( 'Relevance' => 'DESC', 'Title' => 'ASC' )); $news->sort(array( 'Relevance' => 'DESC', 'Date' => 'DESC' )); //output $data = array( 'Pages' => $pages, 'News' => $news, 'Query' => $keyword ); return $this->customise($data)->renderWith(array('Search','Page')); } 

I add all the page classes that I want to find that extend SiteTree in the $siteTreeClasses , and news pieces can be copied for any other DataObject that is required for the search.

I am not saying that this is the best solution, and it certainly can be improved, but it works for me, and it can be a good expression.

+8
source

I adapted the @colymba solution to the silverstripe module: https://github.com/burnbright/silverstripe-pagesearch

It allows you to set the page type in the url.

+2
source

You will need to substantially rewrite SearchForm->getResults() . It uses Database->searchEngine() , but they target the SiteTree and Page classes.

The β€œright” solution is to submit data to a search engine such as Solr or Sphinx. To do this, we have an SS3-compatible module "fulltextsearch": https://github.com/silverstripe-labs/silverstripe-fulltextsearch It is going to do some presetting, and this is only possible if you can do Solr yourself or willing to pay for a SaaS provider. Once you run it, the possibilities are endless, it is a great tool!

+1
source

All Articles