Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions content/collections/pages/5-to-6.md
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,68 @@ The REST API will now just give you sub-keys:
}
```

### Search: Changes to custom searchables

Searchables should now return collections of references (eg. `entry::the-entry-id`) instead of objects.

```php
return Thing::query()->lazy(); // [tl! remove]
return Thing::query()->lazy()->pluck('reference'); // [tl! add]
```

If your searchable allows for it, you may want to consider adding support for query scopes, which we now recommend over filtering.

```php
$query = Thing::query();

$this->applyQueryScope($query);

return $query->pluck('reference');
```

If you support filtering, you may want to split the "with filter" & "without filter" cases into separate return statements.

The `->filter()` method evaluates every item in the collection, which is an unnecessary performance hit when no filter is configured:

```php
$query = Thing::query();

if ($this->hasFilter()) {
return $query
->lazy(config('statamic.search.chunk_size'))
->filter($this->filter())
->values()
->map->reference();
}

return $query->pluck('reference');
```

### Search: Changes to custom search drivers

The `insertDocument` method is now public:

```php
protected function insertDocuments(Documents $documents) // [tl! remove]
public function insertDocuments(Documents $documents) // [tl! add]
```

If you were previously overriding the `insertMultiple` method to chunk documents, you don't need to do that anymore (chunking is now handled by the base method).

If you need to manipulate the fields array before it gets sent to your index, you may define a `fields` method:

```php
public function fields(Searchable $searchable)
{
return array_merge(
$this->searchables()->fields($searchable),
[
'_some_special_field_' => $searchable->id(),
]
);
}
```


## Zero impact changes

Expand Down
15 changes: 11 additions & 4 deletions content/collections/pages/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -570,17 +570,24 @@ class ProductProvider extends Provider
}

/**
* Get a collection of all searchables.
* Get all searchables and return a collection of
* their references.
*
* e.g. 'entry::123'
*/
public function provide(): Collection
{
return Product::all();
return Product::query()
->pluck('id')
->map(fn ($id) => "product::{$id}");

// If you wanted to allow subsets of products, you could specify them in your
// config then retrieve them appropriately here using keys.
// e.g. 'searchables' => ['products:hats', 'products:shoes'],
// $this->keys would be ['keys', 'hats'].
return Product::whereIn('type', $this->keys)->get();
return Product::whereIn('type', $this->keys)
->pluck('id')
->map(fn ($id) => "product::{$id}");
}

/**
Expand Down Expand Up @@ -689,7 +696,7 @@ class FastSearchIndex extends Index
/**
* Insert items into the index.
*/
protected function insertDocuments(Documents $documents)
public function insertDocuments(Documents $documents)
{
$this->client->insertObjects($documents->all());
}
Expand Down