123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- namespace App\Search\Engines;
- use App\Search\Builders\ElasticsearchBuilder;
- use Elasticsearch\Client as Elastic;
- use Illuminate\Database\Eloquent\Collection;
- use Laravel\Scout\Builder;
- use Laravel\Scout\Engines\Engine;
- use Monolog\Handler\StreamHandler;
- use Monolog\Logger;
- class ElasticsearchEngine extends Engine
- {
- private $type="doc";
- /**
- * @var Elastic
- */
- private $elastic;
- private $logger;
- /**
- * ElasticsearchEngine constructor.
- * @param Elastic $elastic
- * @throws \Exception
- */
- public function __construct(Elastic $elastic)
- {
- $this->elastic = $elastic;
- $this->logger = new Logger('elastic');
- $this->logger->pushHandler(new StreamHandler(storage_path('logs/elasticsearch/error.log'), Logger::ERROR));
- }
- /**
- * Update the given model in the index.
- *
- * @param \Illuminate\Database\Eloquent\Collection $models
- * @return void
- */
- public function update($models)
- {
- $params['body'] = [];
- $models->each(function ($model) use (&$params) {
- $params['body'][] = [
- 'update' => [
- '_id' => $model->getKey(),
- '_index' => $model->searchableAs(),
- '_type' => $this->type,
- ]
- ];
- $params['body'][] = [
- 'doc' => $model->toSearchableArray(),
- 'doc_as_upsert' => true
- ];
- });
- $result=$this->elastic->bulk($params);
- if ($result['errors'] === true) {
- $this->logger->error("更新失败", $result);
- //throw new \Exception(json_encode($result));
- }
- }
- /**
- * Remove the given model from the index.
- *
- * @param \Illuminate\Database\Eloquent\Collection $models
- * @return void
- */
- public function delete($models)
- {
- $params['body'] = [];
- $models->each(function ($model) use (&$params) {
- $params['body'][] = [
- 'delete' => [
- '_id' => $model->getKey(),
- '_index' => $model->searchableAs(),
- '_type' => $this->type,
- ]
- ];
- });
- $this->elastic->bulk($params);
- }
- /**
- * Perform the given search on the engine.
- *
- * @param \Laravel\Scout\Builder $builder
- * @return mixed
- */
- public function search(Builder $builder)
- {
- return $this->performSearch($builder, array_filter([
- 'size' => $builder->limit,
- ]));
- }
- /**
- * Perform the given search on the engine.
- *
- * @param \Laravel\Scout\Builder $builder
- * @param int $perPage
- * @param int $page
- * @return mixed
- */
- public function paginate(Builder $builder, $perPage, $page)
- {
- $result = $this->performSearch($builder, [
- 'from' => (($page * $perPage) - $perPage),
- 'size' => $perPage,
- ]);
- return $result;
- }
- /**
- * Pluck and return the primary keys of the given results.
- *
- * @param mixed $results
- * @return \Illuminate\Support\Collection
- */
- public function mapIds($results)
- {
- return collect($results['hits']['hits'])->pluck('_id')->values();
- }
- /**
- * Map the given results to instances of the given model.
- *
- * @param \Laravel\Scout\Builder $builder
- * @param mixed $results
- * @param \Illuminate\Database\Eloquent\Model $model
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public function map(Builder $builder, $results, $model)
- {
- if ($results['hits']['total'] === 0) {
- return Collection::make();
- }
- $keys = collect($results['hits']['hits'])
- ->pluck('_id')->values()->all();
- $models = $model->getScoutModelsByIds($builder, $keys)->keyBy(function ($model) {
- return $model->getScoutKey();
- });
- return collect($results['hits']['hits'])->map(function ($hit) use ($model, $models) {
- return isset($models[$hit['_id']]) ? $models[$hit['_id']] : null;
- })->filter()->values();
- }
- /**
- * Get the total count from a raw result returned by the engine.
- *
- * @param mixed $results
- * @return int
- */
- public function getTotalCount($results)
- {
- return $results['hits']['total']>1000?1000:$results['hits']['total'];
- }
- /**
- * Flush all of the model's records from the engine.
- *
- * @param \Illuminate\Database\Eloquent\Model $model
- * @return void
- */
- public function flush($model)
- {
- // TODO: Implement flush() method.
- }
- /**
- * Perform the given search on the engine.
- *
- * @param ElasticsearchBuilder $builder
- * @param array $options
- * @return mixed
- */
- protected function performSearch($builder, array $options = [])
- {
- $params = [
- 'index' => $builder->model->searchableAs(),
- 'type' => $this->type,
- 'body' => [
- 'query' => [
- 'bool' => [
- ]
- ]
- ]
- ];
- if (!empty($query = $builder->getQueryData())) {
- $params['body']['query']['bool']=$query;
- }
- $relation_sotr=[];
- if (!empty($builder->query)) {
- $relation_sotr[]=['_score' => 'desc'];
- $params['body']['query']['bool']['must'][]=[
- 'bool'=>[
- "should"=>[
- [
- 'match' => [
- 'search_field1' => [
- "query" => "{$builder->query}",
- "boost" => 2
- ]
- ]
- ],
- [
- 'match' => [
- 'search_field2' => [
- "query" => "{$builder->query}",
- "boost" => 0
- ]
- ]
- ]
- ]
- ]
- ];
- }
- if (empty($params['body']['query']['bool'])) {
- $params['body']['query']['bool']['must'][]=['match_all'=>new \ArrayObject()];
- }
- if ($sort = $this->sort($builder)) {
- if (!empty($relation_sotr)) {
- $sort =array_merge($relation_sotr, $sort);
- }
- $params['body']['sort'] = $sort;
- }
- if (isset($options['from'])) {
- $params['body']['from'] = $options['from'];
- }
- if (isset($options['size'])) {
- $params['body']['size'] = $options['size'];
- }
- if ($builder->callback) {
- return call_user_func(
- $builder->callback,
- $this->elastic,
- $builder->query,
- $params
- );
- }
- return $this->elastic->search($params);
- }
- /**
- * Generates the sort if theres any.
- *
- * @param Builder $builder
- * @return array|null
- */
- protected function sort($builder)
- {
- if (count($builder->orders) == 0) {
- return null;
- }
- return collect($builder->orders)->map(function ($order) {
- if (is_array($order['column'])) {
- return ['_geo_distance'=> $order['column']];
- }
- return [$order['column'] => $order['direction']];
- })->toArray();
- }
- }
|