|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DoctrineElastic\Query\Walker\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use DoctrineElastic\Exception\InvalidOperatorException; |
|
6
|
|
|
use DoctrineElastic\Query\Walker\OperatorsMap; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Helper for this extension query walkers |
|
10
|
|
|
* |
|
11
|
|
|
* @author Ands |
|
12
|
|
|
*/ |
|
13
|
|
|
class WalkerHelper { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param array $subBody |
|
17
|
|
|
* @param array $body |
|
18
|
|
|
* @param string $toStatement |
|
19
|
|
|
*/ |
|
20
|
|
|
public function addSubQueryStatement(array $subBody, array &$body, $toStatement = 'must') { |
|
21
|
|
|
if (isset($body['query']['bool'][$toStatement])) { |
|
22
|
|
|
$body['query']['bool'][$toStatement][] = $subBody; |
|
23
|
|
|
} else { |
|
24
|
|
|
$tempBody = array( |
|
25
|
|
|
'query' => array( |
|
26
|
|
|
'bool' => array( |
|
27
|
|
|
$toStatement => [$subBody] |
|
28
|
|
|
) |
|
29
|
|
|
) |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
$body = array_merge_recursive($body, $tempBody); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function addBodyStatement($field, $operator, $value, array &$body) { |
|
37
|
|
|
$bodyTemp = array( |
|
38
|
|
|
'query' => array( |
|
39
|
|
|
'bool' => array( |
|
40
|
|
|
'must' => [], |
|
41
|
|
|
'must_not' => [], |
|
42
|
|
|
'should' => [], |
|
43
|
|
|
'should_not' => [], |
|
44
|
|
|
'filter' => [] |
|
45
|
|
|
), |
|
46
|
|
|
|
|
47
|
|
|
) |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
if (OperatorsMap::isRangeOperator($operator)) { |
|
51
|
|
|
$opStr = OperatorsMap::$mapElastic[$operator]; |
|
52
|
|
|
$bodyTemp['query']['bool']['filter'][] = array( |
|
53
|
|
|
'range' => array( |
|
54
|
|
|
$field => array( |
|
55
|
|
|
$opStr => $value |
|
56
|
|
|
) |
|
57
|
|
|
) |
|
58
|
|
|
); |
|
59
|
|
|
} else { |
|
60
|
|
|
switch ($operator) { |
|
61
|
|
|
case OperatorsMap::EQ: |
|
62
|
|
|
case OperatorsMap::NEQ: |
|
63
|
|
|
if (is_null($value)) { |
|
64
|
|
|
$filterField = ($operator == OperatorsMap::EQ) ? 'missing' : 'exists'; |
|
65
|
|
|
$bodyTemp['query']['bool']['filter'][] = array( |
|
66
|
|
|
$filterField => ['field' => $field] |
|
67
|
|
|
); |
|
68
|
|
|
} else { |
|
69
|
|
|
$boolField = 'must' . ($operator == OperatorsMap::EQ ? '' : '_not'); |
|
70
|
|
|
$itemSearch = array( |
|
71
|
|
|
'match' => array( |
|
72
|
|
|
$field => array( |
|
73
|
|
|
'query' => $value, |
|
74
|
|
|
'operator' => 'AND' |
|
75
|
|
|
) |
|
76
|
|
|
) |
|
77
|
|
|
); |
|
78
|
|
|
$bodyTemp['query']['bool'][$boolField][] = $itemSearch; |
|
79
|
|
|
} |
|
80
|
|
|
break; |
|
81
|
|
|
case OperatorsMap::UNLIKE: |
|
82
|
|
|
case OperatorsMap::LIKE: |
|
83
|
|
|
$boolField = 'must' . ($operator == OperatorsMap::LIKE ? '' : '_not'); |
|
84
|
|
|
$value = str_replace('%', '*', $value); |
|
85
|
|
|
$itemSearch = array( |
|
86
|
|
|
'wildcard' => array( |
|
87
|
|
|
$field => $value |
|
88
|
|
|
) |
|
89
|
|
|
); |
|
90
|
|
|
$bodyTemp['query']['bool'][$boolField][] = $itemSearch; |
|
91
|
|
|
break; |
|
92
|
|
|
default: |
|
93
|
|
|
throw new InvalidOperatorException("'$operator' operator not allowed. "); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$body = array_merge_recursive($body, $bodyTemp); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|