Skip to content

Commit

Permalink
feat: implement better subsite indexing out of the box
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Oct 25, 2024
1 parent 7c8cf00 commit 98f1945
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,48 @@ To run a full reindex of Elastica use
./vendor/bin/sake dev/tasks/ElasticaReindexTask
```

## Subsite support

Our typical setup is to add `SubsiteID` as an indexed_field.

```yml
indexed_fields:
- ..
- SubsiteID
```
And filter by this at query time.
```php
$query = new \Elastica\Query();
$bool = new \Elastica\Query\BoolQuery();

$match = new \Elastica\Query\MultiMatch();
$match
->setQuery(strval($searchString))
->setFields($this->getESFields())
->setType('most_fields')
->setFuzziness('AUTO');
$bool->addMust($match);

$subsiteId = SubsiteState::singleton()->getSubsiteId();

if ($subsiteId) {
$bool->addFilter(
new \Elastica\Query\Term(['SubsiteID' => $subsiteId])
);
} else {
$bool->addFilter(
new \Elastica\Query\Term(['SubsiteID' => 0])
);
}

...
```

At indexing time


## Using Queues

You can make use of queues to have your reindex processes run in the background.
Expand Down
22 changes: 20 additions & 2 deletions src/ElasticaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,15 +421,24 @@ function () {
Versioned::set_stage(Versioned::LIVE);

foreach ($this->getIndexedClasses() as $class) {
foreach (DataObject::get($class) as $record) {
// Only index records with Show In Search enabled, or those that don't expose that fielid
$list = DataObject::get($class);

$this->printMessage($list->count(), sprintf('FOUND %s records of type %s', $list->count(), $class));

$records = $list->chunkedFetch();
foreach ($records as $record) {
// Only index records with Show In Search enabled, or those that don't expose that field
if (!$record->hasField('ShowInSearch') || $record->ShowInSearch) {
if ($this->index($record)) {
$this->printActionMessage($record, 'INDEXED');
} else {
$this->printActionMessage($record, 'ERROR INDEXING');
}
} else {
if ($this->remove($record)) {
$this->printActionMessage($record, 'REMOVED');
} else {
$this->printActionMessage($record, 'ERROR REMOVING');
}
}
}
Expand Down Expand Up @@ -472,6 +481,15 @@ protected function printActionMessage(DataObject $record, $action)
}
}

protected function printMessage(string $details, string $action)
{
if (Director::is_cli()) {
print "{$action}: {$details}\n";
} else {
print "<strong>{$action}: </strong>{$details}<br>";
}
}

/**
* If a logger is configured, log the exception there.
*
Expand Down
5 changes: 4 additions & 1 deletion src/ReindexTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\BuildTask;

/**
* Defines and refreshes the elastic search index.
*/
Expand Down Expand Up @@ -43,6 +42,10 @@ public function __construct(ElasticaService $service)
*/
public function run($request)
{
if (class_exists('\SilverStripe\Subsites\Model\Subsite')) {
\SilverStripe\Subsites\Model\Subsite::disable_subsite_filter(true);
}

$message = function ($content) {
print(Director::is_cli() ? "$content\n" : "<p>$content</p>");
};
Expand Down

0 comments on commit 98f1945

Please sign in to comment.