1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchDSL\Query; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\BuilderInterface; |
15
|
|
|
use ONGR\ElasticsearchDSL\ParametersTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Represents Elasticsearch "bool" query. |
19
|
|
|
* |
20
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html |
21
|
|
|
*/ |
22
|
|
|
class BoolQuery implements BuilderInterface |
23
|
|
|
{ |
24
|
|
|
use ParametersTrait; |
25
|
|
|
|
26
|
|
|
const MUST = 'must'; |
27
|
|
|
const MUST_NOT = 'must_not'; |
28
|
|
|
const SHOULD = 'should'; |
29
|
|
|
const FILTER = 'filter'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $container = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor to prepare container. |
38
|
|
|
*/ |
39
|
|
|
public function __construct() |
40
|
|
|
{ |
41
|
|
|
$this->container = []; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param null $boolType |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function getQueries($boolType = null) |
49
|
|
|
{ |
50
|
|
|
if ($boolType === null) { |
51
|
|
|
$queries = []; |
52
|
|
|
|
53
|
|
|
foreach ($this->container as $item) { |
54
|
|
|
$queries = array_merge($queries, $item); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $queries; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->container[$boolType]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Add BuilderInterface object to bool operator. |
65
|
|
|
* |
66
|
|
|
* @param BuilderInterface $query Query add to the bool. |
67
|
|
|
* @param string $type Bool type. Example: must, must_not, should. |
68
|
|
|
* @param string $key Key that indicates a builder id. |
69
|
|
|
* |
70
|
|
|
* @return string Key of added builder. |
71
|
|
|
* |
72
|
|
|
* @throws \UnexpectedValueException |
73
|
|
|
*/ |
74
|
|
|
public function add(BuilderInterface $query, $type = self::MUST, $key = null) |
75
|
|
|
{ |
76
|
|
|
if (!in_array($type, [self::MUST, self::MUST_NOT, self::SHOULD, self::FILTER])) { |
77
|
|
|
throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!$key) { |
|
|
|
|
81
|
|
|
$key = uniqid(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->container[$type][$key] = $query; |
85
|
|
|
|
86
|
|
|
return $key; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function toArray() |
93
|
|
|
{ |
94
|
|
|
if (count($this->container) === 1 && isset($this->container[self::MUST]) |
95
|
|
|
&& count($this->container[self::MUST]) === 1) { |
96
|
|
|
$query = reset($this->container[self::MUST]); |
97
|
|
|
|
98
|
|
|
return $query->toArray(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$output = []; |
102
|
|
|
|
103
|
|
|
foreach ($this->container as $boolType => $builders) { |
104
|
|
|
/** @var BuilderInterface $builder */ |
105
|
|
|
foreach ($builders as $builder) { |
106
|
|
|
$output[$boolType][] = $builder->toArray(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return [$this->getType() => $output]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function getType() |
117
|
|
|
{ |
118
|
|
|
return 'bool'; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: