diff --git a/app/Elasticsearch/Repositories/ItemRepository.php b/app/Elasticsearch/Repositories/ItemRepository.php index 84ab3dc74..1559c002d 100644 --- a/app/Elasticsearch/Repositories/ItemRepository.php +++ b/app/Elasticsearch/Repositories/ItemRepository.php @@ -128,7 +128,10 @@ public function getSimilar(int $size, Model $model, $locale = null): SearchResul [ 'term' => ['has_iip' => true] ] - ] + ], + 'filter' => [ + ['term' => ['frontend' => config('app.frontend')]], + ], ] ]; diff --git a/app/Http/Controllers/Api/V2/ItemController.php b/app/Http/Controllers/Api/V2/ItemController.php index 22458e7ea..d31c12f5b 100644 --- a/app/Http/Controllers/Api/V2/ItemController.php +++ b/app/Http/Controllers/Api/V2/ItemController.php @@ -44,4 +44,18 @@ public function related(string $id) ->get(); return ItemResource::collection($related); } + + public function similar(string $id) + { + $size = request()->get('size', 1); + $item = Item::findOrFail($id); + $ids = $this->itemRepository + ->getSimilar($size, $item) + ->getCollection() + ->pluck('id'); + $similar = Item::with(['images', 'authorities', 'translations']) + ->whereIn('id', $ids) + ->get(); + return ItemResource::collection($similar); + } } diff --git a/routes/api.php b/routes/api.php index 2c53ab23c..3df355b0d 100644 --- a/routes/api.php +++ b/routes/api.php @@ -36,5 +36,6 @@ Route::prefix('v2')->group(function () { Route::get('items/suggestions', [V2ItemController::class, 'suggestions']); Route::get('items/{id}/related', [V2ItemController::class, 'related']); + Route::get('items/{id}/similar', [V2ItemController::class, 'similar']); Route::get('items/{id}', [V2ItemController::class, 'show']); });