nyeholt / silverstripe-solr

SilverStripe and Solr integration ssaumodule
BSD 3-Clause "New" or "Revised" License
19 stars 18 forks source link

Unindex VirtualPage #24

Closed blpraveen closed 10 years ago

blpraveen commented 10 years ago

Hi Marcus, Currently when re-indexing it indexes all the pages including Virtual Page. Which creates duplicates on the search result page.

I added this extension to uncheck ShowInSearch on VirtualPage whenever page is created.

  class ExtVirtualPage extends DataExtension {

     public function onBeforeWrite() {
       parent::onBeforeWrite();
       $this->owner->ShowInSearch = false;
     }
  }

There no option to unindex all Virtual Pages or Reindex excluding the Virtual Page.

nyeholt commented 10 years ago

Try toggling the ShowInSearch field before calling parent::onBeforeWrite. The SolrIndexable extension detects a change to that field and will unindex anything that has that set to false

    if ($this->owner->isChanged('ShowInSearch') && !$this->owner->ShowInSearch) {
        $this->searchService->unindex($this->owner);
    }

This extension call is triggered by the call made to parent::onBeforeWrite in the code you've pasted above, meaning that it happens before you're setting the flag to false.

blpraveen commented 10 years ago

Any new pages created will automatically unindex. How Do I unindex existing Virtual pages? By manually toggling all the pages. It is cumbersome to carry out this way. There are around 10000 page and I need to look for once virtual pages .

Do we need to build a task to unindex or is there any other way to undex all virtual pages There is no function unindexMultiple also.

What I am looking here is Specify Allowed_Search in each pages which need to reindex and index only those pages

Other way around is running a controller action to unindex all existing virtual pages

config.php

Director::addRules(100, array(
    'utility' => 'UpgradeUtilController'
));

controller

class UpgradeUtilController extends Controller
{
    static $allowed_actions = array(
        'unidex' => 'ADMIN',
    );

    function unidex(SS_HTTPRequest $request) {
        $page_type = $request->getvar('pagetype');
        if(ClassInfo::exists($page_type)) {
            $Pages = $page_type::get()->filter('ShowInSearch' , true);
            if($Pages) {
                foreach($Pages as $Page) {
                     try {
                        $Page->ShowInSearch  = false;
                        $Page->write();
                        $Page->publish('Stage', 'Live');
                        singleton('SolrSearchService')->unindex($Page);
                        echo "<p>Unset ShowInSearch: $Page->ID and indexed</p>";

                     } catch(Exception $e) { 
                        print_r($e->getMessage());  
                        print_r('<br/> Error: ');
                        print_r('<br/> PageID: ' . $Page->ID);
                        print_r('<br/>');

                     }
                }
            }
        }

    }

}