3212 Posts in 847 Topics by 809 members
| Go to End | Next > | |
| Author | Topic: | 2545 Views |
-
Search result page: use MetaDescription as description (just like Google Search)

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?
-
Re: Search result page: use MetaDescription as description (just like Google Search)

15 January 2010 at 5:06pm Last edited: 15 January 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.
-
Re: Search result page: use MetaDescription as description (just like Google Search)

15 January 2010 at 9:14pm
Thanks, this seems to be the code I was searching for.
-
Re: Search result page: use MetaDescription as description (just like Google Search)

16 January 2010 at 2:02am Last edited: 16 January 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 "{$Title}"">Read more about "{$Title}"...</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.
-
Re: Search result page: use MetaDescription as description (just like Google Search)

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
-
Re: Search result page: use MetaDescription as description (just like Google Search)

16 January 2010 at 2:37am Last edited: 16 January 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..
-
Re: Search result page: use MetaDescription as description (just like Google Search)

16 January 2010 at 2:52am
You are the man!
-
Re: Search result page: use MetaDescription as description (just like Google Search)

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
| 2545 Views | ||
| Go to Top | Next > |




