Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Search - Customisation and filtering


Go to End


5 Posts   1982 Views

Avatar
MarkBr

Community Member, 4 Posts

17 March 2012 at 4:41am

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

Avatar
swaiba

Forum Moderator, 1899 Posts

17 March 2012 at 5:43am

Edited: 17/03/2012 5:44am

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

Avatar
MarkBr

Community Member, 4 Posts

17 March 2012 at 6:28am

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

Avatar
swaiba

Forum Moderator, 1899 Posts

17 March 2012 at 11:38pm

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')); 
}

Avatar
MarkBr

Community Member, 4 Posts

20 March 2012 at 4:56am

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