|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Shopware\Elasticsearch\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use Shopware\Core\Framework\Api\Serializer\JsonEntityEncoder; |
|
6
|
|
|
use Shopware\Core\Framework\Context; |
|
7
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry; |
|
8
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Entity; |
|
9
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection; |
|
10
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; |
|
11
|
|
|
use Shopware\Core\Framework\Log\Package; |
|
12
|
|
|
use Shopware\Core\Framework\Routing\Annotation\Since; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @internal |
|
20
|
|
|
*/ |
|
21
|
|
|
#[Package('system-settings')] |
|
22
|
|
|
final class AdminSearchController |
|
23
|
|
|
{ |
|
24
|
|
|
private AdminSearcher $searcher; |
|
25
|
|
|
|
|
26
|
|
|
private JsonEntityEncoder $entityEncoder; |
|
27
|
|
|
|
|
28
|
|
|
private DefinitionInstanceRegistry $definitionRegistry; |
|
29
|
|
|
|
|
30
|
|
|
private AdminElasticsearchHelper $adminEsHelper; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct( |
|
33
|
|
|
AdminSearcher $searcher, |
|
34
|
|
|
DefinitionInstanceRegistry $definitionRegistry, |
|
35
|
|
|
JsonEntityEncoder $entityEncoder, |
|
36
|
|
|
AdminElasticsearchHelper $adminEsHelper |
|
37
|
|
|
) { |
|
38
|
|
|
$this->searcher = $searcher; |
|
39
|
|
|
$this->definitionRegistry = $definitionRegistry; |
|
40
|
|
|
$this->entityEncoder = $entityEncoder; |
|
41
|
|
|
$this->adminEsHelper = $adminEsHelper; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @Since("6.4.19.0") |
|
46
|
|
|
* @Route("/api/_admin/es-search", name="api.admin.es-search", methods={"POST"}, defaults={"_routeScope"={"administration"}}) |
|
47
|
|
|
*/ |
|
48
|
|
|
public function elastic(Request $request, Context $context): Response |
|
49
|
|
|
{ |
|
50
|
|
|
if ($this->adminEsHelper->getEnabled() === false) { |
|
51
|
|
|
throw new \RuntimeException('Admin elasticsearch is not enabled'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$term = trim($request->get('term', '')); |
|
55
|
|
|
$entities = $request->request->all('entities'); |
|
56
|
|
|
|
|
57
|
|
|
if (empty($term)) { |
|
58
|
|
|
throw new \RuntimeException('Search term is empty'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$limit = $request->get('limit', 10); |
|
62
|
|
|
|
|
63
|
|
|
$results = $this->searcher->search($term, $entities, $context, $limit); |
|
64
|
|
|
|
|
65
|
|
|
foreach ($results as $entityName => $result) { |
|
66
|
|
|
$definition = $this->definitionRegistry->getByEntityName($entityName); |
|
67
|
|
|
|
|
68
|
|
|
/** @var EntityCollection<Entity> $entityCollection */ |
|
69
|
|
|
$entityCollection = $result['data']; |
|
70
|
|
|
$entities = []; |
|
71
|
|
|
|
|
72
|
|
|
foreach ($entityCollection->getElements() as $key => $entity) { |
|
73
|
|
|
$entities[$key] = $this->entityEncoder->encode(new Criteria(), $definition, $entity, '/api'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$results[$entityName]['data'] = $entities; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return new JsonResponse(['data' => $results]); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|