Skip to content
Permalink
Browse files

Implement optional indexing of parent tags

  • Loading branch information...
emodric committed Oct 1, 2012
1 parent 64c310d commit b8213f5071c0f6288f3ed4a9815e0191f5be4fe0
Showing with 63 additions and 26 deletions.
  1. +59 −26 classes/ezfsolrdocumentfieldeztags.php
  2. +4 −0 settings/eztags.ini
@@ -6,6 +6,13 @@
*/
class ezfSolrDocumentFieldeZTags extends ezfSolrDocumentFieldBase
{
/**
* If true, synonyms will be indexed, otherwise, main tag will be indexed instead
*
* @var bool
*/
private $indexSynonyms = true;
/**
* Returns the data from content object attribute which is sent to Solr backend
*
@@ -16,8 +23,7 @@ public function getData()
$data = array();
/** @var eZContentClassAttribute $contentClassAttribute */
$contentObjectAttribute = $this->ContentObjectAttribute;
$contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
$contentClassAttribute = $this->ContentObjectAttribute->contentClassAttribute();
$keywordFieldName = parent::generateAttributeFieldName( $contentClassAttribute, 'lckeyword' );
$textFieldName = parent::generateAttributeFieldName( $contentClassAttribute, 'text' );
@@ -27,43 +33,27 @@ public function getData()
$data[$textFieldName] = '';
$data[$tagIDsFieldName] = array();
if ( !$contentObjectAttribute->hasContent() )
if ( !$this->ContentObjectAttribute->hasContent() )
return $data;
/** @var eZTags $objectAttributeContent */
$objectAttributeContent = $contentObjectAttribute->content();
$objectAttributeContent = $this->ContentObjectAttribute->content();
if ( !$objectAttributeContent instanceof eZTags )
return $data;
$tagIDs = array();
$keywords = array();
$indexSynonyms = eZINI::instance( 'eztags.ini' )->variable( 'SearchSettings', 'IndexSynonyms' ) === 'enabled';
$this->indexSynonyms = eZINI::instance( 'eztags.ini' )->variable( 'SearchSettings', 'IndexSynonyms' ) === 'enabled';
$indexParentTags = eZINI::instance( 'eztags.ini' )->variable( 'SearchSettings', 'IndexParentTags' ) === 'enabled';
$tags = $objectAttributeContent->attribute( 'tags' );
if ( is_array( $tags ) )
{
/** @var eZTagsObject $tag */
foreach ( $tags as $tag )
{
if ( !$indexSynonyms && $tag->isSynonym() )
$tag = $tag->getMainTag();
if ( $tag instanceof eZTagsObject )
{
//get keyword in content's locale
$keyword = $tag->getKeyword( $contentObjectAttribute->attribute( 'language_code' ) );
if ( !$keyword )
{
//fall back to main language
/** @var eZContentLanguage $mainLanguage */
$mainLanguage = eZContentLanguage::fetch( $tag->attribute( 'main_language_id') );
if ( $mainLanguage instanceof eZContentLanguage )
$keyword = $tag->getKeyword( $mainLanguage->attribute( 'locale' ) );
}
if ( $keyword )
{
$tagIDs[] = (int) $tag->attribute( 'id' );
$keywords[] = $keyword;
}
$this->processTag( $tag, $tagIDs, $keywords, $indexParentTags );
}
}
}
@@ -106,4 +96,47 @@ static public function getFieldNameList( eZContentClassAttribute $classAttribute
return $fieldsList;
}
/**
* Extracts data to be indexed from the tag
*
* @param eZTagsObject $tag
* @param array $tagIDs
* @param array $keywords
* @param bool $indexParentTags
*/
private function processTag( eZTagsObject $tag, array &$tagIDs, array &$keywords, $indexParentTags = false )
{
if ( !$this->indexSynonyms && $tag->isSynonym() )
$tag = $tag->getMainTag();
//get keyword in content's locale
$keyword = $tag->getKeyword( $this->ContentObjectAttribute->attribute( 'language_code' ) );
if ( !$keyword )
{
//fall back to main language
/** @var eZContentLanguage $mainLanguage */
$mainLanguage = eZContentLanguage::fetch( $tag->attribute( 'main_language_id') );
if ( $mainLanguage instanceof eZContentLanguage )
$keyword = $tag->getKeyword( $mainLanguage->attribute( 'locale' ) );
}
if ( $keyword )
{
$tagIDs[] = (int) $tag->attribute( 'id' );
$keywords[] = $keyword;
}
if ( $indexParentTags )
{
$parentTags = $tag->getPath( true );
foreach ( $parentTags as $parentTag )
{
if ( $parentTag instanceof eZTagsObject )
{
$this->processTag( $parentTag, $tagIDs, $keywords );
}
}
}
}
}
@@ -60,3 +60,7 @@ ReindexWhenDelayedIndexingDisabled=disabled
# When disabled, synonym tags are replaced by their main tag in attribute metadata
# Useful for faceting with ezfind
IndexSynonyms=enabled

# When enabled, parent tags of a tag will also be stored in the index
# Useful for faceting with ezfind
IndexParentTags=disabled

0 comments on commit b8213f5

Please sign in to comment.
You can’t perform that action at this time.