Detektivo
Detektivo copied to clipboard
Support for basic filtering of collections e.g. only indexing published posts
The addon is brilliant so thank you for creating it.
It would be great to add basic filtering on collection fields so that you can choose to only index certain items in a collection.
For example if you have a collection of blog posts you might not wish to index posts that are currently drafts and have not yet been published as they could then appear in any search queries.
Perhaps doing something like this in config.yaml
detektivo:
engine: algolia
app_id: <APP-ID>
api_key: <API-KEY>
collections:
posts: [published:true, title, excerpt, content]
I updated a couple of files to get the above config.yaml working for my use case however the implementation isn't great.
In Controller/Admin.php update the foreach loop in the reindex method on line 33
foreach ($fields as $field) {
$parts = explode(':', $field);
$field = $parts[0];
$options['fields'][$field] = 1;
if(isset($parts[1])){
$filter = $parts[1];
if($filter == 'true'){
$filter = true;
} else if($filter == 'false'){
$filter = false;
}
$options['filter'][$field] = $filter;
}
}
In bootstrap.php update the foreach loop in 'collections.save.after' on line 78
foreach ($fields as $field) {
$parts = explode(':', $field);
$field = $parts[0];
if (isset($entry[$field])) {
$data[$field] = $entry[$field];
if(isset($parts[1])){
$filter = $parts[1];
if($filter == 'true'){
$filter = true;
} else if($filter == 'false'){
$filter = false;
}
if($entry[$field] !== $filter){
return;
}
}
}
}
Now whenever reindexing or saving a blog post if you have set published:true
in your config.yaml only published posts will be indexed with your Detektivo engine (in my case Algolia).
This seems to work with simple filtering like the below but the implementation could definitely be improved.
posts: [published:true, title:Example Post, excerpt, content]