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
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Represents Elasticsearch "filtered" query. |
18
|
|
|
* |
19
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html |
20
|
|
|
*/ |
21
|
|
|
class FilteredQuery implements BuilderInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var BuilderInterface |
25
|
|
|
*/ |
26
|
|
|
private $query; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var BuilderInterface |
30
|
|
|
*/ |
31
|
|
|
private $filter; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $boolFilter; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param BuilderInterface $query |
40
|
|
|
* @param BuilderInterface $filter |
41
|
|
|
*/ |
42
|
|
|
public function __construct(BuilderInterface $query = null, BuilderInterface $filter = null) |
43
|
|
|
{ |
44
|
|
|
$this->query = $query; |
45
|
|
|
$this->filter = $filter; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* adds a query to join with AND operator |
50
|
|
|
* |
51
|
|
|
* @param BuilderInterface $must |
52
|
|
|
* @param BuilderInterface $should |
53
|
|
|
* @param BuilderInterface $mustNot |
54
|
|
|
*/ |
55
|
|
|
public function addBoolFilter( |
56
|
|
|
BuilderInterface $must = null, |
57
|
|
|
BuilderInterface $should = null, |
58
|
|
|
BuilderInterface $mustNot = null |
59
|
|
|
) { |
60
|
|
|
if (!$must && !$should && !$mustNot) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
$filter = []; |
64
|
|
|
if ($must) { |
65
|
|
|
$filter['bool']['must'] = $must->toArray(); |
66
|
|
|
} |
67
|
|
|
if ($should) { |
68
|
|
|
$filter['bool']['should'] = $should->toArray(); |
69
|
|
|
} |
70
|
|
|
if ($mustNot) { |
71
|
|
|
$filter['bool']['should'] = $should->toArray(); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->boolFilter = $filter; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function getType() |
81
|
|
|
{ |
82
|
|
|
return 'filtered'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function toArray() |
89
|
|
|
{ |
90
|
|
|
$output = []; |
91
|
|
|
|
92
|
|
|
if (isset($this->query)) { |
93
|
|
|
$output['query'] = $this->query->toArray(); |
94
|
|
|
} |
95
|
|
|
if (isset($this->filter) || isset($this->boolFilter)) { |
96
|
|
|
$output['filter'] = isset($this->filter) ? $this->filter->toArray() : $this->boolFilter; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return [$this->getType() => $output]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: