Path

ez.no / documentation / extensions / ez publish extensions / ez find / ez find 2.7 / customization / general index time plugin m...


Caution: This documentation is for eZ Publish legacy, from version 3.x to 5.x.
For 5.x documentation covering Platform see eZ Documentation Center, for difference between legacy and Platform see 5.x Architecture overview.

General Index Time Plugin Mechanism

General Index Time Plugin Mechanism

Audience

This feature requires deeper understanding of eZ Publish kernel (object API), experience with eZ Find and PHP programming skills.

Goals and Use Cases

In some specific situations, the indexing with custom plugins per datatype is not sufficient or even applicable, hence the need to create a new index time plugin system that is capable of inserting new fields and/or modifying the fields generated by the regular eZ Find index engine.
Use cases are for example: indexing parent node information with an object or coping with a data model implementation where relations are part of the domain model, but do not use related objects or its datatype equivalents. This is typically the case where large amounts of data are imported into eZ Publish.
Another use case is to be able to modify boost factors of individual fields or whole documents based on the values or relations contained within an eZ Publish content object. For example, boosting articles that reference certain images, or articles that appear inside special eZ Flow blocks.

How it Works

During indexing, eZ Find can be instructed to call user defined functions from classes which implement a dedicated PHP interface. This is done after the eZ Find indexer has done the bulk of the work indexing the regular data and metadata. The arguments are on one hand the current content object in the indexing process and on the other hand the eZ Solr document object --- consisting of raw fields and its corresponding values --- to be modified by the custom defined function. Not only fields can be modified or added, but also the overall document level and field level boost factors.

How to Use

The first step is to create your custom class implementing the function 'modify' as defined by the interface:

interface ezfIndexPlugin
{
   /**
    * @var eZContentObject $contentObject
    * @var array $docList
    */
   public function modify(eZContentObject $contentObject, &$docList);
}

An example is given in the following listing where, for every language version of an object, the main node parent object name is indexed in a new field:

class ezfIndexParentName implements ezfIndexPlugin
{
     /**
      * The modify method gets the current content object AND the list of
      * Solr Docs (for each available language version).
      *
      *
      * @param eZContentObject $contentObect
      * @param array $docList
      */
      public function modify(eZContentObject $contentObect, &$docList)
      {
            $contentNode = $contentObect->attribute('main_node');
            $parentNode = $contentNode->attribute('parent');
            if ($parentNode instanceof eZContentObjectTreeNode)
            {
                  $parentObject = $parentNode->attribute('object');
                  $parentVersion = $parentObject->currentVersion();
                  $availableLanguages = $parentVersion->translationList( false, false );
                  foreach ($availableLanguages as $languageCode)
                  {
                          $docList[$languageCode]->addField('extra_parent_node_name_t',  $parentObject->name( false, $languageCode ) );
                  }
            }
       }
}

In order to activate your plugin, you need to add your PHP class name to the eZ Find ezfind.ini settings in the IndexPlugins section, for example:

 [IndexPlugins]
# Allow injection of custom fields and manipulation of fields/boost parameters
# at index time
# This can be defined at the class level or general
General[]
#General[]=ezfIndexParentName
#Classhooks will only be called for objects of the specified class
Class[]
Class[myspecialclass]=ezfIndexParentName

This will enable the PHP class ezfIndexParentName for the myspecialclass eZ Publish class of objects.

Andrea Melo (16/05/2012 2:09 pm)

Andrea Melo (16/05/2012 2:09 pm)


Comments

There are no comments.