Skip to content

Commit

Permalink
Merge pull request #4 from heyday/upgrade/silverstripe4-elasticsearch5
Browse files Browse the repository at this point in the history
Upgrade/silverstripe4 elasticsearch5
  • Loading branch information
bendubuisson authored Aug 31, 2017
2 parents 2e995dd + e2c81cc commit 3fd8d9d
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 115 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ This module is a fork of [SilverStripe's Elastica Module](https://github.com/sil

## Compatibility

This release is compatible with all elasticsearch 2.x releases.
This release is compatible with all elasticsearch 5.x releases.
This release requires SilverStripe 4.x

If you need to work with an earlier version of elasticsearch, please try the 0.0.1 release of this module
If you need to work with an earlier version of elasticsearch (2.x) and SS (3.x), please try the 1.0 release of this module

## Installation

```bash
$ composer require heyday/silverstripe-elastica:~1.0
$ composer require heyday/silverstripe-elastica:~2.0
```

## Usage
Expand Down Expand Up @@ -53,7 +54,7 @@ Heyday\Elastica\ElasticaService: # Example of customising the index config on th
Only:
environment: dev
---
Injector:
SilverStripe\Core\Injector\Injector:
Elastica\Client:
constructor:
- host: localhost # hostname of the elastic search server
Expand All @@ -74,7 +75,7 @@ mysite/_config/search.yml

# PageTypes

Page:
Your\Namespace\Page:
extensions:
- Heyday\Elastica\Searchable
indexed_fields: &page_defaults
Expand All @@ -83,7 +84,7 @@ Page:
- Content
- MetaDescription

SpecialPageWithAdditionalFields:
Your\Namespace\SpecialPageWithAdditionalFields:
extensions:
- Heyday\Elastica\Searchable # only needed if this page does not extend the 'Page' configured above
indexed_fields:
Expand All @@ -92,14 +93,14 @@ SpecialPageWithAdditionalFields:
- BannerCopy
- SubHeading

SpecialPageWithRelatedDataObject:
Your\Namespace\SpecialPageWithRelatedDataObject:
extensions:
- Heyday\Elastica\Searchable
indexed_fields:
<<: *page_defaults
- RelatedDataObjects

RelatedDataObject:
Your\Namespace\RelatedDataObject:
extensions:
- Heyday\Elastica\Searchable
indexed_fields:
Expand All @@ -116,7 +117,7 @@ mysite/_config/search.yml

# PageTypes

Page:
Your\Namespace\Page:
extensions:
- Heyday\Elastica\Searchable
indexed_fields:
Expand Down Expand Up @@ -238,7 +239,7 @@ We use silverstripe-queuedjobs (https://github.com/symbiote/silverstripe-queuedj

To turn on queues, you will need the following config:
```yaml
Injector:
SilverStripe\Core\Injector\Injector:
Heyday\Elastica\Searchable:
properties:
Queued: true
Expand All @@ -252,7 +253,7 @@ Every minute to run the jobs in the queue
```
and to clean up the jobs, add the cleanup job once by running (it then gets automatically added to run once a day):
```
framework/sake dev/tasks/CreateQueuedJobTask?name=CleanupJob
framework/sake dev/tasks/CreateQueuedJobTask?name=Symbiote\QueuedJobs\Jobs\CleanupJob
```

## License
Expand Down
4 changes: 2 additions & 2 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
Name: Heyday Elastica Config
---
Injector:
SilverStripe\Core\Injector\Injector:
Heyday\Elastica\ReindexTask:
constructor:
- %$Heyday\Elastica\ElasticaService
Expand All @@ -11,5 +11,5 @@ Injector:
ManyManyList:
class: Heyday\Elastica\ManyManyList

CleanupJob:
Symbiote\QueuedJobs\Jobs\CleanupJob:
is_enabled: true
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@
"issues": "http://github.com/heyday/silverstripe-elastica/issues"
},
"require": {
"ruflin/elastica": "3.1.0",
"symbiote/silverstripe-queuedjobs": "~2.10"
"php": ">=5.6.0",
"ruflin/elastica": "~5.3",
"symbiote/silverstripe-queuedjobs": "~4.0"
},
"autoload": {
"psr-4": { "Heyday\\Elastica\\": "src/" }
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
41 changes: 27 additions & 14 deletions src/ElasticaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
use Elastica\Exception\NotFoundException;
use Elastica\Query;
use Psr\Log\LoggerInterface;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Director;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Versioned\Versioned;

/**
* A service used to interact with elastic search.
*/
class ElasticaService extends \Object
class ElasticaService
{
use Configurable;

/**
* @var Client
Expand Down Expand Up @@ -87,7 +93,7 @@ protected function getIndexConfig()
* @param \Elastica\Query|string|array $query
* @param array $options Options defined in \Elastica\Search
* @param bool $returnResultList
* @return ResultList
* @return ResultList | \Elastica\ResultSet
*/
public function search($query, $options = null, $returnResultList = true)
{
Expand Down Expand Up @@ -205,27 +211,36 @@ public function define()
*/
public function refresh()
{
$reading_mode = \Versioned::get_reading_mode();
\Versioned::set_reading_mode('Stage.Live');
$reading_mode = Versioned::get_reading_mode();
Versioned::set_reading_mode('Stage.Live');

foreach ($this->getIndexedClasses() as $class) {
foreach ($class::get() as $record) {

//Only index records with Show In Search enabled for Site Tree descendants
//otherwise index all other data objects
if (($record instanceof \SiteTree && $record->ShowInSearch) ||
(!$record instanceof \SiteTree && ($record->hasMethod('getShowInSearch') && $record->getShowInSearch())) ||
(!$record instanceof \SiteTree && !$record->hasMethod('getShowInSearch'))
if (($record instanceof SiteTree && $record->ShowInSearch) ||
(!$record instanceof SiteTree && ($record->hasMethod('getShowInSearch') && $record->getShowInSearch())) ||
(!$record instanceof SiteTree && !$record->hasMethod('getShowInSearch'))
) {
$this->index($record);
print "<strong>INDEXED: </strong> " . $record->getTitle() . "<br>\n";
if (Director::is_cli()) {
print "INDEXED: Document Type \"" . $record->getClassName() . "\" - " . $record->getTitle() . " - ID " . $record->ID . "\n";
} else {
print "<strong>INDEXED: </strong>Document Type \"" . $record->getClassName() . "\" - " . $record->getTitle() . " - ID " . $record->ID . "<br>";
}

} else {
$this->remove($record);
print "<strong>REMOVED: </strong> " . $record->getTitle() . "<br>\n";
if (Director::is_cli()) {
print "REMOVED: Document Type \"" . $record->getClassName() . "\" - " . $record->getTitle() . " - ID " . $record->ID . "\n";
} else {
print "<strong>REMOVED: </strong>Document Type \"" . $record->getClassName() . "\" - " . $record->getTitle() . " - ID " . $record->ID . "<br>";
}
}
}
}
\Versioned::set_reading_mode($reading_mode);
Versioned::set_reading_mode($reading_mode);
}

/**
Expand All @@ -236,14 +251,12 @@ public function refresh()
public function getIndexedClasses()
{
$classes = array();

foreach (\ClassInfo::subclassesFor('DataObject') as $candidate) {
foreach (ClassInfo::subclassesFor('SilverStripe\ORM\DataObject') as $candidate) {
$candidateInstance = singleton($candidate);
if ($candidateInstance->hasExtension('Heyday\\Elastica\\Searchable')) {
if ($candidateInstance->hasExtension('Heyday\\Elastica\\Searchable') && $candidate != 'Page') {
$classes[] = $candidate;
}
}

return $classes;
}

Expand Down
75 changes: 42 additions & 33 deletions src/Jobs/ReindexAfterWriteJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,39 @@

namespace Heyday\Elastica\Jobs;

use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
use Symbiote\QueuedJobs\Services\QueuedJob;


/**
* Created by PhpStorm.
* User: bdubuisson
* Date: 9/08/17
* Time: 10:06 AM
*/
class ReindexAfterWriteJob extends \AbstractQueuedJob implements \QueuedJob
class ReindexAfterWriteJob extends AbstractQueuedJob implements QueuedJob
{

/**
*
* get the instance to reindex and the service
* ReindexAfterWriteJob constructor.
* @param null $owner
* @param null $id
* @param null $class
*/
public function __construct($owner = null)
public function __construct($id = null, $class = null)
{
if ($owner) {
$this->owner = $owner;
if ($id) {
$this->id = $id;
}
if ($class) {
$this->class = $class;
}

$this->service = \Injector::inst()->get('Heyday\Elastica\ElasticaService');
}


Expand All @@ -35,7 +45,7 @@ public function __construct($owner = null)
*/
public function getTitle()
{
return "Reindexing " . $this->owner->ClassName . " ID " . $this->owner->ID;
return "Reindexing " . $this->class . " ID " . $this->id;
}

/**
Expand All @@ -51,7 +61,7 @@ public function getTitle()
public function getJobType()
{
$this->totalSteps = 'Lots';
return \QueuedJob::QUEUED;
return QueuedJob::QUEUED;
}


Expand All @@ -60,27 +70,26 @@ public function getJobType()
*/
public function process()
{
if ($this->owner && $this->service) {
$reading_mode = \Versioned::get_reading_mode();
\Versioned::set_reading_mode('Stage.Live');
if ($this->id && $this->class) {

$versionToIndex = \DataObject::get($this->owner->ClassName)->byID($this->owner->ID);
if (is_null($versionToIndex)) {
$versionToIndex = $this->owner;
}
$service = Injector::inst()->get('Heyday\Elastica\ElasticaService');
$reading_mode = Versioned::get_reading_mode();
Versioned::set_reading_mode('Stage.Live');

$versionToIndex = DataObject::get($this->class)->byID($this->id);

if (($versionToIndex instanceof \SiteTree && $versionToIndex->ShowInSearch) ||
(!$versionToIndex instanceof \SiteTree && ($versionToIndex->hasMethod('getShowInSearch') && $versionToIndex->ShowInSearch)) ||
(!$versionToIndex instanceof \SiteTree && !$versionToIndex->hasMethod('getShowInSearch'))
if (($versionToIndex instanceof SiteTree && $versionToIndex->ShowInSearch) ||
(!$versionToIndex instanceof SiteTree && ($versionToIndex->hasMethod('getShowInSearch') && $versionToIndex->ShowInSearch)) ||
(!$versionToIndex instanceof SiteTree && !$versionToIndex->hasMethod('getShowInSearch'))
) {
$this->service->index($versionToIndex);
$service->index($versionToIndex);
} else {
$this->service->remove($versionToIndex);
$service->remove($versionToIndex);
}

$this->updateDependentClasses();
$this->updateDependentClasses($versionToIndex, $service);

\Versioned::set_reading_mode($reading_mode);
Versioned::set_reading_mode($reading_mode);
$this->isComplete = true;
return;
} else {
Expand All @@ -91,24 +100,24 @@ public function process()
/**
* Updates the records of all instances of dependent classes.
*/
protected function updateDependentClasses()
protected function updateDependentClasses($versionToIndex, $service)
{
$classes = $this->dependentClasses();
$classes = $this->dependentClasses($versionToIndex);
if ($classes) {
foreach ($classes as $class) {
$list = \DataList::create($class);
$list = DataList::create($class);

foreach ($list as $object) {

if ($object instanceof \DataObject &&
if ($object instanceof DataObject &&
$object->hasExtension('Heyday\\Elastica\\Searchable')
) {
if (($object instanceof \SiteTree && $object->ShowInSearch) ||
(!$object instanceof \SiteTree)
if (($object instanceof SiteTree && $object->ShowInSearch) ||
(!$object instanceof SiteTree)
) {
$this->service->index($object);
$service->index($object);
} else {
$this->service->remove($object);
$service->remove($object);
}
}
}
Expand All @@ -121,8 +130,8 @@ protected function updateDependentClasses()
* extended class is updated or when a relationship to it changes.
* @return array|\scalar
*/
public function dependentClasses()
public function dependentClasses($versionToIndex)
{
return $this->owner->stat('dependent_classes');
return $versionToIndex->stat('dependent_classes');
}
}
Loading

0 comments on commit 3fd8d9d

Please sign in to comment.