Jump to:

23490 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Possible solution to allow the built-in search to include fields without hacking the search engine?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2191 Views
  • Codetapper
    Avatar
    Community Member
    8 Posts

    Possible solution to allow the built-in search to include fields without hacking the search engine? Link to this post

    I have read the forums regarding the limitations of searches in Silverstripe (whereby the engine will only search the title and content fields) and like others, found the search rather disappointing.

    Is it possible to create a page type with all the relevant fields you need, and the content editor the CMS user sees is actually a different field (let's call it rawcontent for example), and when it is saved, it copies all the fields plus the rawcontent into the $Content section and saves? Will that work?

    That way a search will still index all the data for that record, and the template would just be changed to display $rawcontent when the page is viewed.

    If this is possible, what is the "beforesave" type method? I find the API very difficult to follow compared to other languages and prefer code snippets!

  • schellmax
    Avatar
    Community Member
    126 Posts

    Re: Possible solution to allow the built-in search to include fields without hacking the search engine? Link to this post

    i like your approach, didn't think of that.
    what you're looking for is the 'onBeforeWrite' hook (http://doc.silverstripe.org/doku.php?id=datamodel#onbeforewrite)

  • brokemeister
    Avatar
    Community Member
    30 Posts

    Re: Possible solution to allow the built-in search to include fields without hacking the search engine? Link to this post

    Hi!

    Just use following snippet in MainDataObject (e.g. Page):

    static $db = array (
          "ContentToSearch" => "Text",
       );

    static $indexes = array(
          "SearchFields" => "fulltext (Title, ContentToSearch)",
          "TitleSearchFields" => "fulltext (Title)",
       );

    protected $searchable_content_fields = array(); //Here you can enter your fields

    protected function onBeforeWrite() {
          // CAUTION: You are required to call the parent-function, otherwise sapphire will not execute the request.
          parent::onBeforeWrite();
          
          if (is_array($this->searchable_content_fields)
             && count($this->searchable_content_fields) > 0) {
                $SearchContent = array();
                foreach ($this->searchable_content_fields AS $field) {
                   if (!empty($this->$field)) {
                      $SearchContent[] = $this->$field;
                   }
                }
                $this->ContentToSearch = implode(chr(10), $SearchContent);
          }
       }

    You also can use Content-field...

    Cheers,

    Malte

  • Codetapper
    Avatar
    Community Member
    8 Posts

    Re: Possible solution to allow the built-in search to include fields without hacking the search engine? Link to this post

    Thanks for the reply. When I put that code into top part of Page.php and rebuild the database I get the following error:

    (User Error) Couldn't run query: CREATE TABLE `Page` ( `ID` int(11) not null auto_increment, `ContentToSearch` mediumtext character set utf8 collate utf8_general_ci, fulltext `SearchFields` (Title,ContentToSearch), fulltext `TitleSearchFields` (Title), primary key (ID) ) TYPE=MyISAM Key column 'Title' doesn't exist in table
    GET /ss/db/build?flush=1

    Line 400 in C:\wamp\www\ss\sapphire\core\model\MySQLDatabase.php

    Also if you look at the indexes in phpMyAdmin, it has all of the following:

    Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords

    Does the code above mean it's not going to index the meta tags anymore unless I re-add them to the $indexes array? (Is this because of indexing changes in the version you used to write the code or something? I am using the latest version 2.3.1).

    2191 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.