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 as BaseBuilder; |
8
|
|
|
use Laravel\Scout\Engines\Engine; |
9
|
|
|
use Laravel\Scout\Searchable; |
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
|
|
|
/** @var string */ |
29
|
|
|
private $hitsIterator = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new engine instance. |
33
|
|
|
* |
34
|
|
|
* @param \Elasticsearch\Client $elasticsearch |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
27 |
|
public function __construct(\Elasticsearch\Client $elasticsearch) |
38
|
|
|
{ |
39
|
27 |
|
$this->elasticsearch = $elasticsearch; |
40
|
27 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
20 |
|
public function update($models) |
46
|
|
|
{ |
47
|
20 |
|
$params = new Bulk(); |
48
|
20 |
|
$params->index($models); |
49
|
20 |
|
$response = $this->elasticsearch->bulk($params->toArray()); |
50
|
20 |
|
if (array_key_exists('errors', $response) && $response['errors']) { |
51
|
1 |
|
$error = new ServerErrorResponseException(json_encode($response, JSON_PRETTY_PRINT)); |
52
|
1 |
|
throw new \Exception('Bulk update error', $error->getCode(), $error); |
53
|
|
|
} |
54
|
19 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
1 |
|
public function delete($models) |
60
|
|
|
{ |
61
|
1 |
|
$params = new Bulk(); |
62
|
1 |
|
$params->delete($models); |
63
|
1 |
|
$this->elasticsearch->bulk($params->toArray()); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
2 |
|
public function flush($model) |
70
|
|
|
{ |
71
|
2 |
|
$indexName = $model->searchableAs(); |
72
|
2 |
|
$exist = $this->elasticsearch->indices()->exists(['index' => $indexName]); |
73
|
2 |
|
if ($exist) { |
74
|
2 |
|
$body = (new Search())->addQuery(new MatchAllQuery())->toArray(); |
75
|
2 |
|
$params = new SearchParams($indexName, $body); |
76
|
2 |
|
$this->elasticsearch->deleteByQuery($params->toArray()); |
77
|
2 |
|
$this->elasticsearch->indices()->refresh((new Refresh($indexName))->toArray()); |
78
|
|
|
} |
79
|
2 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
6 |
|
public function search(BaseBuilder $builder) |
85
|
|
|
{ |
86
|
6 |
|
return $this->performSearch($builder, []); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
1 |
|
public function paginate(BaseBuilder $builder, $perPage, $page) |
93
|
|
|
{ |
94
|
1 |
|
return $this->performSearch($builder, [ |
95
|
1 |
|
'from' => ($page - 1) * $perPage, |
96
|
1 |
|
'size' => $perPage, |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
1 |
|
public function mapIds($results) |
104
|
|
|
{ |
105
|
1 |
|
return collect($results['hits']['hits'])->pluck('_id'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
7 |
|
public function map(BaseBuilder $builder, $results, $model) |
112
|
|
|
{ |
113
|
7 |
|
if(is_null($this->hitsIterator)){ |
|
|
|
|
114
|
7 |
|
$hits = app()->makeWith(HitsIteratorAggregate::class, |
115
|
7 |
|
['results' => $results, |
116
|
7 |
|
'callback' => $builder->queryCallback, |
117
|
|
|
]); |
118
|
|
|
} else { |
119
|
|
|
$hits = app()->makeWith($this->hitsIterator, |
120
|
|
|
['results' => $results, |
121
|
|
|
'callback' => $builder->queryCallback, |
122
|
|
|
]); |
123
|
|
|
} |
124
|
|
|
|
125
|
7 |
|
return new Collection($hits); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
1 |
|
public function getTotalCount($results) |
132
|
|
|
{ |
133
|
1 |
|
return $results['hits']['total']['value']; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param BaseBuilder $builder |
138
|
|
|
* @param array $options |
139
|
|
|
* @return SearchResults|mixed |
140
|
|
|
*/ |
141
|
7 |
|
private function performSearch(BaseBuilder $builder, $options = []) |
142
|
|
|
{ |
143
|
7 |
|
$searchBody = SearchFactory::create($builder, $options); |
144
|
7 |
|
if ($builder->callback) { |
145
|
|
|
/** @var callable */ |
146
|
2 |
|
$callback = $builder->callback; |
147
|
|
|
|
148
|
2 |
|
return call_user_func( |
149
|
2 |
|
$callback, |
150
|
2 |
|
$this->elasticsearch, |
151
|
2 |
|
$searchBody, |
152
|
2 |
|
$this |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
/** @var Searchable $model */ |
156
|
5 |
|
$model = $builder->model; |
157
|
5 |
|
$indexName = $builder->index ?: $model->searchableAs(); |
158
|
5 |
|
$params = new SearchParams($indexName, $searchBody->toArray()); |
159
|
|
|
|
160
|
5 |
|
return $this->elasticsearch->search($params->toArray()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param $value |
165
|
|
|
*/ |
166
|
|
|
public function useHitsIterator($value) |
167
|
|
|
{ |
168
|
|
|
$this->hitsIterator = $value; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|