1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitBag\SyliusElasticsearchPlugin\QueryBuilder; |
6
|
|
|
|
7
|
|
|
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\SearchPropertyNameResolverRegistryInterface; |
8
|
|
|
use Elastica\Query\AbstractQuery; |
9
|
|
|
use Elastica\Query\BoolQuery; |
10
|
|
|
use Elastica\Query\MultiMatch; |
11
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
12
|
|
|
|
13
|
|
|
final class SearchProductsQueryBuilder implements QueryBuilderInterface |
14
|
|
|
{ |
15
|
|
|
public const QUERY_KEY = 'query'; |
16
|
|
|
|
17
|
|
|
/** @var SearchPropertyNameResolverRegistryInterface */ |
18
|
|
|
private $searchProperyNameResolverRegistry; |
19
|
|
|
|
20
|
|
|
/** @var LocaleContextInterface */ |
21
|
|
|
private $localeContext; |
22
|
|
|
|
23
|
|
|
/** @var QueryBuilderInterface */ |
24
|
|
|
private $isEnabledQueryBuilder; |
25
|
|
|
|
26
|
|
|
/** @var QueryBuilderInterface */ |
27
|
|
|
private $hasChannelQueryBuilder; |
28
|
|
|
|
29
|
|
|
public function __construct( |
30
|
|
|
SearchPropertyNameResolverRegistryInterface $searchProperyNameResolverRegistry, |
31
|
|
|
LocaleContextInterface $localeContext, |
32
|
|
|
QueryBuilderInterface $isEnabledQueryBuilder, |
33
|
|
|
QueryBuilderInterface $hasChannelQueryBuilder |
34
|
|
|
) { |
35
|
|
|
$this->searchProperyNameResolverRegistry = $searchProperyNameResolverRegistry; |
36
|
|
|
$this->localeContext = $localeContext; |
37
|
|
|
$this->isEnabledQueryBuilder = $isEnabledQueryBuilder; |
38
|
|
|
$this->hasChannelQueryBuilder = $hasChannelQueryBuilder; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function buildQuery(array $data): ?AbstractQuery |
42
|
|
|
{ |
43
|
|
|
if (!array_key_exists(self::QUERY_KEY, $data)) { |
44
|
|
|
throw new \RuntimeException( |
45
|
|
|
sprintf( |
46
|
|
|
'Could not build search products query because there\'s no "query" key in provided data. ' . |
47
|
|
|
'Got the following keys: %s', |
48
|
|
|
implode(', ', array_keys($data)) |
49
|
|
|
) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
$query = $data[self::QUERY_KEY]; |
53
|
|
|
if (!is_string($query)) { |
54
|
|
|
throw new \RuntimeException( |
55
|
|
|
sprintf( |
56
|
|
|
'Could not build search products query because the provided "query" is expected to be a string ' . |
57
|
|
|
'but "%s" is given.', |
58
|
|
|
is_object($query) ? get_class($query) : gettype($query) |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$multiMatch = new MultiMatch(); |
64
|
|
|
$multiMatch->setQuery($query); |
65
|
|
|
$multiMatch->setFuzziness('AUTO'); |
66
|
|
|
$fields = []; |
67
|
|
|
foreach ($this->searchProperyNameResolverRegistry->getPropertyNameResolvers() as $propertyNameResolver) { |
68
|
|
|
$fields[] = $propertyNameResolver->resolvePropertyName($this->localeContext->getLocaleCode()); |
69
|
|
|
} |
70
|
|
|
$multiMatch->setFields($fields); |
71
|
|
|
$bool = new BoolQuery(); |
72
|
|
|
$bool->addMust($multiMatch); |
73
|
|
|
$bool->addFilter($this->isEnabledQueryBuilder->buildQuery([])); |
|
|
|
|
74
|
|
|
$bool->addFilter($this->hasChannelQueryBuilder->buildQuery([])); |
75
|
|
|
return $bool; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|