|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Laravel\Scout\Engines; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\Config\SearchConfig as Algolia3SearchConfig; |
| 6 | +use Algolia\AlgoliaSearch\SearchClient as Algolia3SearchClient; |
| 7 | +use Laravel\Scout\Builder; |
| 8 | +use Laravel\Scout\Jobs\RemoveableScoutCollection; |
| 9 | + |
| 10 | +/** |
| 11 | + * @template TAlgoliaClient of \Algolia\AlgoliaSearch\SearchClient |
| 12 | + */ |
| 13 | +class Algolia3Engine extends AlgoliaEngine |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Create a new engine instance. |
| 17 | + * |
| 18 | + * @param TAlgoliaClient $algolia |
| 19 | + * @param bool $softDelete |
| 20 | + * @return void |
| 21 | + */ |
| 22 | + public function __construct(Algolia3SearchClient $algolia, $softDelete = false) |
| 23 | + { |
| 24 | + parent::__construct($algolia, $softDelete); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Make a new engine instance. |
| 29 | + * |
| 30 | + * @param array $config |
| 31 | + * @param array $headers |
| 32 | + * @param bool $softDelete |
| 33 | + * @return static |
| 34 | + */ |
| 35 | + public static function make(array $config, array $headers, bool $softDelete = false) |
| 36 | + { |
| 37 | + $config = Algolia3SearchConfig::create([ |
| 38 | + 'appId' => $config['id'], |
| 39 | + 'apiKey' => $config['secret'], |
| 40 | + ])->setDefaultHeaders($headers); |
| 41 | + |
| 42 | + if (is_int($connectTimeout = $config['connect_timeout'])) { |
| 43 | + $configuration->setConnectTimeout($connectTimeout); |
| 44 | + } |
| 45 | + |
| 46 | + if (is_int($readTimeout = $config['read_timeout'])) { |
| 47 | + $configuration->setReadTimeout($readTimeout); |
| 48 | + } |
| 49 | + |
| 50 | + if (is_int($writeTimeout = $config['write_timeout'])) { |
| 51 | + $configuration->setWriteTimeout($writeTimeout); |
| 52 | + } |
| 53 | + |
| 54 | + if (is_int($batchSize = $config['batch_size'])) { |
| 55 | + $configuration->setBatchSize($batchSize); |
| 56 | + } |
| 57 | + |
| 58 | + return new static(Algolia3SearchClient::createWithConfig($configuration), $softDelete); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Update the given model in the index. |
| 63 | + * |
| 64 | + * @param \Illuminate\Database\Eloquent\Collection $models |
| 65 | + * @return void |
| 66 | + * |
| 67 | + * @throws \Algolia\AlgoliaSearch\Exceptions\AlgoliaException |
| 68 | + */ |
| 69 | + public function update($models) |
| 70 | + { |
| 71 | + if ($models->isEmpty()) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + $index = $this->algolia->initIndex($models->first()->indexableAs()); |
| 76 | + |
| 77 | + if ($this->usesSoftDelete($models->first()) && $this->softDelete) { |
| 78 | + $models->each->pushSoftDeleteMetadata(); |
| 79 | + } |
| 80 | + |
| 81 | + $objects = $models->map(function ($model) { |
| 82 | + if (empty($searchableData = $model->toSearchableArray())) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + return array_merge( |
| 87 | + $searchableData, |
| 88 | + $model->scoutMetadata(), |
| 89 | + ['objectID' => $model->getScoutKey()], |
| 90 | + ); |
| 91 | + })->filter()->values()->all(); |
| 92 | + |
| 93 | + if (! empty($objects)) { |
| 94 | + $index->saveObjects($objects); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Remove the given model from the index. |
| 100 | + * |
| 101 | + * @param \Illuminate\Database\Eloquent\Collection $models |
| 102 | + * @return void |
| 103 | + */ |
| 104 | + public function delete($models) |
| 105 | + { |
| 106 | + if ($models->isEmpty()) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + $index = $this->algolia->initIndex($models->first()->indexableAs()); |
| 111 | + |
| 112 | + $keys = $models instanceof RemoveableScoutCollection |
| 113 | + ? $models->pluck($models->first()->getScoutKeyName()) |
| 114 | + : $models->map->getScoutKey(); |
| 115 | + |
| 116 | + $index->deleteObjects($keys->all()); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Delete a search index. |
| 121 | + * |
| 122 | + * @param string $name |
| 123 | + * @return mixed |
| 124 | + */ |
| 125 | + public function deleteIndex($name) |
| 126 | + { |
| 127 | + return $this->algolia->initIndex($name)->delete(); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Flush all of the model's records from the engine. |
| 132 | + * |
| 133 | + * @param \Illuminate\Database\Eloquent\Model $model |
| 134 | + * @return void |
| 135 | + */ |
| 136 | + public function flush($model) |
| 137 | + { |
| 138 | + $index = $this->algolia->initIndex($model->indexableAs()); |
| 139 | + |
| 140 | + $index->clearObjects(); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Perform the given search on the engine. |
| 145 | + * |
| 146 | + * @param \Laravel\Scout\Builder $builder |
| 147 | + * @param array $options |
| 148 | + * @return mixed |
| 149 | + */ |
| 150 | + protected function performSearch(Builder $builder, array $options = []) |
| 151 | + { |
| 152 | + $algolia = $this->algolia->initIndex( |
| 153 | + $builder->index ?: $builder->model->searchableAs() |
| 154 | + ); |
| 155 | + |
| 156 | + $options = array_merge($builder->options, $options); |
| 157 | + |
| 158 | + if ($builder->callback) { |
| 159 | + return call_user_func( |
| 160 | + $builder->callback, |
| 161 | + $algolia, |
| 162 | + $builder->query, |
| 163 | + $options |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + return $algolia->search($builder->query, $options); |
| 168 | + } |
| 169 | +} |
0 commit comments