Skip to content

Instantly share code, notes, and snippets.

@wilr
Last active September 5, 2016 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilr/735cab62711edb29979d6adfad8b7018 to your computer and use it in GitHub Desktop.
Save wilr/735cab62711edb29979d6adfad8b7018 to your computer and use it in GitHub Desktop.
<?php
class SearchIndexWithTags extends SolrSearchIndex
{
public function init()
{
$this->addFilterField('SiteTree_TagID');
parent::init();
}
public function getFieldDefinitions()
{
$xml = parent::getFieldDefinitions();
$xml .= "\n\t\t<field name='SiteTree_TagID' type='tint' indexed='true' stored='false' multiValued='true' />";
return $xml;
}
protected function _addAs($object, $base, $options)
{
$includeSubs = $options['include_children'];
$doc = new Apache_Solr_Document();
$doc->setField('_documentid', $this->getDocumentID($object, $base, $includeSubs));
$doc->setField('ID', $object->ID);
$doc->setField('ClassName', $object->ClassName);
foreach (SearchIntrospection::hierarchy(get_class($object), false) as $class) {
$doc->addField('ClassHierarchy', $class);
}
foreach ($this->getFieldsIterator() as $name => $field) {
if ($field['base'] == $base) {
$this->_addField($doc, $object, $field);
}
}
// add each of the tags on the page on
foreach($object->Tags() as $tag) {
$doc->addField('SiteTree_TagID', $tag->ID);
}
try {
$this->getService()->addDocument($doc);
} catch (Exception $e) {
SS_Log::log($e, SS_Log::WARN);
return false;
}
return $doc;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment