1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nord\Lumen\Elasticsearch\Search\Query\TermLevel; |
4
|
|
|
|
5
|
|
|
use Nord\Lumen\Elasticsearch\Exceptions\InvalidArgument; |
6
|
|
|
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasBoost; |
7
|
|
|
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasValue; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class RegexpQuery |
11
|
|
|
* @package Nord\Lumen\Elasticsearch\Search\Query\TermLevel |
12
|
|
|
* |
13
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/2.4/query-dsl-regexp-query.html |
14
|
|
|
*/ |
15
|
|
|
class RegexpQuery extends AbstractQuery |
16
|
|
|
{ |
17
|
|
|
use HasBoost; |
18
|
|
|
use HasValue; |
19
|
|
|
|
20
|
|
|
const FLAG_ALL = 'ALL'; |
21
|
|
|
const FLAG_ANYSTRING = 'ANYSTRING'; |
22
|
|
|
const FLAG_COMPLEMENT = 'COMPLEMENT'; |
23
|
|
|
const FLAG_EMPTY = 'EMPTY'; |
24
|
|
|
const FLAG_INTERSECTION = 'INTERSECTION'; |
25
|
|
|
const FLAG_INTERVAL = 'INTERVAL'; |
26
|
|
|
const FLAG_NONE = 'NONE'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $flags = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $maxDeterminizedStates; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
* |
41
|
|
|
* @throws InvalidArgument |
42
|
|
|
*/ |
43
|
|
|
public function toArray() |
44
|
|
|
{ |
45
|
|
|
if (!isset($this->field) || !isset($this->value)) { |
46
|
|
|
throw new InvalidArgument('"field" and "value" must be set for this type of query'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$definition = [ |
50
|
|
|
'value' => $this->getValue(), |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
if ($this->hasBoost()) { |
54
|
|
|
$definition['boost'] = $this->getBoost(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!empty($this->flags)) { |
58
|
|
|
$definition['flags'] = $this->getFlagsValue(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($this->maxDeterminizedStates !== null) { |
62
|
|
|
$definition['max_determinized_states'] = $this->maxDeterminizedStates; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return [ |
66
|
|
|
'regexp' => [ |
67
|
|
|
$this->getField() => $definition, |
68
|
|
|
], |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function getFlags() |
76
|
|
|
{ |
77
|
|
|
return $this->flags; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array $flags |
82
|
|
|
* |
83
|
|
|
* @return RegexpQuery |
84
|
|
|
*/ |
85
|
|
|
public function setFlags($flags) |
86
|
|
|
{ |
87
|
|
|
$this->flags = $flags; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function getFlagsValue() |
96
|
|
|
{ |
97
|
|
|
return implode('|', $this->flags); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return int |
102
|
|
|
*/ |
103
|
|
|
public function getMaxDeterminizedStates() |
104
|
|
|
{ |
105
|
|
|
return $this->maxDeterminizedStates; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param int $maxDeterminizedStates |
110
|
|
|
* |
111
|
|
|
* @return RegexpQuery |
112
|
|
|
* |
113
|
|
|
* @throws InvalidArgument |
114
|
|
|
*/ |
115
|
|
|
public function setMaxDeterminizedStates($maxDeterminizedStates) |
116
|
|
|
{ |
117
|
|
|
if (!is_int($maxDeterminizedStates)) { |
118
|
|
|
throw new InvalidArgument('max_determinized_states must be an integer'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->maxDeterminizedStates = $maxDeterminizedStates; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|