| 1 | <?php |
||
| 2 | |||
| 3 | namespace Matchish\ScoutElasticSearch\Engines; |
||
| 4 | |||
| 5 | use Elasticsearch\Common\Exceptions\ServerErrorResponseException; |
||
| 6 | use Illuminate\Database\Eloquent\Collection; |
||
| 7 | use Laravel\Scout\Builder; |
||
| 8 | use Laravel\Scout\Builder as BaseBuilder; |
||
| 9 | use Laravel\Scout\Engines\Engine; |
||
| 10 | use Matchish\ScoutElasticSearch\ElasticSearch\HitsIteratorAggregate; |
||
| 11 | use Matchish\ScoutElasticSearch\ElasticSearch\Params\Bulk; |
||
| 12 | use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Refresh; |
||
| 13 | use Matchish\ScoutElasticSearch\ElasticSearch\Params\Search as SearchParams; |
||
| 14 | use Matchish\ScoutElasticSearch\ElasticSearch\SearchFactory; |
||
| 15 | use Matchish\ScoutElasticSearch\ElasticSearch\SearchResults; |
||
| 16 | use ONGR\ElasticsearchDSL\Query\MatchAllQuery; |
||
| 17 | use ONGR\ElasticsearchDSL\Search; |
||
| 18 | |||
| 19 | final class ElasticSearchEngine extends Engine |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * The ElasticSearch client. |
||
| 23 | * |
||
| 24 | * @var \Elasticsearch\Client |
||
| 25 | */ |
||
| 26 | protected $elasticsearch; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Create a new engine instance. |
||
| 30 | * |
||
| 31 | * @param \Elasticsearch\Client $elasticsearch |
||
| 32 | * @return void |
||
| 33 | */ |
||
| 34 | 27 | public function __construct(\Elasticsearch\Client $elasticsearch) |
|
| 35 | { |
||
| 36 | 27 | $this->elasticsearch = $elasticsearch; |
|
| 37 | 27 | } |
|
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | 20 | public function update($models) |
|
| 43 | { |
||
| 44 | 20 | $params = new Bulk(); |
|
| 45 | 20 | $params->index($models); |
|
| 46 | 20 | $response = $this->elasticsearch->bulk($params->toArray()); |
|
| 47 | 20 | if (array_key_exists('errors', $response) && $response['errors']) { |
|
| 48 | 1 | $error = new ServerErrorResponseException(json_encode($response, JSON_PRETTY_PRINT)); |
|
| 49 | 1 | throw new \Exception('Bulk update error', $error->getCode(), $error); |
|
| 50 | } |
||
| 51 | 19 | } |
|
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritdoc} |
||
| 55 | */ |
||
| 56 | 1 | public function delete($models) |
|
| 57 | { |
||
| 58 | 1 | $params = new Bulk(); |
|
| 59 | 1 | $params->delete($models); |
|
| 60 | 1 | $this->elasticsearch->bulk($params->toArray()); |
|
| 61 | 1 | } |
|
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | 2 | public function flush($model) |
|
| 67 | { |
||
| 68 | 2 | $indexName = $model->searchableAs(); |
|
| 69 | 2 | $exist = $this->elasticsearch->indices()->exists(['index' => $indexName]); |
|
| 70 | 2 | if ($exist) { |
|
| 71 | 2 | $body = (new Search())->addQuery(new MatchAllQuery())->toArray(); |
|
| 72 | 2 | $params = new SearchParams($indexName, $body); |
|
| 73 | 2 | $this->elasticsearch->deleteByQuery($params->toArray()); |
|
| 74 | 2 | $this->elasticsearch->indices()->refresh((new Refresh($indexName))->toArray()); |
|
| 75 | } |
||
| 76 | 2 | } |
|
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritdoc} |
||
| 80 | */ |
||
| 81 | 6 | public function search(BaseBuilder $builder) |
|
| 82 | { |
||
| 83 | 6 | return $this->performSearch($builder, []); |
|
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * {@inheritdoc} |
||
| 88 | */ |
||
| 89 | 1 | public function paginate(BaseBuilder $builder, $perPage, $page) |
|
| 90 | { |
||
| 91 | 1 | return $this->performSearch($builder, [ |
|
| 92 | 1 | 'from' => ($page - 1) * $perPage, |
|
| 93 | 1 | 'size' => $perPage, |
|
| 94 | ]); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | */ |
||
| 100 | 1 | public function mapIds($results) |
|
| 101 | { |
||
| 102 | 1 | return collect($results['hits']['hits'])->pluck('_id'); |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritdoc} |
||
| 107 | */ |
||
| 108 | 7 | public function map(BaseBuilder $builder, $results, $model) |
|
| 109 | { |
||
| 110 | 7 | $hits = app()->makeWith( |
|
| 111 | 7 | HitsIteratorAggregate::class, |
|
| 112 | [ |
||
| 113 | 7 | 'results' => $results, |
|
| 114 | 7 | 'callback' => $builder->queryCallback, |
|
| 115 | ] |
||
| 116 | ); |
||
| 117 | |||
| 118 | 7 | return new Collection($hits); |
|
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Map the given results to instances of the given model via a lazy collection. |
||
| 123 | * |
||
| 124 | * @param \Laravel\Scout\Builder $builder |
||
| 125 | * @param mixed $results |
||
| 126 | * @param \Illuminate\Database\Eloquent\Model $model |
||
| 127 | * @return \Illuminate\Support\LazyCollection |
||
| 128 | */ |
||
| 129 | public function lazyMap(Builder $builder, $results, $model) |
||
| 130 | { |
||
| 131 | throw new \Error('Not implemented'); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Create a search index. |
||
| 136 | * |
||
| 137 | * @param string $name |
||
| 138 | * @param array $options |
||
| 139 | * @return mixed |
||
| 140 | */ |
||
| 141 | public function createIndex($name, array $options = []) |
||
| 142 | { |
||
| 143 | throw new \Error('Not implemented'); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Delete a search index. |
||
| 148 | * |
||
| 149 | * @param string $name |
||
| 150 | * @return mixed |
||
| 151 | */ |
||
| 152 | public function deleteIndex($name) |
||
| 153 | { |
||
| 154 | throw new \Error('Not implemented'); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | */ |
||
| 160 | 1 | public function getTotalCount($results) |
|
| 161 | { |
||
| 162 | 1 | return $results['hits']['total']['value']; |
|
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param BaseBuilder $builder |
||
| 167 | * @param array $options |
||
| 168 | * @return SearchResults|mixed |
||
| 169 | */ |
||
| 170 | 7 | private function performSearch(BaseBuilder $builder, $options = []) |
|
| 171 | { |
||
| 172 | 7 | $searchBody = SearchFactory::create($builder, $options); |
|
| 173 | 7 | if ($builder->callback) { |
|
| 174 | /** @var callable */ |
||
| 175 | 2 | $callback = $builder->callback; |
|
| 176 | |||
| 177 | 2 | return call_user_func( |
|
| 178 | 2 | $callback, |
|
| 179 | 2 | $this->elasticsearch, |
|
| 180 | $searchBody |
||
| 181 | ); |
||
| 182 | } |
||
| 183 | |||
| 184 | 5 | $model = $builder->model; |
|
| 185 | 5 | $indexName = $builder->index ?: $model->searchableAs(); |
|
| 186 | 5 | $params = new SearchParams($indexName, $searchBody->toArray()); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 187 | |||
| 188 | 5 | return $this->elasticsearch->search($params->toArray()); |
|
| 189 | } |
||
| 190 | } |
||
| 191 |