Skip to content
Merged
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
27 changes: 27 additions & 0 deletions config/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,31 @@
'fields' => ['title'],
],

/*
|--------------------------------------------------------------------------
| Indexing Queue
|--------------------------------------------------------------------------
|
| Here you may configure the queue name and connection used when indexing
| documents.
|
*/

'queue' => env('STATAMIC_SEARCH_QUEUE'),

'queue_connection' => env('STATAMIC_SEARCH_QUEUE_CONNECTION'),

/*
|--------------------------------------------------------------------------
| Chunk Size
|--------------------------------------------------------------------------
|
| Here you can configure the chunk size used when indexing documents.
| The higher you make it, the more memory it will use, but the quicker
| the indexing process will be.
|
*/

'chunk_size' => 100,

];
2 changes: 1 addition & 1 deletion src/Search/Algolia/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function search($query)
return (new Query($this))->query($query);
}

protected function insertDocuments(Documents $documents)
public function insertDocuments(Documents $documents)
{
$documents = $documents->map(function ($item, $id) {
$item['objectID'] = $id;
Expand Down
2 changes: 1 addition & 1 deletion src/Search/Comb/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function delete($document)
$this->save($data);
}

protected function insertDocuments(Documents $documents)
public function insertDocuments(Documents $documents)
{
try {
$data = $this->data();
Expand Down
22 changes: 14 additions & 8 deletions src/Search/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Closure;
use Statamic\Contracts\Search\Searchable;
use Statamic\Support\Arr;
use Statamic\Support\Str;

abstract class Index
Expand All @@ -20,7 +19,7 @@ abstract public function delete($document);

abstract public function exists();

abstract protected function insertDocuments(Documents $documents);
abstract public function insertDocuments(Documents $documents);

abstract protected function deleteIndex();

Expand Down Expand Up @@ -84,20 +83,27 @@ public function ensureExists()

public function insert($document)
{
return $this->insertMultiple(Arr::wrap($document));
return $this->insertMultiple(collect($document));
}

public function insertMultiple($documents)
{
$documents = (new Documents($documents))->mapWithKeys(function (Searchable $item) {
return [$item->getSearchReference() => $this->searchables()->fields($item)];
});

$this->insertDocuments($documents);
$documents
->chunk(config('statamic.search.chunk_size'))
->each(fn ($documents) => InsertMultipleJob::dispatch(
name: Str::beforeLast($this->name, '_'),
locale: $this->locale,
documents: $documents
));

return $this;
}

public function fields(Searchable $searchable)
{
return $this->searchables()->fields($searchable);
}

public function shouldIndex($searchable)
{
return $this->searchables()->contains($searchable);
Expand Down
52 changes: 52 additions & 0 deletions src/Search/InsertMultipleJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Statamic\Search;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Statamic\Contracts\Search\Searchable;
use Statamic\Facades\Search;
use Statamic\Search\Searchables\Providers;
use Statamic\Support\Str;

class InsertMultipleJob implements ShouldQueue
{
use Queueable;

/**
* Create a new job instance.
*/
public function __construct(
public string $name,
public ?string $locale,
public Collection|LazyCollection $documents,
) {
$this->onConnection($connection = config('statamic.search.queue_connection', config('queue.default')));
$this->onQueue(config('statamic.search.queue', config("queue.connections.{$connection}.queue")));
}

/**
* Execute the job.
*/
public function handle(): void
{
$providers = app(Providers::class);
$index = Search::index($this->name, $this->locale);

$documents = $this->documents
->groupBy(fn ($document) => explode('::', $document)[0])
->flatMap(function ($documents, $prefix) use ($providers) {
return $providers
->getByPrefix($prefix)
->find($documents->map(fn ($reference) => Str::after($reference, '::'))->all())
->all();
})
->mapWithKeys(function (Searchable $item) use ($index) {
return [$item->getSearchReference() => $index->fields($item)];
});

$index->insertDocuments(new Documents($documents));
}
}
2 changes: 1 addition & 1 deletion src/Search/Null/NullIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function exists()
return true;
}

protected function insertDocuments(Documents $documents)
public function insertDocuments(Documents $documents)
{
//
}
Expand Down
2 changes: 1 addition & 1 deletion src/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function updateWithinIndexes(Searchable $searchable)
$exists = $index->exists();

if ($shouldIndex && $exists) {
$index->insert($searchable);
$index->insert($searchable->getSearchReference());
} elseif ($shouldIndex && ! $exists) {
$index->update();
} elseif ($exists) {
Expand Down
4 changes: 3 additions & 1 deletion src/Search/Searchables/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public function provide(): Collection
: AssetCollection::make($this->keys)
->flatMap(fn ($key) => Asset::whereContainer($key));

return $assets->filter($this->filter())->values();
// TODO: query scope support?

return $assets->filter($this->filter())->values()->map->reference();
}

public function contains($searchable): bool
Expand Down
26 changes: 24 additions & 2 deletions src/Search/Searchables/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ public function provide(): Collection|LazyCollection
$query->where('site', $site);
}

return $query->lazy(100)->filter($this->filter())->values();
$this->applyQueryScope($query);

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

$query->where('status', 'published');

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

public function contains($searchable): bool
Expand All @@ -48,7 +60,17 @@ public function contains($searchable): bool
return false;
}

return $this->filter()($searchable);
if ($this->hasFilter()) {
return $this->filter()($searchable);
}

$query = Entry::query()
->where('status', 'published')
->where('id', $searchable->id());

$this->applyQueryScope($query);

return $query->exists();
}

public function find(array $ids): Collection
Expand Down
16 changes: 16 additions & 0 deletions src/Search/Searchables/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Statamic\Search\Searchables;

use Statamic\Facades\Scope;
use Statamic\Facades\Search;
use Statamic\Search\Index;
use Statamic\Search\ProvidesSearchables;
use Statamic\Support\Arr;

abstract class Provider implements ProvidesSearchables
{
Expand Down Expand Up @@ -55,6 +57,20 @@ protected function usesWildcard()
return in_array('*', $this->keys);
}

protected function applyQueryScope($query)
{
if (! $scope = $this->index->config()['query_scope'] ?? null) {
return;
}

Scope::find($scope)->apply($query, []);
}

protected function hasFilter()
{
return Arr::has($this->index->config(), 'filter');
}

protected function filter()
{
$filter = $this->index->config()['filter'] ?? null;
Expand Down
22 changes: 20 additions & 2 deletions src/Search/Searchables/Terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,17 @@ public function provide(): Collection|LazyCollection
$query->where('site', $site);
}

return $query->lazy(100)->filter($this->filter())->values();
$this->applyQueryScope($query);

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

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

public function contains($searchable): bool
Expand All @@ -49,7 +59,15 @@ public function contains($searchable): bool
return false;
}

return $this->filter()($searchable);
if ($this->hasFilter()) {
return $this->filter()($searchable);
}

$query = Term::query()->where('reference', $searchable->reference());

$this->applyQueryScope($query);

return $query->exists();
}

public function find(array $refs): Collection
Expand Down
27 changes: 24 additions & 3 deletions src/Search/Searchables/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\Search\Searchables;

use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Statamic\Contracts\Auth\User as UserContract;
use Statamic\Facades\User;

Expand All @@ -18,9 +19,21 @@ public static function referencePrefix(): string
return 'user';
}

public function provide(): Collection
public function provide(): Collection|LazyCollection
{
return User::all()->filter($this->filter())->values();
$query = User::query();

$this->applyQueryScope($query);

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

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

public function contains($searchable): bool
Expand All @@ -29,7 +42,15 @@ public function contains($searchable): bool
return false;
}

return $this->filter()($searchable);
if ($this->hasFilter()) {
return $this->filter()($searchable);
}

$query = User::query()->where('id', $searchable->id());

$this->applyQueryScope($query);

return $query->exists();
}

public function find(array $ids): Collection
Expand Down
3 changes: 2 additions & 1 deletion tests/Search/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function it_updates_indexes($updateMock)
{
$index = Mockery::mock(Index::class);
$item = Mockery::mock(Searchable::class);
$item->shouldReceive('getSearchReference')->andReturn('a');

$updateMock($index, $item);

Expand All @@ -37,7 +38,7 @@ public static function saveProvider()
function ($mock, $entry) {
$mock->shouldReceive('shouldIndex')->with($entry)->andReturnTrue();
$mock->shouldReceive('exists')->andReturnTrue();
$mock->shouldReceive('insert')->once()->with($entry);
$mock->shouldReceive('insert')->once()->with($entry->getSearchReference());
},
],

Expand Down
Loading