Jump to:

23478 Posts in 18950 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Search - Customisation and filtering

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 763 Views
  • MarkBr
    Avatar
    Community Member
    4 Posts

    Search - Customisation and filtering Link to this post

    Hi

    I'm using 2.4.4 and implementing a search based on the tutorial 4 - Site search one (http://doc.silverstripe.org/sapphire/en/tutorials/4-site-search.

    I want to filter out all images, files and folders from the search results. This bit works fine but causes problems when displaying these results using pagination. I'm using $Results.TotalItems in the ss file and this returns the total number of results INCLUDING the filtered out ones. For example, might get 23 results with 6 "File"s - pagination shows 3 pages - first page might have 8 results, second page 9 results, third page no results. How would I filter and then display results on the entire set rather than what is currently happening - filtering by page?

    An aside: How do I change/set the number of results returned for each page? Would this help solve the issue?

    I also want to restrict search terms to 4 characters or above - hence the line $searchQueryLength = strlen($searchQuery);
    How would I do this?

    (I know mySQL defaults to indexing 4 chars but my ISP won't decrease ft_min_word_len so searches of less than 4 chars return inconsistent results.)

    Code is below:

    function results($data, $form){
       $searchQuery = $form->getSearchQuery();
       $searchQueryLength = strlen($searchQuery);

       $results = $form->getResults();
                
       /* Filters out results not to be displayed */
       foreach($results AS $result) {
       if($result->ClassName == "Image") $results->remove($result);
          if($result->ClassName == "File") $results->remove($result);
          if($result->ClassName == "Folder") $results->remove($result);
       }

       $data = array(
          'Results' => $results,
       'Query' => $form->getSearchQuery(),
       'Title' => 'Search Results'
       );
       $this->Query = $form->getSearchQuery();

       return $this->customise($data)->renderWith(array('Search_results', 'Page'));
    }

    Hoping someone can point me in the right direction.

    Thank you in advance, Mark

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Search - Customisation and filtering Link to this post

    Hi Mark,

    Welcome to the forums!

    I advise you to upgrade and pay attention to the first item...

    http://doc.silverstripe.org/sapphire/en/trunk/changelogs/2.4.6#upgrading-notes

    (or just apply the first item to your existing config)

    Barry

  • MarkBr
    Avatar
    Community Member
    4 Posts

    Re: Search - Customisation and filtering Link to this post

    Hi Barry

    Thanks for the welcome and such a fast response!

    I'll definitely upgrade first thing on Monday morning (unfortunately can't get to install til then). I'm sure that will resolve the filtering issue.

    Any idea on limiting the search term to 4 characters or more? I can get the search term length from $searchQueryLength = strlen($searchQuery); but not sure how to return 0 results back to the page?

    Sorry - pretty new to this - and haven't done a lot that's not out-of-the-box.

    Thanks, Mark

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Search - Customisation and filtering Link to this post

    How about this...

    function results($data, $form){
       ...
       if (RETURN NO RESULTS) {
          $data = array(
             'Results' => new DataObjectSet(),
             'Query' => $form->getSearchQuery(),
             'Title' => 'Search Results'
          );
       }
       else {
          $data = array(
             'Results' => $form->getResults(),
             'Query' => $form->getSearchQuery(),
             'Title' => 'Search Results'
          );
       }
       return $this->customise($data)->renderWith(array('Search_results', 'Page'));
    }

  • MarkBr
    Avatar
    Community Member
    4 Posts

    Re: Search - Customisation and filtering Link to this post

    Thanks again Barry

    I've updated to 2.4.7.
    Adding the line

    FulltextSearchable::enable(array('SiteTree'));

    into mysite/_config.php didn't work. However, I added in

    $form->classesToSearch(array('SiteTree'));

    to the results function within page.php and that did the trick.

    The code for sending an empty DataObjectSet back to the results page when the search has 3 or less characters worked perfectly - thank you very much.

    I have one final search customisation I'm struggling with! Because of the restriction to 4 or above characters in the search query, and the fact that a number of potential search queries for our site could well be 2 or 3 characters, I want to be able to change the search query depending on what it is. I know it's not great user experience, but there'll only be a small, select number of these and in some cases I can sort of justify it! In the results function I have:

    if ($searchQuery == 'ic') {
       $searchQuery = 'information centre';
    }


    But this doesn't seem to change the search term and returns results for 'ic' rather than 'information centre'. Is getResults() in a latter line

    $results = $form->getResults();

    over-riding the change made to $searchQuery and taking the physical text within the search box?

    Any pointers would be greatly appreciated.

    Thanks, Mark

    763 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.