Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
TokenCountRouter | |
0.00% |
0 / 22 |
|
0.00% |
0 / 6 |
240 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
setText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setFallback | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setField | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setAnalyzer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addCondition | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
72 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Extra\Query; |
4 | |
5 | use Elastica\Query\AbstractQuery; |
6 | |
7 | /** |
8 | * TokenCountRouter query used to trigger a particular query by counting |
9 | * the number of tokens in the user query. |
10 | * |
11 | * @link https://github.com/wikimedia/search-extra/blob/master/docs/token_count_router.md |
12 | * |
13 | * This program is free software; you can redistribute it and/or modify |
14 | * it under the terms of the GNU General Public License as published by |
15 | * the Free Software Foundation; either version 2 of the License, or |
16 | * (at your option) any later version. |
17 | * |
18 | * This program is distributed in the hope that it will be useful, |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21 | * GNU General Public License for more details. |
22 | * |
23 | * You should have received a copy of the GNU General Public License along |
24 | * with this program; if not, write to the Free Software Foundation, Inc., |
25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
26 | * http://www.gnu.org/copyleft/gpl.html |
27 | */ |
28 | |
29 | class TokenCountRouter extends AbstractQuery { |
30 | /** |
31 | * @const string greater than |
32 | */ |
33 | public const GT = 'gt'; |
34 | |
35 | /** |
36 | * @const string greater or equal |
37 | */ |
38 | public const GTE = 'gte'; |
39 | |
40 | /** |
41 | * @const string equal |
42 | */ |
43 | public const EQ = 'eq'; |
44 | |
45 | /** |
46 | * @const string not equal |
47 | */ |
48 | public const NEQ = 'neq'; |
49 | |
50 | /** |
51 | * @const string less than or equal |
52 | */ |
53 | public const LTE = 'lte'; |
54 | |
55 | /** |
56 | * @const string less than |
57 | */ |
58 | public const LT = 'lt'; |
59 | |
60 | /** |
61 | * @param string $text the text to analyze |
62 | * @param AbstractQuery $fallbackQuery the query to run when no |
63 | * conditions match |
64 | * @param string|null $field use the analyzer of this field |
65 | * @param string|null $analyzer use this analyzer |
66 | */ |
67 | public function __construct( $text, AbstractQuery $fallbackQuery, $field = null, $analyzer = null ) { |
68 | $this->setText( $text ); |
69 | $this->setFallback( $fallbackQuery ); |
70 | if ( $field ) { |
71 | $this->setField( $field ); |
72 | } |
73 | if ( $analyzer ) { |
74 | $this->setAnalyzer( $analyzer ); |
75 | } |
76 | } |
77 | |
78 | /** |
79 | * @param string $text count tokens from this text |
80 | * @return self |
81 | */ |
82 | public function setText( $text ) { |
83 | return $this->setParam( 'text', $text ); |
84 | } |
85 | |
86 | /** |
87 | * @param AbstractQuery $query |
88 | * @return self |
89 | */ |
90 | public function setFallback( AbstractQuery $query ) { |
91 | return $this->setParam( 'fallback', $query ); |
92 | } |
93 | |
94 | /** |
95 | * @param string $field the field to fetch analyzer info |
96 | * @return self |
97 | */ |
98 | public function setField( $field ) { |
99 | return $this->setParam( 'field', $field ); |
100 | } |
101 | |
102 | /** |
103 | * @param string $analyzer the field to fetch analyzer info |
104 | * @return self |
105 | */ |
106 | public function setAnalyzer( $analyzer ) { |
107 | return $this->setParam( 'analyzer', $analyzer ); |
108 | } |
109 | |
110 | /** |
111 | * Adds a new condition |
112 | * The first condition that evaluates to true is applied. |
113 | * If none match the fallback query is applied. |
114 | * |
115 | * @param string $type the condition to apply |
116 | * @param int $value the value to compare |
117 | * @param AbstractQuery $query the query to run if the condition is |
118 | * true ignoring all remaining conditions |
119 | * @return self |
120 | */ |
121 | public function addCondition( $type, $value, AbstractQuery $query ) { |
122 | switch ( $type ) { |
123 | case self::GT: |
124 | case self::GTE: |
125 | case self::EQ: |
126 | case self::NEQ: |
127 | case self::LT: |
128 | case self::LTE: |
129 | break; |
130 | default: |
131 | throw new \InvalidArgumentException( "$type is not allowed as a condition type" ); |
132 | } |
133 | return $this->addParam( 'conditions', [ |
134 | $type => $value, |
135 | 'query' => $query, |
136 | ] ); |
137 | } |
138 | } |