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.

General Questions /

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

Search for certain pages only


Reply


4 Posts   1035 Views

Avatar
prawnstar

Community Member, 127 Posts

24 July 2010 at 12:43pm

Edited: 24/07/2010 12:46pm

I'm following the [url=http://doc.silverstripe.org/tutorial:4-site-search]tutorial:4[/url] tutorial for enabling search.

Is it possible to limit searches to certain pages or even page types?

I want to create a very simple "blog" style page for recipes, but only search the recipes "blog."

Ideally, I'd like to set up other pages just like it, and have the search only search their respective blog items.

Thanks!
Chris.b

Avatar
swaiba

Forum Moderator, 1804 Posts

25 July 2010 at 2:01am

How about changing the Page.php function (with appropriate SOMETHING's :)

function results($data, $form){
   $dosResults = $form->getResults();
   
   $dosNewResults = new DataObjectSet();
   foreach ($dosResults as $doResult)
   {
      if ($doResult->Title == "SOMETHING"
         || $doResult->URLSegment == "SOMETHING"
         || $doResult->Name == "SOMETHING")
      {
         //filtered
      }
      else
      {
         //keep
         $dosNewResults->push($doResult);
      }
   }

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

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

Barry

Avatar
prawnstar

Community Member, 127 Posts

26 July 2010 at 8:53am

Edited: 26/07/2010 10:24am

Thanks for that Barry! That's what I'm looking for, but I don't think I understand the URLSegment part well enough to make my query work properly.

To test is I set up two pages that had "Salad" in their title. One page inside the /recipes section and one outside. With the query below I'm returning both, but I only want the one inside the /recipes section. I was able to get mixed results filtering by Title and Name, but URLSegment is definitely what I want to filter by.

Actually...What would be preferable would be something like this:
$doResult->URLSegment == "$TheCurrentSegment"

where "TheCurrentSegment" is the section I'm searching in (ie. the recipes section).

Any idea?

// search
   function results($data, $form) {
      $dosResults = $form->getResults();
      $dosNewResults = new DataObjectSet();
      foreach ($dosResults as $doResult) {
         if ($doResult->URLSegment == "recipes")
         {
            // filtered
         } else {
            // unfiltered
            $dosNewResults->push($doResult);
         }
      }
      $data = array(
         'Results' => $dosNewResults,
         'Query' => $form->getSearchQuery(),
         'Title' => 'Search Results'
      );
      $this->Query = $form->getSearchQuery();
      return $this->customise($data)->renderWith(array('ArticleSearchResults', 'ArticleHolder'));
}

Avatar
prawnstar

Community Member, 127 Posts

26 July 2010 at 10:16am

Edited: 26/07/2010 11:21am

I just tried a slightly different approach using [url=http://doc.silverstripe.org/recipes:removingassetsfromsearch]this tutorial[/url]. But I get the same results. Apparently I fail at URLSegment. Any suggestions are greatly appreciated.

Using this function I should be removing the result found in mysite.com/otherpage/the_recipe, but it's not removing. I'm getting both results from the /recipes and /otherpage sections.

   function results($data, $form) {
      $data = array(
         'Results' => $form->getResults(),
         'Query' => $form->getSearchQuery(),
         'Title' => 'Search Results'
      );
      foreach($data["Results"] as $page) {
         if($page->URLSegment == "otherpage") {
            $data["Results"]->remove($page);
         }
      }
      $this->Query = $form->getSearchQuery();
      return $this->customise($data)->renderWith(array('ArticleSearchResults', 'ArticleHolder'));
   }

EDIT: So I now realize that URLSegment is returning the actual recipe page URLSegment. So that's why it's not filtering the page. So what I want would look like this, except this obviously doesn't work.

if($page->ParentURLSegment != ThisParentURLSegment)