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