Documentation

BeforeIndex

handler function(
	array arFields
);

"BeforeIndex" event is called prior to indexing of an element by the CSearch::Index method.

Parameters

Parameter Description
arFields Array of the following content:
  • MODULE_ID - module ID (not modified);
  • ITEM_ID - element ID (not modified));
  • PARAM1 - first element parameter;
  • PARAM2 - second element parameter;
  • DATE_FROM - date when element's activity started;
  • DATE_TO - date when element's activity finished;
  • TITLE - title;
  • BODY - contents;
  • TAGS - element tags;
  • SITE_ID - array of sites;
  • PERMISSIONS - array of user group IDs permitted to be read;
  • URL - address relative site root, via which this element is available;

This handler can modify fields of the arFields parameter and must return it as the result.

Important

  • If SITE_ID field is modified in any way within the handler, it's important to remember that indexes in the array must start from zero and with a sequence order. Otherwise the array will be deemed as associative and the elements will associate with sites incorrectly (digits will replace SITE_ID and elements will disappear from the search).

  • To avoid adding an index entry (for example, if a subsection of information block must not be indexed), the following must be executed in the handler function:
    unset($arFields["BODY"]);
    unset($arFields["TITLE"]);
    
  • If the following types of checks are executed:
    $bTitle = array_key_exists("TITLE", $arFields);
    $bBody = array_key_exists("BODY", $arFields);
    if($bTitle && $bBody && strlen($arFields["BODY"])<=0 && strlen($arFields["TITLE"])<=0)
    
    then to exclude an element from the index, the following must be executed:
    $arFields["BODY"]='';
    $arFields["TITLE"]='';
    

See Also

Example of handler function:

<?
// file /bitrix/php_interface/init.php
// register the handler
AddEventHandler("search", "BeforeIndex", Array("MyClass", "BeforeIndexHandler"));
class MyClass { // create event handler "BeforeIndex" function BeforeIndexHandler($arFields) { if($arFields["MODULE_ID"] == "iblock" && $arFields["PARAM2"] == 33) { if(array_key_exists("BODY", $arFields)) { $arFields["BODY"] .= " the hottest news"; } } return $arFields; } } ?>

Example of handler function use:

Example of using handler function, for the search.title component to execute search not only by titles, but by some property:

// register the handler
AddEventHandler("search", "BeforeIndex", "BeforeIndexHandler");
 // create event handler "BeforeIndex"
public static function BeforeIndexHandler($arFields)
{
   if(!CModule::IncludeModule("iblock")) // connect the module
      return $arFields;
   if($arFields["MODULE_ID"] == "iblock")
   {
      $db_props = CIBlockElement::GetProperty(                        // Request properties of indexed element
                                    $arFields["PARAM2"],         // BLOCK_ID of indexed property
                                    $arFields["ITEM_ID"],          // ID of indexed property
                                    array("sort" => "asc"),       // Sorting (can be skipped)
                                    Array("CODE"=>"CML2_ARTICLE")); // property CODE (in this case, article)
      if($ar_props = $db_props->Fetch())
         $arFields["TITLE"] .= " ".$ar_props["VALUE"];   // add a property to the end of a indexed element title
   }
   return $arFields; // return the implemented modifications
}


© «Bitrix24», 2001-2024
Up