This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit ezplatform.com

eZ Community » Learn » eZ Publish » Advanced development with eZ Find -...

Advanced development with eZ Find - part 2 : Indexing additional fields in Solr

Tuesday 15 June 2010 2:20:10 pm

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Step 4 : Understanding the role of the getData() method

This method is invoked to extract data from eZ Publish, and prepare it prior to indexing in Solr. This method is the place to add additional fields like 'year' et 'yearmonth'. Once these new fields (subattributes) are indexed, we will leverage them by facetting or filtering on them, using the following, classical syntax:

  • 'mycontentclass/mydateattribute/year', translated in Solr under : 'subattr_date-year_dt'
  • 'mycontentclass/mydateattribute/yearmonth', translated in Solr under : 'subattr_date-yearmonth_dt'
 
public function getData()
{
  $contentClassAttribute = $this->ContentObjectAttribute->attribute( 'contentclass_attribute' );
 
  switch ( $contentClassAttribute->attribute( 'data_type_string' ) )
  {   
    case 'ezdate' :
    {
      $returnArray = array();
 
      // Get timestamp attribute value
      $value = $this->ContentObjectAttribute->metaData();
 
      // Generate the main filedName attr_XXX_dt 
      $fieldName = parent::generateAttributeFieldName( $contentClassAttribute,
      self::DEFAULT_ATTRIBUTE_TYPE );
      $returnArray[$fieldName] = parent::convertTimestampToDate( $value );
 
      // Generate the yearmonth subattribute filedName subattr_year_dt
      $fieldName = parent::generateSubattributeFieldName( $contentClassAttribute,
      'year',
      self::DEFAULT_SUBATTRIBUTE_TYPE );
 
      $year = date("Y", $value); // Get Year value : 2010
      $returnArray[$fieldName] = parent::convertTimestampToDate( strtotime($year.'-01-01') );
 
      // Generate the yearmonth subattribute filedName subattr_yearmonth_dt
      $fieldName = parent::generateSubattributeFieldName( $contentClassAttribute,
      'yearmonth',
      self::DEFAULT_SUBATTRIBUTE_TYPE ); 
 
       $month = date("n", $value); // Get Month value : 3
      $returnArray[$fieldName] = parent::convertTimestampToDate( strtotime($year.'-'.$month.'-01') );
 
      return $returnArray;
 
  } break;
 
  default:
  {} break;
  }
 }
}
 

Note:
$returnArray contains an associative array looking like this ( using var_dump ) :

array(3) {
 ["attr_date_dt"]=>
 string(24) "2008-12-28T00:00:00.000Z"
 ["subattr_date-year_dt"]=>
 string(24) "2008-01-01T00:00:00.000Z"
 ["subattr_date-yearmonth_dt"]=>
 string(24) "2008-12-01T00:00:00.000Z"
}

Note :
Solr uses the ISO 8601 date format, of the form : '2010-04-30T00:00:00Z'. The parent class ( ezfSolrDocumentFieldBase ) exposes the convertTimestampToDate() methodm used to convert a timestamp to an ISO 8601 formatted date.

 
36 542 Users on board!

Tutorial menu

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)

Proudly Developed with from