10
10
11
11
class ElasticsearchEngine extends Engine
12
12
{
13
- /**
14
- * Index where the models will be saved.
15
- *
16
- * @var string
17
- */
18
- protected $ index ;
19
-
20
13
/**
21
14
* Elastic where the instance of Elastic|\Elasticsearch\Client is stored.
22
15
*
@@ -30,10 +23,9 @@ class ElasticsearchEngine extends Engine
30
23
* @param \Elasticsearch\Client $elastic
31
24
* @return void
32
25
*/
33
- public function __construct (Elastic $ elastic, $ index )
26
+ public function __construct (Elastic $ elastic )
34
27
{
35
28
$ this ->elastic = $ elastic ;
36
- $ this ->index = $ index ;
37
29
}
38
30
39
31
/**
@@ -46,12 +38,11 @@ public function update($models)
46
38
{
47
39
$ params ['body ' ] = [];
48
40
49
- $ models ->each (function ($ model ) use (&$ params )
50
- {
41
+ $ models ->each (function ($ model ) use (&$ params ) {
51
42
$ params ['body ' ][] = [
52
43
'update ' => [
53
44
'_id ' => $ model ->getKey (),
54
- '_index ' => $ this -> index ,
45
+ '_index ' => $ model -> searchableAs () ,
55
46
'_type ' => $ model ->searchableAs (),
56
47
]
57
48
];
@@ -74,12 +65,11 @@ public function delete($models)
74
65
{
75
66
$ params ['body ' ] = [];
76
67
77
- $ models ->each (function ($ model ) use (&$ params )
78
- {
68
+ $ models ->each (function ($ model ) use (&$ params ) {
79
69
$ params ['body ' ][] = [
80
70
'delete ' => [
81
71
'_id ' => $ model ->getKey (),
82
- '_index ' => $ this -> index ,
72
+ '_index ' => $ model -> searchableAs () ,
83
73
'_type ' => $ model ->searchableAs (),
84
74
]
85
75
];
@@ -118,7 +108,7 @@ public function paginate(Builder $builder, $perPage, $page)
118
108
'size ' => $ perPage ,
119
109
]);
120
110
121
- $ result ['nbPages ' ] = $ result ['hits ' ]['total ' ]/$ perPage ;
111
+ $ result ['nbPages ' ] = $ result ['hits ' ]['total ' ]/$ perPage ;
122
112
123
113
return $ result ;
124
114
}
@@ -133,8 +123,7 @@ public function paginate(Builder $builder, $perPage, $page)
133
123
protected function performSearch (Builder $ builder , array $ options = [])
134
124
{
135
125
$ params = [
136
- 'index ' => $ this ->index ,
137
- 'type ' => $ builder ->index ?: $ builder ->model ->searchableAs (),
126
+ 'index ' => $ builder ->index ?: $ builder ->model ->searchableAs (),
138
127
'body ' => [
139
128
'query ' => [
140
129
'bool ' => [
@@ -157,8 +146,10 @@ protected function performSearch(Builder $builder, array $options = [])
157
146
}
158
147
159
148
if (isset ($ options ['numericFilters ' ]) && count ($ options ['numericFilters ' ])) {
160
- $ params ['body ' ]['query ' ]['bool ' ]['must ' ] = array_merge ($ params ['body ' ]['query ' ]['bool ' ]['must ' ],
161
- $ options ['numericFilters ' ]);
149
+ $ params ['body ' ]['query ' ]['bool ' ]['must ' ] = array_merge (
150
+ $ params ['body ' ]['query ' ]['bool ' ]['must ' ],
151
+ $ options ['numericFilters ' ]
152
+ );
162
153
}
163
154
164
155
if ($ builder ->callback ) {
@@ -218,7 +209,8 @@ public function map($results, $model)
218
209
->pluck ('_id ' )->values ()->all ();
219
210
220
211
$ models = $ model ->whereIn (
221
- $ model ->getKeyName (), $ keys
212
+ $ model ->getKeyName (),
213
+ $ keys
222
214
)->get ()->keyBy ($ model ->getKeyName ());
223
215
224
216
return collect ($ results ['hits ' ]['hits ' ])->map (function ($ hit ) use ($ model , $ models ) {
@@ -249,7 +241,7 @@ protected function sort($builder)
249
241
return null ;
250
242
}
251
243
252
- return collect ($ builder ->orders )->map (function ($ order ) {
244
+ return collect ($ builder ->orders )->map (function ($ order ) {
253
245
return [$ order ['column ' ] => $ order ['direction ' ]];
254
246
})->toArray ();
255
247
}
0 commit comments