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 » Forums » Developer » eZFind sort on string field
expandshrink

eZFind sort on string field

eZFind sort on string field

Monday 14 October 2013 5:42:20 pm - 5 replies

Hello everyone.
I have a problem with sorting in eZFind.
Sorting on attributes of type string do anything.

["SortBy"]=>  array(1) {      ["attr_nom_s"]=>     string(3) "asc"   }

  1. L'ILE D'YEU
  2. NOTRE DAME DE RIEZ
  3. SAINT HILAIRE DE RIEZ
  4. SAINT ETIENNE DU BOIS
  5. LE PERRIER
  6. SAINT CHRISTOPHE DU LIGNERON 
  7. SAINT GEORGES DE POINTINDOUX
  8. PISSOTTE
  9. CHALLANS
  10. AUBIGNY

If I index a sub-attribute 'subattr_nom_s' with value string what I replace all space by underscrore (' _ ') I do not have this problem. But I doubt that will be the right solution ...

 

Solr 3.1 | eZ find 2.2

Modified on Monday 14 October 2013 5:47:59 pm by Rémy PHP

Tuesday 15 October 2013 10:16:28 am

I can not find a solution.
I explore this time the track indexing a subattribute.

<span style="background-color: #ffffff; font-size: 0.8em;"><?php</span>

// @see extension/client/settings/ezfind.ini.append.php [SolrFieldMapSettings] CustomMap[ezstring]
class V_ezfSolrDocumentFieldString extends ezfSolrDocumentFieldBase
{
    public function getData()     {                 $contentClassAttribute = $this->ContentObjectAttribute->attribute( 'contentclass_attribute' );
        $return = parent::getData();                            $string = $this->ContentObjectAttribute->value();
        $fieldName = self::generateSubattributeFieldName_SORTABLE( $contentClassAttribute, 'sort', 'string' );         $return[$fieldName] = str_replace(' ', '_', $string);                            return $return;
    }
    public static function generateSubattributeFieldName_SORTABLE(eZContentClassAttribute $classAttribute, $subfieldName, $type)     {         $parent = self::generateSubattributeFieldName( $classAttribute, $subfieldName, $type );         return str_replace('____', '_', $parent);     } }
?>

Modified on Tuesday 15 October 2013 10:17:25 am by Rémy PHP

Wednesday 16 October 2013 2:15:35 pm

Hello Rémy

How do you do your ezfind search ? In the template or in PHP code ?Can we see your code to understand ?

Are you sure that attr_nom_s is really indexed in SOLR ? Your can access your admin SOLR via http://wwwyoursite.com:8983/solr/admin

Let us know

Thursday 17 October 2013 12:34:48 pm

Hello Solène,

 

My understanding of solr is improved.
My research is done in PHP

$sort_by = array( 'attr_nom_s' => 'desc' );
$fetch_parameters = array('sort_by' => $sort_by, );
$result = eZFunctionHandler::execute('ezfind', 'search', $fetch_parameters);

I have access to my adminsitration interface Solr.
And my fields are indexed.
I discovered that the sorts of fields in "* _t" is based on all fields of the term.

Examples:
If I start my research on this dataset:
<result name="response" numFound="*" start="0">
<doc><str name="attr_nom_t">aa zz</str></doc>
<doc><str name="attr_nom_t">za ab</str></doc>
</result>

The result will be:
# aa zz
# za ab

Because the word "zz" and the greatest ones in the alphabet.
The sort on a field "* _t" does not correspond to what I'm looking.
So I turn to the sorts of attributes in "_s".

But this sort is case sencitive ..
On top of that my data is not very clean and contain unnecessary spaces.
And "  zz" is found before "aa".

Modified on Thursday 17 October 2013 12:35:22 pm by Rémy PHP

Thursday 17 October 2013 3:06:37 pm

Here is my code to index a sortable sub-attribut

 

<?php
/**
 * Indexation des subattr_ pour les string.
 *
 *
 * @see extension/client/settings/ezfind.ini.append.php [SolrFieldMapSettings] CustomMap[ezstring]
 * @see http://share.ez.no/learn/ez-publi...g-additional-fields-in-solr/(page)/3
 */
class V_ezfSolrDocumentFieldString extends ezfSolrDocumentFieldBase
{
   
    /**
     * return the array keys (fieldname => value), like : array('attr_mydate_dt' => '2010-04-30T00:00:00Z')
     * @see ezfSolrDocumentFieldBase::getData()
     */
    public function getData()
    {       
        $contentClassAttribute = $this->ContentObjectAttribute->attribute( 'contentclass_attribute' );
        //$contentClassAttribute instanceof eZContentClassAttribute;
       
        $return = parent::getData();
       
        $string = $this->ContentObjectAttribute->value();
        $sort_string = V_ezfSolrDocumentFieldString::cleanSortString($string);
       
        $fieldName = self::generateSubattributeFieldName_SORTABLE( $contentClassAttribute, 'sort', 'string' );
        $return[$fieldName] = $sort_string;
        $fieldName = self::generateSubattributeFieldName_SORTABLE( $contentClassAttribute, 'sort', 'text' );
        $return[$fieldName] = $sort_string;
       
        SLogger::v($return, __METHOD__);
       
        return $return;
    }
   
    /**
     * trim, accents, upper case, etc
     * @param string $string
     * @return string
     */
    public static function cleanSortString($string)
    {
        $sort_string = str_replace(array(' ', "'", ','), '_', trim($string));
       
        http://www.weirdog.com/blog/php/s...ccents-des-caracteres-accentues.html
        $sort_string = htmlentities($sort_string, ENT_NOQUOTES, 'utf-8');
        $sort_string = preg_replace('#&([A-za-z])(?:acute|cedil|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $sort_string);
        $sort_string = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $sort_string); // pour les ligatures e.g. '&oelig;'
        $sort_string = preg_replace('#&[^;]+;#', '', $sort_string); // supprime les autres caractères
       
        $sort_string = mb_strtoupper($sort_string);
        return $sort_string;
    }
    /**
     * ezfSolrDocumentFieldBase::generateSubattributeFieldName() génére des noms de sub-attributs finissant par ____*.
     * Or ces noms indiquent des type Multivalued. Et on ne peut pas trier sur un Multivalued.
     * Cette fonction remplace donc les 4 '_' par un seul pour avoir des types _*.
     *
     * @param eZContentClassAttribute $classAttribute
     * @param string $subfieldName
     * @param string $type
     * @see ezfSolrDocumentFieldBase::generateSubattributeFieldName()
     */
    public static function generateSubattributeFieldName_SORTABLE(eZContentClassAttribute $classAttribute, $subfieldName, $type)
    {
        $parent = self::generateSubattributeFieldName( $classAttribute, $subfieldName, $type );
        return str_replace('____', '_', $parent);
    }
}
?>

Thursday 17 October 2013 3:08:38 pm

I know this is not the right solution but I must deliver it to the last week.
I still listen to your suggestions and comments.

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from