2014-10-28 06:42:27 +01:00
title: Fulltext Search
summary: Fulltext search allows sophisticated searching on text content.
# FulltextSearchable
Fulltext search allows advanced search criteria for searching words within a text based data column. While basic
2017-07-03 03:22:12 +02:00
Fulltext search can be achieved using the built-in [MySQLDatabase ](api:SilverStripe\ORM\Connect\MySQLDatabase ) class a more powerful wrapper for Fulltext
2014-10-28 06:42:27 +01:00
search is provided through a module.
< div class = "notice" markdown = "1" >
See the [FulltextSearch Module ](https://github.com/silverstripe-labs/silverstripe-fulltextsearch/ ). This module provides
a high level wrapper for running advanced search services such as Solr, Lucene or Sphinx in the backend rather than
`MySQL` search.
< / div >
## Adding Fulltext Support to MySQLDatabase
2017-07-03 03:22:12 +02:00
The [MySQLDatabase ](api:SilverStripe\ORM\Connect\MySQLDatabase ) class defaults to creating tables using the InnoDB storage engine. As Fulltext search in MySQL
2014-10-28 06:42:27 +01:00
requires the MyISAM storage engine, any DataObject you wish to use with Fulltext search must be changed to use MyISAM
storage engine.
You can do so by adding this static variable to your class definition:
2017-08-03 02:51:32 +02:00
```php
2014-10-28 06:42:27 +01:00
< ?php
class MyDataObject extends DataObject {
private static $create_table_options = array(
'MySQLDatabase' => 'ENGINE=MyISAM'
);
}
2017-08-03 02:51:32 +02:00
```
2014-10-28 06:42:27 +01:00
2017-07-03 03:22:12 +02:00
The [FulltextSearchable ](api:SilverStripe\ORM\Search\FulltextSearchable ) extension will add the correct `Fulltext` indexes to the data model.
2014-10-28 06:42:27 +01:00
< div class = "alert" markdown = "1" >
2017-07-03 03:22:12 +02:00
The [SearchForm ](api:SilverStripe\CMS\Search\SearchForm ) and [FulltextSearchable ](api:SilverStripe\ORM\Search\FulltextSearchable ) API's are currently hard coded to be specific to `Page` and `File`
2014-10-28 06:42:27 +01:00
records and cannot easily be adapted to include custom `DataObject` instances. To include your custom objects in the
default site search, have a look at those extensions and modify as required.
< / div >
2015-01-29 00:48:46 +01:00
### Fulltext Filter
2017-07-03 03:22:12 +02:00
SilverStripe provides a [FulltextFilter ](api:SilverStripe\ORM\Filters\FulltextFilter ) which you can use to perform custom fulltext searches on
[DataList ](api:SilverStripe\ORM\DataList )s.
2015-01-29 00:48:46 +01:00
Example DataObject:
2017-08-03 02:51:32 +02:00
```php
2015-01-29 00:48:46 +01:00
class SearchableDataObject extends DataObject {
private static $db = array(
"Title" => "Varchar(255)",
"Content" => "HTMLText",
);
private static $indexes = array(
'SearchFields' => array(
'type' => 'fulltext',
2017-05-19 20:32:32 +02:00
'columns' => ['Title', 'Content'],
2015-01-29 00:48:46 +01:00
)
);
private static $create_table_options = array(
'MySQLDatabase' => 'ENGINE=MyISAM'
);
}
2017-08-03 02:51:32 +02:00
```
2015-01-29 00:48:46 +01:00
Performing the search:
2017-08-03 02:51:32 +02:00
```php
2016-08-29 07:00:27 +02:00
SearchableDataObject::get()->filter('SearchFields:Fulltext', 'search term');
2017-08-03 02:51:32 +02:00
```
2015-01-29 00:48:46 +01:00
If your search index is a single field size, then you may also specify the search filter by the name of the
field instead of the index.
2014-10-28 06:42:27 +01:00
## API Documentation
2017-07-03 03:22:12 +02:00
* [FulltextSearchable ](api:SilverStripe\ORM\Search\FulltextSearchable )