|
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; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Uses the Terms Query Parser. |
|
18
|
|
|
* |
|
19
|
|
|
* May be more efficient in some cases than using |
|
20
|
|
|
* the Standard Query Parser to generate a boolean |
|
21
|
|
|
* query since the default implementation method avoids scoring. |
|
22
|
|
|
* |
|
23
|
|
|
* @see https://lucene.apache.org/solr/guide/8_4/other-parsers.html#term-query-parser |
|
24
|
|
|
*/ |
|
25
|
|
|
final class Terms |
|
26
|
|
|
{ |
|
27
|
|
|
private $params = [ |
|
28
|
|
|
'f' => null, |
|
29
|
|
|
'method' => null, |
|
30
|
|
|
'separator' => null, |
|
31
|
|
|
'cache' => null, |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
private $values; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(string $field, array $values) |
|
37
|
|
|
{ |
|
38
|
|
|
if ('' === $field) { |
|
39
|
|
|
throw new \InvalidArgumentException('The "field" parameter can not be empty.'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if ([] === $values) { |
|
43
|
|
|
throw new \InvalidArgumentException('The "values" parameter can not be empty.'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->params['f'] = $field; |
|
47
|
|
|
$this->values = $values; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function __toString(): string |
|
51
|
|
|
{ |
|
52
|
|
|
$params = $this->params; |
|
53
|
|
|
|
|
54
|
|
|
if (null !== $params['separator']) { |
|
55
|
|
|
$params['separator'] = sprintf('"%s"', addslashes($separator = $params['separator'])); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return sprintf('{!terms %s}%s', urldecode(http_build_query($params, '', ' ')), implode($separator ?? ',', $this->values)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public static function create(string $field, array $values): self |
|
62
|
|
|
{ |
|
63
|
|
|
return new self($field, $values); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function separator(string $separator): self |
|
67
|
|
|
{ |
|
68
|
|
|
$terms = clone $this; |
|
69
|
|
|
$terms->params['separator'] = $separator; |
|
70
|
|
|
|
|
71
|
|
|
return $terms; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function method(string $method): self |
|
75
|
|
|
{ |
|
76
|
|
|
if (!\in_array($method, $available = ['termsFilter', 'booleanQuery', 'automaton', 'docValuesTermsFilter'], true)) { |
|
77
|
|
|
throw new \InvalidArgumentException(sprintf('Available methods are: "%s"', implode('", "', $available))); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$terms = clone $this; |
|
81
|
|
|
$terms->params['method'] = $method; |
|
82
|
|
|
|
|
83
|
|
|
return $terms; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function cache(bool $cache): self |
|
87
|
|
|
{ |
|
88
|
|
|
$terms = clone $this; |
|
89
|
|
|
$terms->params['cache'] = $cache ? 'true' : 'false'; |
|
90
|
|
|
|
|
91
|
|
|
return $terms; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|