ez projects / ezstarrating / forum / general / use ezfind 2.1 capability f...

Use ezfind 2.1 capability for indexing average score

You need to be logged in to post messages in the forums. New users may register here.

Gaetano Giunta

Member since:
30 November 1999

Posts: 269

Wednesday 18 November 2009 6:16:15 pm

Current data indexed is the number of votes.

This means using ezfind number of votes can be searched, but also used for sorting and for range queries.

Doing sorting on average score would also be very nice, as well as range queries on it - it is easily doable when taking advantage of ezfind's new subattribute-indexing capabilities

class ezfSolrDocumentFieldStarRating extends ezfSolrDocumentFieldBase


   // Q: why was the 'default subattribute' removed?


   public static $subattributesDefinition = array( self::SUBATTRIBUTE_RATING_AVERAGE => 'float',

                                                   self::SUBATTRIBUTE_RATING_COUNT => 'int' );


   const SUBATTRIBUTE_RATING_AVERAGE = 'rating_average';

   const SUBATTRIBUTE_RATING_COUNT = 'rating_count';



    * @see ezfSolrDocumentFieldBase::getData()


   public function getData()


       $data = array();


       $contentClassAttribute = $this->ContentObjectAttribute->attribute( 'contentclass_attribute' );

       $contentObjectAttribute = $this->ContentObjectAttribute->attribute( 'content' );


       // Loop through the subattributes defined upper, and get associated key in ContentObjectAttribute

       foreach ( self::getFieldNameList( $contentClassAttribute ) as $objectFieldName => $solrFieldName )


           $data[$solrFieldName] = $contentObjectAttribute->attribute( $objectFieldName );



       return $data;




    * @see ezfSolrDocumentFieldBase::getFieldNameList()


   public static function getFieldNameList( eZContentClassAttribute $classAttribute, $exclusiveTypeFilter = array() )


       // Generate the list of subfield names.

       $subfields = array();


       //  Then hanlde all other subattributes

       foreach ( self::$subattributesDefinition as $name => $type )


           if ( empty( $exclusiveTypeFilter ) or !in_array( $type, $exclusiveTypeFilter ) )


               $subfields[$name] = parent::generateSubattributeFieldName( $classAttribute, $name, $type );



       return $subfields;



Principal Consultant International Business
Member of the Community Project Board


Benjamin Kroll

Member since:
08 July 2010

Posts: 1

Thursday 08 July 2010 7:05:18 pm

Thanks for posting this Gaetano.

I've just tried using this class (as I'm interested in the average vote rating more than overall votes cast), but somehow I'm not having much luck getting results.

I'm on ezp 4.2.0 with ezFind 2.1 here.

I added the class to the ezfind/classes folder, ran ezpgenerateautoloads and then added
the following to my ezfind.ini:



The indexing process runs without errors but when I try to use the new sub-attribute as part of a facet like:

$defaultSearchFacets = array( hash('field', 'my_product/star_rating/rating_average', 'name', 'Star rating', 'limit', 5)

I'm not getting any results. I'm fairly sure I'm doing something wrong somewhere.
I've tried assigning different data types to ezsrrating in the DatatypeMap[] setting in the ini but that didn't change anything.

Any ideas where to look for clues?

Peter Keung

Member since:
15 August 2007

Posts: 1

Friday 26 November 2010 1:30:32 am

You can use subattr_rating___rating_average_f as the facet field. I'm not sure what the proper "identifier" syntax (a la "product/rating/rating_average") is though.

Also, you can map it as a float:

Mugo Web, eZ Partner in Vancouver, Canada


Bartek Tokarczyk

Member since:
06 April 2012

Posts: 5

Friday 10 January 2014 2:04:46 pm

I'm using the same class as well. And it seems to work fine, nodes are indexed properly.

I have a problem with using this subattribute as a sorting option though. If I use it for filtering it works great:

'filter', 'subattr_star_rating___rating_average____f:[2 TO 4]'

But if I try to use it for sorting I get no results

'sort_by', hash('subattr_star_rating___rating_average____f', 'asc')

Any help would be appreciated :)

Benjamin Kroll

Member since:
30 November 1999

Posts: 1

Wednesday 15 October 2014 6:24:30 am

In case someone else stumbles across this.

Sub-attribute fields are marked as 'multiValued' in Solr's schema.xml that comes with ezfind. Solr cannot sort on multiValued fields, which is why a fetch with sorting by


does not return any results. Solr throws an error citing the multiValued issue:

... can not sort on multivalued field: subattr_star_rating___rating_average____f

I'm not sure why even the 'sortable' sint, sfloat etc. field types are marked as multiValued in the schema, but as they're stored that way sorting is not possible.

Some options to get sorting when working directly with Solr:

- change the field type suffix in your ezfSolrDocumentField ... class to end with a single underscore after the sub-attribute key. So you end up with


or similar. This is stored as a single value, which will allow you to sort based on it.

- add a new dynamic sub-attribute field definition to the schema and use that when indexing your fields

- change the sub-attribute field definitions in the schema.xml, removing the 'multiValued' property from sint, sfloat etc. (USE AT YOUR OWN RISK)

David Sayre

Member since:
15 May 2009

Posts: 2

Thursday 03 September 2015 4:13:49 pm

The sort for subattr_star_rating___rating_average____f is not possible because it is a multi-valued field (as Peter describes).

I'm unsure why the attribute (attr_) code was omitted from the indexing class but here is a simple snippet to add it based on the average value calculation. This will allow sorting.

1. As mentioned before, add the "CustomMap[ezsrrating]=ezfSolrDocumentFieldStarRating" to the ezfind.ini.append.php

2. Edit esfDocumentFieldStarRating.php
line 27-ish in the getData() function just before the return $data;

// DJS @ BFRE - Add attribute during data population to permit sorting
$sAttributeName= parent::getFieldName($contentClassAttribute );
$data[$sAttributeName] = $contentObjectAttribute->attribute('rating_average');

If this is done in a custom extension then ezfind remains unmodified

You need to be logged in to post messages in the forums. New users may register here.