@@ -34,7 +34,7 @@ public function __construct($store, $softDelete = false)
34
34
/**
35
35
* Update the given model in the index.
36
36
*
37
- * @param Collection $models
37
+ * @param Collection $models
38
38
* @return void
39
39
*/
40
40
public function update ($ models )
@@ -58,7 +58,7 @@ public function update($models)
58
58
/**
59
59
* Remove the given model from the index.
60
60
*
61
- * @param Collection $models
61
+ * @param Collection $models
62
62
* @return void
63
63
*/
64
64
public function delete ($ models )
@@ -84,8 +84,8 @@ public function search(Builder $builder)
84
84
/**
85
85
* Perform the given search on the engine.
86
86
*
87
- * @param int $perPage
88
- * @param int $page
87
+ * @param int $perPage
88
+ * @param int $page
89
89
* @return mixed
90
90
*/
91
91
public function paginate (Builder $ builder , $ perPage , $ page )
@@ -99,7 +99,7 @@ public function paginate(Builder $builder, $perPage, $page)
99
99
/**
100
100
* Perform the given search on the engine.
101
101
*
102
- * @return mixed
102
+ * @return array
103
103
*/
104
104
protected function performSearch (Builder $ builder , array $ options = [])
105
105
{
@@ -108,9 +108,12 @@ protected function performSearch(Builder $builder, array $options = [])
108
108
$ matches = $ this ->store ->find ($ index , function ($ record ) use ($ builder ) {
109
109
$ values = new RecursiveIteratorIterator (new RecursiveArrayIterator ($ record ));
110
110
111
- return $ this ->matchesFilters ($ record , $ builder ->wheres ) && ! empty (array_filter (iterator_to_array ($ values , false ), function ($ value ) use ($ builder ) {
112
- return ! $ builder ->query || stripos ($ value , $ builder ->query ) !== false ;
113
- }));
111
+ return $ this ->matchesFilters ($ record , $ builder ->wheres ) &&
112
+ $ this ->matchesFilters ($ record , $ builder ->whereIns ) &&
113
+ $ this ->matchesFilters ($ record , data_get ($ builder , 'whereNotIns ' , []), true ) &&
114
+ !empty (array_filter (iterator_to_array ($ values , false ), function ($ value ) use ($ builder ) {
115
+ return !$ builder ->query || stripos ($ value , $ builder ->query ) !== false ;
116
+ }));
114
117
}, true );
115
118
116
119
$ matches = Collection::make ($ matches );
@@ -124,25 +127,46 @@ protected function performSearch(Builder $builder, array $options = [])
124
127
/**
125
128
* Determine if the given record matches given filters.
126
129
*
127
- * @param array $record
128
- * @param array $filters
130
+ * @param array $record
131
+ * @param array $filters
132
+ * @param bool $not
129
133
* @return bool
130
134
*/
131
- private function matchesFilters ($ record , $ filters )
135
+ private function matchesFilters ($ record , $ filters, $ not = false )
132
136
{
133
137
if (empty ($ filters )) {
134
138
return true ;
135
139
}
136
140
137
- return Collection::make ($ filters )->every (function ($ value , $ key ) use ($ record ) {
138
- return $ record [$ key ] === $ value ;
141
+ $ match = function ($ record , $ key , $ value ) {
142
+ if (is_array ($ value )) {
143
+ return in_array (data_get ($ record , $ key ), $ value , true );
144
+ }
145
+ return data_get ($ record , $ key ) === $ value ;
146
+ };
147
+
148
+ $ match = Collection::make ($ filters )->every (function ($ value , $ key ) use ($ match , $ record ) {
149
+ $ keyExploded = explode ('. ' , $ key );
150
+ if (count ($ keyExploded ) > 1 ) {
151
+ if (data_get ($ record , $ keyExploded [0 ]) instanceof Collection) {
152
+ return data_get ($ record , $ keyExploded [0 ])->contains (function ($ subRecord ) use ($ match , $ keyExploded , $ value ) {
153
+ return $ match ($ subRecord , $ keyExploded [1 ], $ value );
154
+ });
155
+ }
156
+
157
+ return $ match ($ record , $ keyExploded , $ value );
158
+ }
159
+
160
+ return $ match ($ record , $ key , $ value );
139
161
});
162
+
163
+ return $ not ? !$ match : $ match ;
140
164
}
141
165
142
166
/**
143
167
* Pluck and return the primary keys of the given results.
144
168
*
145
- * @param mixed $results
169
+ * @param mixed $results
146
170
* @return \Illuminate\Support\Collection
147
171
*/
148
172
public function mapIds ($ results )
@@ -153,8 +177,8 @@ public function mapIds($results)
153
177
/**
154
178
* Map the given results to instances of the given model.
155
179
*
156
- * @param mixed $results
157
- * @param \Illuminate\Database\Eloquent\Model $model
180
+ * @param mixed $results
181
+ * @param \Illuminate\Database\Eloquent\Model $model
158
182
* @return Collection
159
183
*/
160
184
public function map (Builder $ builder , $ results , $ model )
@@ -177,8 +201,8 @@ public function map(Builder $builder, $results, $model)
177
201
/**
178
202
* Map the given results to instances of the given model via a lazy collection.
179
203
*
180
- * @param mixed $results
181
- * @param \Illuminate\Database\Eloquent\Model $model
204
+ * @param mixed $results
205
+ * @param \Illuminate\Database\Eloquent\Model $model
182
206
* @return \Illuminate\Support\LazyCollection
183
207
*/
184
208
public function lazyMap (Builder $ builder , $ results , $ model )
@@ -203,7 +227,7 @@ public function lazyMap(Builder $builder, $results, $model)
203
227
/**
204
228
* Get the total count from a raw result returned by the engine.
205
229
*
206
- * @param mixed $results
230
+ * @param mixed $results
207
231
* @return int
208
232
*/
209
233
public function getTotalCount ($ results )
@@ -214,7 +238,7 @@ public function getTotalCount($results)
214
238
/**
215
239
* Flush all of the model's records from the engine.
216
240
*
217
- * @param \Illuminate\Database\Eloquent\Model $model
241
+ * @param \Illuminate\Database\Eloquent\Model $model
218
242
* @return void
219
243
*/
220
244
public function flush ($ model )
@@ -225,7 +249,7 @@ public function flush($model)
225
249
/**
226
250
* Create a search index.
227
251
*
228
- * @param string $name
252
+ * @param string $name
229
253
* @return mixed
230
254
*/
231
255
public function createIndex ($ name , array $ options = [])
@@ -236,7 +260,7 @@ public function createIndex($name, array $options = [])
236
260
/**
237
261
* Delete a search index.
238
262
*
239
- * @param string $name
263
+ * @param string $name
240
264
* @return mixed
241
265
*/
242
266
public function deleteIndex ($ name )
@@ -247,11 +271,25 @@ public function deleteIndex($name)
247
271
/**
248
272
* Determine if the given model uses soft deletes.
249
273
*
250
- * @param \Illuminate\Database\Eloquent\Model $model
274
+ * @param \Illuminate\Database\Eloquent\Model $model
251
275
* @return bool
252
276
*/
253
277
protected function usesSoftDelete ($ model )
254
278
{
255
279
return in_array (SoftDeletes::class, class_uses_recursive ($ model ));
256
280
}
281
+
282
+ protected function buildSearchQuery (Builder $ builder )
283
+ {
284
+ $ query = $ this ->initializeSearchQuery (
285
+ $ builder ,
286
+ array_keys ($ builder ->model ->toSearchableArray ()),
287
+ $ this ->getPrefixColumns ($ builder ),
288
+ $ this ->getFullTextColumns ($ builder )
289
+ );
290
+
291
+ return $ this ->constrainForSoftDeletes (
292
+ $ builder , $ this ->addAdditionalConstraints ($ builder , $ query ->take ($ builder ->limit ))
293
+ );
294
+ }
257
295
}
0 commit comments