1 | <?php |
||
10 | class TNTSearchEngine extends Engine |
||
11 | { |
||
12 | /** |
||
13 | * @var TNTSearch |
||
14 | */ |
||
15 | protected $tnt; |
||
16 | |||
17 | /** |
||
18 | * Create a new engine instance. |
||
19 | * |
||
20 | * @param TNTSearch $tnt |
||
21 | */ |
||
22 | public function __construct(TNTSearch $tnt) |
||
26 | |||
27 | /** |
||
28 | * Update the given model in the index. |
||
29 | * |
||
30 | * @param Collection $models |
||
31 | * |
||
32 | * @return void |
||
33 | */ |
||
34 | public function update($models) |
||
35 | { |
||
36 | $this->initIndex($models->first()); |
||
37 | $this->tnt->selectIndex("{$models->first()->searchableAs()}.index"); |
||
38 | $index = $this->tnt->getIndex(); |
||
39 | $index->setPrimaryKey($models->first()->getKeyName()); |
||
40 | |||
41 | $index->indexBeginTransaction(); |
||
42 | $models->each(function ($model) use ($index) { |
||
43 | if ($model->getKey()) { |
||
44 | $index->update($model->getKey(), $model->toSearchableArray()); |
||
45 | } else { |
||
46 | $index->insert($model->toSearchableArray()); |
||
47 | } |
||
48 | }); |
||
49 | $index->indexEndTransaction(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Remove the given model from the index. |
||
54 | * |
||
55 | * @param Collection $models |
||
56 | * |
||
57 | * @return void |
||
58 | */ |
||
59 | public function delete($models) |
||
69 | |||
70 | /** |
||
71 | * Perform the given search on the engine. |
||
72 | * |
||
73 | * @param Builder $builder |
||
74 | * |
||
75 | * @return mixed |
||
76 | */ |
||
77 | public function search(Builder $builder) |
||
81 | |||
82 | /** |
||
83 | * Perform the given search on the engine. |
||
84 | * |
||
85 | * @param Builder $builder |
||
86 | * @param int $perPage |
||
87 | * @param int $page |
||
88 | * |
||
89 | * @return mixed |
||
90 | */ |
||
91 | public function paginate(Builder $builder, $perPage, $page) |
||
111 | |||
112 | /** |
||
113 | * Perform the given search on the engine. |
||
114 | * |
||
115 | * @param Builder $builder |
||
116 | * |
||
117 | * @return mixed |
||
118 | */ |
||
119 | protected function performSearch(Builder $builder) |
||
127 | |||
128 | /** |
||
129 | * Get the filter array for the query. |
||
130 | * |
||
131 | * @param Builder $builder |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | protected function filters(Builder $builder) |
||
141 | |||
142 | /** |
||
143 | * Map the given results to instances of the given model. |
||
144 | * |
||
145 | * @param mixed $results |
||
146 | * @param \Illuminate\Database\Eloquent\Model $model |
||
147 | * |
||
148 | * @return Collection |
||
149 | */ |
||
150 | public function map($results, $model) |
||
165 | |||
166 | /** |
||
167 | * Pluck and return the primary keys of the given results. |
||
168 | * |
||
169 | * @param mixed $results |
||
170 | * @return \Illuminate\Support\Collection |
||
171 | */ |
||
172 | public function mapIds($results) |
||
176 | |||
177 | /** |
||
178 | * Get the total count from a raw result returned by the engine. |
||
179 | * |
||
180 | * @param mixed $results |
||
181 | * |
||
182 | * @return int |
||
183 | */ |
||
184 | public function getTotalCount($results) |
||
188 | |||
189 | public function initIndex($model) |
||
203 | } |
||
204 |