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.

Template Questions /

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

Search result page: use MetaDescription as description (just like Google Search)


Go to End


10 Posts   6337 Views

Avatar
karlevid

Community Member, 10 Posts

15 January 2010 at 2:52am

For my search result page I would like to use the MetaDescription (just like Google Search) instead of example $Content.LimitWordCountXML, this is used in the tutorial (http://doc.silverstripe.org/doku.php?id=tutorial:4-site-search). I found out there are some possilbities, http://doc.silverstripe.org/doku.php?id=text but no MetaDescription in there.

Any ideas?

Avatar
CodeGuerrilla

Community Member, 105 Posts

15 January 2010 at 5:06pm

Edited: 15/01/2010 5:15pm

SilverStripes out of the box search is pretty basic and only searches the Content and page Titles etc..

What we do is create a normal form in the template and point its action at a new Page Types action eg:

class SearchPage extends Page {

...

}

class SearchPage_Controller extends Page_Controller {

public $searchEntry = "";
	public $resultsSize = 0;
	
	public function init() {
		parent::init();
		
		if(isset($_GET["browseall"]))
		{
			unset($_SESSION["search_input"]);
		}
		
		
		if(isset($_POST["search_input"]) && $_POST["search_input"] != 'Search Products') {
			$this->searchEntry = filter_var($_POST["search_input"], FILTER_SANITIZE_STRING);
			$_SESSION["search_input"] = $this->searchEntry;
		}
		else if (isset($_SESSION["search_input"])) 
		{
			$this->searchEntry = $_SESSION["search_input"];
		}
		
	}
                      
    public function doSearch()
    {
                if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) 
			{
				$_GET['start'] = 0;
			}
  			$SQL_start = (int)$_GET['start'];

			$results = DataObject::get('Product', 
			 	"Title LIKE '%".$this->searchEntry."%' ".
				"OR Brand LIKE '%".$this->searchEntry."%' ".
				"OR PartNumber LIKE '%".$this->searchEntry."%' ".
				"OR MetaTitle LIKE '%".$this->searchEntry."%' ".
				"OR MetaDescription LIKE '%".$this->searchEntry."%' ".
				"OR FIND_IN_SET('".trim($this->searchEntry)."', `MetaKeywords`) > 0", "Title ASC", NULL, "{$SQL_start},".Page::$resultsPerPage);
				
			
		    $title = empty($this->searchEntry)?'Search Results':'Search results for: '.$this->searchEntry;
			
			if ($results)
			{
				$this->resultsSize = $results->Count();
			}
			
			$data = array(
			 'Results' => $results,
			 'Query' => !isset($_GET['q'])?$this->searchEntry:$_GET['q'],
			 'Title' => $title
		  );

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

}

What we are doing here is calling '<searchpage-urlsegment>/doSearch' and searching the products but also MetaDescription and MetaKeywords you could modify this to search Page Content, Title instead fairly easily.

Avatar
karlevid

Community Member, 10 Posts

15 January 2010 at 9:14pm

Thanks, this seems to be the code I was searching for.

Avatar
bummzack

Community Member, 904 Posts

16 January 2010 at 2:02am

Edited: 16/01/2010 2:08am

Do you want to search the MetaDescription, or just output the MetaDescription in the Search-Results (instead of the Summary)?
If the latter is the case, just use $MetaDescription instead of content?
Here's an example with modified code from: http://doc.silverstripe.org/doku.php?id=tutorial:4-site-search#showing_the_results

 <% if Results %>
	<ul id="SearchResults">
	<% control Results %>
		<li>
			<a class="searchResultHeader" href="$Link">
			<% if MenuTitle %>
				$MenuTitle
			<% else %>
				$Title
			<% end_if %>
			</a>
			<!-- instead of this: -->
			<p>$Content.LimitWordCountXML</p>
			<!-- use this: -->
			<p>$MetaDescription</p>
			<a class="readMoreLink" href="$Link" title="Read more about &quot;{$Title}&quot;">Read more about &quot;{$Title}&quot;...</a>
		</li>
	<% end_control %>
</ul>
<% else %>
	<p>Sorry, your search query did not return any results.</p>
<% end_if %>

If you want to search the MetaDescription, you'll have to implement a search like the one CodeGuerilla provided.

Avatar
karlevid

Community Member, 10 Posts

16 January 2010 at 2:10am

Thanks Banal ;) Yes, this was my idea also I used this, but this gives a empty output.

http://process-m.com/processm/suche/SearchForm?Search=axel&locale=de_DE&action_results=Search

Avatar
bummzack

Community Member, 904 Posts

16 January 2010 at 2:37am

Edited: 16/01/2010 2:37am

Ah yes. I see. The SearchForm class doesn't return the MetaDescription in the Results.
I created a subclass of the SearchForm that also returns MetaDescription (kinda sucks that I had to copy the whole method for this tiny change..)

1) Place the code from here: http://pastie.org/779513 in mysite/code/search/CustomSearch.php
2) Run dev/build
3) In your Page Controller change this line:

return new SearchForm($this, "SearchForm", $fields, $actions);

to

return new CustomSearch($this, "SearchForm", $fields, $actions);

You should then be able to access $MetaDescription in your Search Results..

Avatar
karlevid

Community Member, 10 Posts

16 January 2010 at 2:52am

You are the man!

Avatar
vancouverWill

Community Member, 121 Posts

25 August 2010 at 11:22am

Have any of you tried updating the SearchEngine function similar to what Banal did but changing it so that it looks at more tables than just site tree and file. I have tried editing but haven't been able to fix the sql bugs I'm getting. Been trying on and off all week, coming from different angles and as yet no success.

Cheers

Will

Go to Top