Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Making content searchable

SheriffenSheriffen
edited August 2010 in Vanilla 2.0 - 2.8
I have written a simple CMS application and I want the content of the pages to be searchable though the vanilla default search (everything from the same box)

How do I add own items to the search?

Comments

  • ToddTodd Vanilla Staff
    edited August 2010
    What you want to do is hook into the search system. Vanilla (the application) does this so I'll use it as an example.
    1. Make sure your content has fulltext indexes. You do this in the structure file by specifying an index type of 'fulltext' on the relevant columns. Look at Vanilla's structure file if you aren't sure about the correct syntax.
    2. Using the SQL object, build queries that will grab your content in a format that the search expects. Again, Vanilla does all of this in /applications/vanilla/models/class.vanillasearchmodel.php. Make sure all of your columns are selected in the same order that Vanilla does it or things will get mixed up. You will notice that there is a $this->SQL->Reset. Make sure you don't leave this out or you will some very strange errors.
    3. Hook into the search system in your application's hooks file. If you are doing this in a plugin then to it in the plugin (since the hooks file is just a plugin. Your event handler will look something like this (again, from Vanilla):
      /**
      * Add the discussion search to the search.
      * @param SearchController $Sender
      */
      public function SearchModel_Search_Handler($Sender) {
      $SearchModel = new VanillaSearchModel();
      $SearchModel->Search($Sender);
      }
    If you have just a simple, one table search then you can just put everything in your hooks file.

    So to summarize, look at the following files:
    • /applications/vanilla/settings/structure.php (specifically the Body column of the Comment table)
    • /applications/vanilla/models/class.vanillasearchmodel.php
    • /applications/vanilla/settings/hooks.php (specifically SearchModel_Search_Handler())
  • Much much appreciated!
  • Thanks for the direction. I found this question and answer while searching how to add columns to the search function, and I thought I'd share how Todd's answer led me to part of my solution.

    The directory:

    applications/vanilla/models/class.vanillasearchmodel.php

    has a function DiscussionSql() that accepts the columns to match against. I added

    d.Tags

    to

    'd.Body, d.Name'

    in the second argument. You are required to add whatever columns from the Discussions table you add to the match clause to the FULLTEXT index in the database.

    This hardcoded solution probably isn't best practice and should be handled with a hook or a plugin as Todd describes above.
Sign In or Register to comment.