Skip to content

Instantly share code, notes, and snippets.

@ss23

ss23/foo.php Secret

Created October 10, 2013 21:48
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 ss23/bd20caaaa03cc93c3f2f to your computer and use it in GitHub Desktop.
Save ss23/bd20caaaa03cc93c3f2f to your computer and use it in GitHub Desktop.
<?php
class PageSolrIndex extends SolrIndex {
function init() {
$this->addClass('Page');
$this->addClass('DMSDocument');
$stored = (Director::isDev()) ? 'true' : 'false';
// Fields are available on both types
$this->addFulltextField('Title', 'Text', array('boost' => '3',));
// Highlighted fields need to be stored
$this->addFulltextField('Content', 'HTMLText', array('stored' => $stored));
$this->addFulltextField('Description', 'HTMLText', array('stored' => $stored, 'boost' => '1.25'));
// Technically, filter and sort fields are the same in Solr/Lucene
$this->addSortField('ViewCount');
// Caution: Stores LastEdited for SiteTree, but LastChanged for DMSDocument (see below)
$this->addSortField('LastEdited', 'SSDatetime');
//$this->addSortField('Title');
$this->addFilterField('ShowInSearch');
$this->addFilterField('CanViewType');
$this->addFilterField('EmbargoedIndefinitely');
$this->addFilterField('EmbargoedUntilPublished');
$this->addFilterField('EmbargoedUntilDate', 'SSDatetime');
$this->addFilterField('ExpireAtDate', 'SSDatetime');
// Aggregates for DMSDocument
$this->addFilterField('ViewerGroups.ID', 'Int');
$this->addFilterField('Pages.ID', 'Int');
$this->addFilterField('ContentType.Name');
// Aggregates for Page
$this->addFilterField('Documents.ID', 'Int');
$this->addFilterField('WorkStream.Name');
$this->addFilterField('Topics.Name');
// Aggregate fields for spelling checks
$this->addCopyField('SiteTree_Title', 'spellcheckData');
$this->addCopyField('DMSDocument_Title', 'spellcheckData');
$this->addCopyField('SiteTree_Content', 'spellcheckData');
$this->addCopyField('DMSDocument_Content', 'spellcheckData');
// Aggregate fields for highlighting
$this->addCopyField('SiteTree_Title', 'highlightData', array('maxChars' => 10000));
$this->addCopyField('DMSDocument_Title', 'highlightData', array('maxChars' => 10000));
$this->addCopyField('SiteTree_Content', 'highlightData', array('maxChars' => 10000));
$this->addCopyField('DMSDocument_Content', 'highlightData', array('maxChars' => 10000));
// Don't support searching staging
$this->excludeVariantState(array('SearchVariantVersioned' => 'Stage'));
}
function getName() {
$name = parent::getName();
if(defined('SS_SOLR_INDEXNAME_SUFFIX')) $name .= SS_SOLR_INDEXNAME_SUFFIX;
return $name;
}
protected function _addAs($object, $base, $options) {
$includeSubs = $options['include_children'];
$doc = new Apache_Solr_Document();
// Always present fields
$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);
// Add the user-specified fields
foreach ($this->getFieldsIterator() as $name => $field) {
if ($field['base'] == $base) $this->_addField($doc, $object, $field);
}
// CUSTOM Duplicate index combined fields ("Title" rather than "DMSDocument_Title" and "SiteTree_Title").
// This allows us to sort on these fields without deeper architectural changes to the fulltextsearch module.
// Note: We can't use <copyField> for this purpose because it only writes into multiValue=true
// fields, and those can't be (reliably) sorted on.
$this->_addField($doc, $object, $this->getCustomPropertyFieldData('Title', $object));
$lastEditedField = ($object instanceof DMSDocument) ? 'LastChanged' : 'LastEdited';
$this->_addField($doc, $object, $this->getCustomPropertyFieldData('LastEdited', $object, 'SSDatetime', $lastEditedField));
$this->_addField($doc, $object, $this->getCustomPropertyFieldData('ViewCount', $object, 'Integer'));
// CUSTOM END
$this->getService()->addDocument($doc);
return $doc;
}
function getFieldDefinitions() {
$xml = parent::getFieldDefinitions();
$stored = (Director::isDev()) ? "stored='true'" : "stored='false'";
$xml .= "\n\n\t\t<!-- Additional custom fields for sorting, see PageSolrIndex.php -->";
$xml .= "\n\t\t<field name='Title' type='alphaOnlySort' indexed='true' stored='true' />";
$xml .= "\n\t\t<field name='LastEdited' type='tdate' indexed='true' stored='true' />";
$xml .= "\n\t\t<field name='ViewCount' type='tint' indexed='true' stored='true' />";
$xml .= "\n\n\t\t<!-- Additional custom fields for spell checking, see PageSolrIndex.php -->";
$xml .= "\n\t\t<field name='spellcheckData' type='textSpell' $stored indexed='true' multiValued='true' />";
$xml .= "\n\t\t<field name='highlightData' type='htmltext' stored='true' indexed='true' multiValued='true' />";
return $xml;
}
/**
* Return a fake field definition (mainly to stay DRY).
*/
protected function getCustomPropertyFieldData($name, $object, $type = null, $customProperty = null) {
if(!$type) $type = 'Varchar';
return array(
'name' => $name,
'field' => $name,
'fullfield' => $name,
'base' => get_class($object),
'origin' => get_class($object),
'class' => get_class($object),
'lookup_chain' => array(
array('call' => 'property', 'property' => $customProperty ? $customProperty : $name)
),
'type' => $type,
'multi_valued' => false,
'extra_options' => array()
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment