Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Usage overview

nyeholt edited this page · 8 revisions

Install Solr (packages available for most OSes)

For demonstration and testing purposes, a standalone Jetty based installation of solr is available in the solr/ subdirectory. To execute, simply change to that directory and run java -jar start.jar - the default settings will be fine for evaluation.

If you are running a custom Solr instance, make sure to copy the solr/solr/conf/schema.xml file to your solr instance - there are a couple of custom types defined that SilverStripe uses.

If you have a configuration different to the default locahost:8983/solr configuration, you can configure things by calling

SolrSearchService::$solr_details = array('host' => 'ip.address', 'port' => ':port', 'context' => '/context_path_on_host');

Optional configuration settings

  • multi-core instances of Solr must define the core in the SolrSearchService::$solr_details["context"] path, as so SolrSearchService::$solr_details = array('host' => '127.0.0.1', 'port' => '8983', 'context' => '/solr/core0');
  • If you are running with the built in jetty servlet container, and want to specify where the indexes go, set SolrSearchService::$java_bin = '/usr/bin/java -Dsolr.data.dir=/path/to/solr_data ';

Start Solr

If using the provided sample solr instance, change directory (from the silverstripe root) to solr/solr (yeah, I know...). Then, type java -server -jar start.jar. This will start solr in a local Jetty instance.

Add the extension to your pages

Add the SolrSearchable decorator to any SiteTree objects you want to search. Support for other data objects may work, though file indexing is not yet supported

Object::add_extension('SiteTree', 'SolrIndexable');

Additionally, you need to add the extension that makes a SearchForm available in your page templates.

Object::add_extension('Page_Controller', 'SolrSearchExtension');

Reindex your content

Run the dev/tasks/SolrReindexTask to index all your content in Solr. You'll see a bunch of things happening in your Solr console if you have that open.

Configure your search page

This module creates a new page of type SolrSearchPage in your site's root. This should be published before being able to perform searches.

Adding new fields to be indexed

To add different fields to be indexed by Solr, you need to add them to your $searchable_fields static variable. This can be done either in your Page class, or by specifying them via an extension like

class MySiteTree extends SiteTreeDecorator {
    public function updateSearchableFields(&$fields) {
        $fields['MetaDescription'] = array(
            'title'             => 'MetaDescription',
            'filter'            => 'PartialMatchFilter',
        );

        $fields['MetaKeywords'] = array(
            'title'             => 'MetaKeywords',
            'filter'            => 'PartialMatchFilter',
        );
    }
}

Note that you need to use the ampersand in the &$field definition there - you're modifying an array, and they're passed by value (meaning a copy) in PHP.

Once indexed, you then need to make sure that the items are included in search results. There's two ways of doing this - specifying it in your query directly, eg MetaKeywords_ms:search_string, or adding it to the default fields in the query,

SolrSearchService::add_default_query_field('MetaDescription_t');

Be aware that you need to include the Solr name of the variable (MetaKeywords_ms, MetaDescription_t etc) as it would be converted by the following

    switch ($type) {
        case 'MultiValueField': {
            return $field.'_ms';
        }
        case 'Text':
        case 'HTMLText': {
            return $field.'_t';
        }
        case 'SS_Datetime': {
            return $field.'_dt';
        }
        case 'Enum':
        case 'Varchar': {
            return $field.'_ms';
        }
        case 'Int':
        case 'Integer': {
            return $field.'_i';
        }
        default: {
            return $field.'_ms';
        }
    }

Finally - be aware that only _t (text field) types do tokenised matches! What this means is that for other string field types (eg _ms) you must have a full text match for a result to be returned, so a query like silverstripe will only match if that is the exact content of the Keywords field, meaning "silverstripe cms" will NOT be found. However, a query of silverstripe* will be successful.

Under the covers, the default search page implementation uses SolrSearchService->parseSearch to convert a user 'search' input to a lucene query. It's very simple, but will be a point of extension in future.

Geospatial search

With Solr 3.6, it's possible to use some basic geospatial searching - see http://wiki.apache.org/solr/SpatialSearch

To add geospatial fields to your data objects, add a field of type SolrGeoPoint

public static $db = array(
    'Location'      => 'SolrGeoPoint',
);

At present there's no integration with the SolrSearchPage for automatically creating queries with this data; you must manually execute queries to restrict items.

Template Options

Displaying a search input

The SolrSearchExtension makes a $SearchForm variable available in all your pages. You can place this anywhere in your site to access a small search box. Additionally, you can have a standalone search page (by default, created at /search) that will trigger searches

Displaying Faceting information

Facets can be used in your templates with code similar to the following. In this instance, the argument passed to the Facets call is the name of a multivalue parameter on a set of dataobjects that have been indexed in Solr.

<div id="Facets">
    <div class="facetList">
            <h3>Keywords</h3>
            <ul>
            <% control Facets(AlcKeywords_ms) %>
            <li><a href="$SearchLink">$Name</a> ($Count)</li>
            <% end_control %>
            </ul>
    </div>
     <div class="clear"><!-- --></div>
</div>

To customise search results display, provide a SolrSearchPage_results.ss file in your theme's templates directory. There's a default available

Something went wrong with that request. Please try again.