Forums / General / sorting search results 3.3.5

sorting search results 3.3.5

Author Message

Kenneth Colwell

Friday 21 May 2004 8:15:14 am

I'm sure this is caused by my own errors but I can't seem to the get the search results to sort. I've overridden advancesearch.tpl and have changed the fetch to include sort_by and emptied the cache. No matter how I change the sort_by to nothing changes in my results. There seems to be some default to have the results oldest to newest.

I've tried: sort_by,array('modified',true()), sort_by,array('modified',false()),sort_by,array('name',true()),

Does anyone see what I'm missing?

Thanks,
Ken Colwell

{set search=fetch(content,search,
hash(text,$search_text,
section_id,$search_section_id,
subtree_array,$search_sub_tree,
sort_by,array('modified',true()),
class_id,$search_contentclass_id,
class_attribute_id,$search_contentclass_attribute_id,
offset,$view_parameters.offset,
limit,$page_limit))}

Kenneth Colwell

Friday 21 May 2004 12:27:01 pm

I found the answer to my question while looking in site.ini for something else. Change SearchViewHandling to template otherwise it just ignores you sort request.

SearchViewHandling=template

Of course, a comment in the search templates would have been nice to point me in the right direction to change this setting.

Thanks,
Ken Colwell

# How the search and advanced search page works,
# by default the search view does the search but it's
# possible to do the search in pure template code.
# Possible values are:
# default - The view code does the search and passes result to template
# template - The template does the search and passes the result back to the view code
SearchViewHandling=template
# Whether to log search stats or not

Hardy Pottinger

Thursday 09 September 2004 1:30:05 pm

This is a great hint, but it doesn't go nearly far enough. Does anyone out there have anything written up about how to actually do what the remarks in site.ini say I can do?

# How the search and advanced search page works,
# by default the search view does the search but it's
# possible to do the search in pure template code.
# Possible values are:
# default - The view code does the search and passes result to template
# template - The template does the search and passes the result back to the view code

If I change from default to template, it appears that my sort_by stuff is trying to work, but I don't see any search results.

I know that eventually, through 2-3 days of trial and error, I'll finally divine what it is I'm supposed to be doing here... but I'm not having any fun, and the pile in the inbox is getting dangerously tall...

** begin rant about documentation here **

I don't think the documentation on searching can get much poorer... check out this page:

http://www.ez.no/ez_publish/documentation/reference/data_fetching/content/search

Next to the sort_by parameter, we see "Sort the result. See description below." There's <i>nothing</i> below.

I know that better documentation was the chief bullet point to come out of the latest conference... are we there yet? Can I help? Where do I sign up?

** end rant **

I've just spent a couple days upgrading to version 3.4.1, so this is the latest stable code base, if that matters at all.

Hardy Pottinger

Friday 10 September 2004 9:55:23 am

Instead of spending days trying to figure out how to rebuild the advanced search functionality entirely in template code, without any documentation, I've opted to alter the kernel/content/advancedsearch.php file to work with the sortby option. Here's my patch (if there's a better place for this, please reply to this message and I'll upload it wherever):

Index: advancedsearch.php

===================================================================

--- advancedsearch.php	(revision 91)

+++ advancedsearch.php	(working copy)

@@ -172,6 +172,19 @@

     }
 }
 
+$sortArray = array();
+if ( $http->hasVariable( 'SortArray' ) )
+{
+    if ( is_array( $http->variable( 'SortArray' ) ) )
+    {
+        $sortArray =& $http->variable( 'SortArray' );
+    }
+    else
+    {
+        $sortArray = array( $http->variable( 'SortArray' ) );
+    }
+}
+
 $Module->setTitle( "Search for: $searchText" );
 
 $classArray =& eZContentClass::fetchList();
@@ -189,7 +202,8 @@

                                                            'SearchDate' => $searchDate,
                                                            'SearchTimestamp' => $searchTimestamp,
                                                            'SearchLimit' => $pageLimit,
-                                                           'SearchOffset' => $Offset ),
+                                                           'SearchOffset' => $Offset,
+                                                           'SortArray' => $sortArray ),
                                        $searchArray );
     if ( strlen(trim($searchText)) == 0 && count( $searchArray ) > 0  )
     {
@@ -207,6 +221,7 @@

 $tpl->setVariable( 'search_date', $searchDate );
 $tpl->setVariable( 'search_timestamp', $searchTimestamp );
 $tpl->setVariable( 'search_sub_tree', $subTreeArray );
+$tpl->setVariable( 'search_sort', $sortArray );
 $tpl->setVariable( 'search_text', $searchText );
 $tpl->setVariable( 'search_page_limit', $searchPageLimit );
 $tpl->setVariable( 'full_search_text', $fullSearchText );

With that patch in place, you can then alter your content_advancedsearch.tpl override file thusly:

    {set search=fetch(content,search,
                      hash(text,$search_text,
                           section_id,$search_section_id,
                           subtree_array,$search_sub_tree,
                           class_id,$search_contentclass_id,
                           class_attribute_id,$search_contentclass_attribute_id,
                           offset,$view_parameters.offset,
                           limit,$page_limit,
                           sort_by,$search_sort))}
...

<input type="hidden" name="SortArray[]" value="name">
<input type="hidden" name="SortArray[]" value="1">

with the "sort_by,$search_sort" part of the fetch, and the two hidden SortArray[] fields controlling the show. You can even make this a user-controllable option, with a little more work.

This is working for me in 3.4.1, it might work for you in another version, possibly even an earlier one. Good luck!