Skip to content

Commit

Permalink
Filter DataObjects on ShowInSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Mar 26, 2020
1 parent 53b373f commit 6fbae44
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Search/Processors/SearchUpdateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,19 @@ protected function prepareIndexes()
SearchVariant::activate_state($state);

// Ensure that indexes for all new / updated objects are included
$indexesThatDeleted = [];
$objs = DataObject::get($base)->byIDs(array_keys($ids));
foreach ($objs as $obj) {
foreach ($ids[$obj->ID] as $index) {
if (!$indexes[$index]->variantStateExcluded($state)) {
$indexes[$index]->add($obj);
$dirtyIndexes[$index] = $indexes[$index];
// remove any existing records from search index if ShowInSearch is false
if (isset($obj->ShowInSearch) && !$obj->ShowInSearch && $obj->ID) {
$indexes[$index]->delete($base, $obj->ID, $state);
$indexesThatDeleted[$index] = true;
} else {
$indexes[$index]->add($obj);
$dirtyIndexes[$index] = $indexes[$index];
}
}
}
unset($ids[$obj->ID]);
Expand All @@ -104,6 +111,10 @@ protected function prepareIndexes()
}
}
}

foreach (array_keys($indexesThatDeleted) as $index) {
$indexes[$index]->commit();
}
}

SearchVariant::activate_state($originalState);
Expand Down
12 changes: 12 additions & 0 deletions src/Solr/Reindex/Handlers/SolrReindexBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ protected function getRecordsInGroup(SolrIndex $indexInstance, $class, $groups,
{
// Generate filtered list of local records
$baseClass = DataObject::getSchema()->baseDataClass($class);
/** @var DataList $items */
$items = DataList::create($class)
->where(sprintf(
'"%s"."ID" %% \'%d\' = \'%d\'',
Expand All @@ -236,6 +237,17 @@ protected function getRecordsInGroup(SolrIndex $indexInstance, $class, $groups,
$items = $items->filter('ClassName', $class);
}

// ShowInSearch filter
// we cannot use $items->remove($item), as that deletes the record from the database
$ids = $items->sort('ID')->column('ID');
$ids = array_fill_keys($ids, true);
foreach ($items as $item) {
if (isset($item->ShowInSearch) && !$item->ShowInSearch) {
unset($ids[$item->ID]);
}
}
$items = DataList::create($class)->filter(['ID' => array_keys($ids)]);

return $items;
}

Expand Down

0 comments on commit 6fbae44

Please sign in to comment.