Skip to content

Commit bae2bc3

Browse files
[6.x] Search Indexing Performance Improvements (#1774)
1 parent 6bfd4bf commit bae2bc3

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

content/collections/pages/5-to-6.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,68 @@ The REST API will now just give you sub-keys:
588588
}
589589
```
590590

591+
### Search: Changes to custom searchables
592+
593+
Searchables should now return collections of references (eg. `entry::the-entry-id`) instead of objects.
594+
595+
```php
596+
return Thing::query()->lazy(); // [tl! remove]
597+
return Thing::query()->lazy()->pluck('reference'); // [tl! add]
598+
```
599+
600+
If your searchable allows for it, you may want to consider adding support for query scopes, which we now recommend over filtering.
601+
602+
```php
603+
$query = Thing::query();
604+
605+
$this->applyQueryScope($query);
606+
607+
return $query->pluck('reference');
608+
```
609+
610+
If you support filtering, you may want to split the "with filter" & "without filter" cases into separate return statements.
611+
612+
The `->filter()` method evaluates every item in the collection, which is an unnecessary performance hit when no filter is configured:
613+
614+
```php
615+
$query = Thing::query();
616+
617+
if ($this->hasFilter()) {
618+
return $query
619+
->lazy(config('statamic.search.chunk_size'))
620+
->filter($this->filter())
621+
->values()
622+
->map->reference();
623+
}
624+
625+
return $query->pluck('reference');
626+
```
627+
628+
### Search: Changes to custom search drivers
629+
630+
The `insertDocument` method is now public:
631+
632+
```php
633+
protected function insertDocuments(Documents $documents) // [tl! remove]
634+
public function insertDocuments(Documents $documents) // [tl! add]
635+
```
636+
637+
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).
638+
639+
If you need to manipulate the fields array before it gets sent to your index, you may define a `fields` method:
640+
641+
```php
642+
public function fields(Searchable $searchable)
643+
{
644+
return array_merge(
645+
$this->searchables()->fields($searchable),
646+
[
647+
'_some_special_field_' => $searchable->id(),
648+
]
649+
);
650+
}
651+
```
652+
591653
### Bard: Strikes now output `<strike>` tags, rather than `<s>`
592654
**Affects apps relying on the `<s>` tag for strikes.**
593655

content/collections/pages/search.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,17 +595,24 @@ class ProductProvider extends Provider
595595
}
596596

597597
/**
598-
* Get a collection of all searchables.
598+
* Get all searchables and return a collection of
599+
* their references.
600+
*
601+
* e.g. 'entry::123'
599602
*/
600603
public function provide(): Collection
601604
{
602-
return Product::all();
605+
return Product::query()
606+
->pluck('id')
607+
->map(fn ($id) => "product::{$id}");
603608

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

611618
/**
@@ -726,7 +733,7 @@ class FastSearchIndex extends Index
726733
/**
727734
* Insert items into the index.
728735
*/
729-
protected function insertDocuments(Documents $documents)
736+
public function insertDocuments(Documents $documents)
730737
{
731738
$this->client->insertObjects($documents->all());
732739
}

0 commit comments

Comments
 (0)