diff --git a/src/Engines/ElasticEngine.php b/src/Engines/ElasticEngine.php index cb56e85..ffd4eaa 100644 --- a/src/Engines/ElasticEngine.php +++ b/src/Engines/ElasticEngine.php @@ -29,7 +29,7 @@ class ElasticEngine extends Engine */ public function __construct(Elastic $elastic) { - + $this->elastic = $elastic; } @@ -51,7 +51,7 @@ public function update($models) if ($this->usesSoftDelete($models->first()) && config('scout.soft_delete', false)) { $models->each->pushSoftDeleteMetadata(); } - + $models->map( function ($model) use (&$params, $index) { $array = array_merge($model->toSearchableArray(), $model->scoutMetadata()); @@ -60,12 +60,11 @@ function ($model) use (&$params, $index) { $object = []; $index['_id'] = $model->getScoutKey(); $object["index"] = $index['_index']; - $object["type"] = 'doc'; + $object["type"] = 'doc'; $object["id"] = $index['_id']; $object['body'] = ["doc" => $array, "doc_as_upsert" => true]; $params[] = $object; $this->elastic->update($object); - } } ); @@ -85,11 +84,11 @@ public function delete($models) function ($model) use (&$params, $index) { $index['_id'] = $model->getScoutKey(); $params['body'][] = [ - 'delete' => $index + 'delete' => $index ]; } ); - if (! empty($params)) { + if (!empty($params)) { $this->elastic->bulk($params); } } @@ -105,10 +104,11 @@ public function search(Builder $builder) return $this->performSearch( - $builder, array_filter( + $builder, + array_filter( [ - 'numericFilters' => $this->filters($builder), - 'hitsPerPage' => $builder->limit, + 'numericFilters' => $this->filters($builder), + 'hitsPerPage' => $builder->limit, ] ) ); @@ -125,12 +125,13 @@ public function search(Builder $builder) public function paginate(Builder $builder, $perPage, $page) { - + return $this->performSearch( - $builder, [ - 'numericFilters' => $this->filters($builder), - 'hitsPerPage' => $perPage, - 'page' => $page - 1, + $builder, + [ + 'numericFilters' => $this->filters($builder), + 'hitsPerPage' => $perPage, + 'page' => $page - 1, ] ); } @@ -163,18 +164,18 @@ public function map(Builder $builder, $results, $model) $keys = collect($results['hits']['hits'])->pluck('_id')->values()->all(); - if(method_exists($model, "multipleAs")){ + if (method_exists($model, "multipleAs")) { return collect($results); - }else{ + } else { return $model->getScoutModelsByIds( - $builder, $keys + $builder, + $keys )->filter( function ($model) use ($keys) { - return in_array($model->getScoutKey(), $keys); + return in_array($model->getScoutKey(), $keys); } ); } - } /** @@ -188,6 +189,22 @@ public function getTotalCount($results) return $results['hits']['total']; } + /** + * Create an index + * + * @param $index + * @return void + */ + public function createIndex($index, $map_file) + { + $mapping = json_decode(file_get_contents(database_path($map_file)), true); + $params = [ + 'index' => $index, + 'body' => $mapping + ]; + return $this->elastic->indices()->create($params); + } + /** * Flush all of the model's records from the engine. * @@ -224,7 +241,7 @@ public function drop($model) ]; return $this->elastic->indices()->delete($params); } - + /** * Determine if the given model uses soft deletes. * @@ -244,9 +261,9 @@ protected function usesSoftDelete($model) protected function multiIndex(Model $model) { - if(method_exists($model, "multipleAs")){ + if (method_exists($model, "multipleAs")) { $index = $model->multipleAs(); - }else{ + } else { $index = $model->searchableAs(); } @@ -256,9 +273,9 @@ protected function multiIndex(Model $model) ]; return $params; } - - /** + + /** * Config index and type with model * * @param string $index @@ -272,7 +289,7 @@ protected function initIndex(Model $model) ]; return $params; } - + /** * Get the filter array for the query. * @@ -288,9 +305,9 @@ function ($value, $key) { } return ['match_phrase' => [$key => $value]]; } - )->values()->all(); + )->values()->all(); } - + /** * Perform the given search on the engine. * @@ -303,7 +320,7 @@ protected function performSearch(Builder $builder, array $options = []) $index = $this->multiIndex($builder->model); - + $params = [ 'index' => $index['_index'], 'type' => "doc", @@ -321,7 +338,7 @@ protected function performSearch(Builder $builder, array $options = []) ] ] ]; - + if ($sort = $this->sort($builder)) { $params['body']['sort'] = $sort; } @@ -337,7 +354,7 @@ protected function performSearch(Builder $builder, array $options = []) $options['numericFilters'] ); } - + if ($builder->callback) { return call_user_func( $builder->callback, @@ -349,7 +366,7 @@ protected function performSearch(Builder $builder, array $options = []) return $this->elastic->search($params); } - + /** * Generates the sort if theres any. * @@ -367,4 +384,4 @@ function ($order) { } )->toArray(); } -} \ No newline at end of file +}