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