1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Solr Client Symfony package. |
7
|
|
|
* |
8
|
|
|
* (c) ingatlan.com Zrt. <[email protected]> |
9
|
|
|
* |
10
|
|
|
* This source file is subject to the MIT license that is bundled |
11
|
|
|
* with this source code in the file LICENSE. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace iCom\SolrClient\Query\Helper; |
15
|
|
|
|
16
|
|
|
use iCom\SolrClient\Query\QueryHelper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Uses the Terms Query Parser. |
20
|
|
|
* |
21
|
|
|
* May be more efficient in some cases than using |
22
|
|
|
* the Standard Query Parser to generate a boolean |
23
|
|
|
* query since the default implementation method avoids scoring. |
24
|
|
|
* |
25
|
|
|
* @see https://lucene.apache.org/solr/guide/8_4/other-parsers.html#term-query-parser |
26
|
|
|
*/ |
27
|
|
|
final class Terms implements QueryHelper |
28
|
|
|
{ |
29
|
|
|
private $params = [ |
30
|
|
|
'f' => null, |
31
|
|
|
'method' => null, |
32
|
|
|
'separator' => null, |
33
|
|
|
'cache' => null, |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
private $values; |
37
|
|
|
|
38
|
|
|
public function __construct(string $field, array $values) |
39
|
|
|
{ |
40
|
|
|
if ('' === $field) { |
41
|
|
|
throw new \InvalidArgumentException('The "field" parameter can not be empty.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ([] === $values) { |
45
|
|
|
throw new \InvalidArgumentException('The "values" parameter can not be empty.'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->params['f'] = $field; |
49
|
|
|
$this->values = $values; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function create(string $field, array $values): self |
53
|
|
|
{ |
54
|
|
|
return new self($field, $values); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function separator(string $separator): self |
58
|
|
|
{ |
59
|
|
|
$terms = clone $this; |
60
|
|
|
$terms->params['separator'] = $separator; |
61
|
|
|
|
62
|
|
|
return $terms; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function method(string $method): self |
66
|
|
|
{ |
67
|
|
|
if (!\in_array($method, $available = ['termsFilter', 'booleanQuery', 'automaton', 'docValuesTermsFilter'], true)) { |
68
|
|
|
throw new \InvalidArgumentException(sprintf('Available methods are: "%s"', implode('", "', $available))); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$terms = clone $this; |
72
|
|
|
$terms->params['method'] = $method; |
73
|
|
|
|
74
|
|
|
return $terms; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function cache(bool $cache): self |
78
|
|
|
{ |
79
|
|
|
$terms = clone $this; |
80
|
|
|
$terms->params['cache'] = $cache ? 'true' : 'false'; |
81
|
|
|
|
82
|
|
|
return $terms; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function toString(): string |
86
|
|
|
{ |
87
|
|
|
$params = $this->params; |
88
|
|
|
|
89
|
|
|
if (null !== $params['separator']) { |
90
|
|
|
$params['separator'] = sprintf('"%s"', addslashes($separator = $params['separator'])); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return sprintf('{!terms %s}%s', urldecode(http_build_query($params, '', ' ')), implode($separator ?? ',', $this->values)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|