-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathElasticsearchSelectionFactory.php
More file actions
146 lines (119 loc) · 4.02 KB
/
ElasticsearchSelectionFactory.php
File metadata and controls
146 lines (119 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace G4\DataMapper\Engine\Elasticsearch;
use G4\DataMapper\Common\Selection\Comparison;
use G4\DataMapper\Common\Selection\Operator;
use G4\DataMapper\Common\SelectionFactoryInterface;
use G4\DataMapper\Common\Selection\Sort;
use G4\DataMapper\Common\SingleValue;
class ElasticsearchSelectionFactory implements SelectionFactoryInterface
{
/**
* @var ElasticsearchIdentityInterface
*/
private $identity;
public function __construct(ElasticsearchIdentityInterface $identity)
{
$this->identity = $identity;
}
public function fieldNames()
{
$fieldNames = $this->identity->getFieldNames();
if (!is_array($fieldNames)) {
return [];
}
return count($fieldNames) === 0
? []
: $fieldNames;
}
public function group()
{
return [
'group_by_' . $this->identity->getGrouping() => [
'terms' => [
'field' => $this->identity->getGrouping()
]
]
];
}
public function limit()
{
return (int) $this->identity->getLimit();
}
public function offset()
{
return (int) $this->identity->getOffset();
}
public function sort()
{
$rawSorting = $this->identity->getSorting();
if (empty($rawSorting)) {
return [];
}
$sorting = [];
foreach ($rawSorting as $oneSort) {
if ($oneSort instanceof Sort) {
$sorting[] = $oneSort->getSort($this->makeSortingFormatter($oneSort));
}
}
return $sorting;
}
//TODO:Vladan:It is too complicated.
public function where()
{
if ($this->identity->isVoid()) {
return $this->addConsistentRandomKey(['bool' => ['must' => ['match_all' => new \stdClass()]]]);
}
$comparisons = [];
foreach ($this->identity->getComparisons() as $oneComparison) {
if ($oneComparison instanceof Comparison && !$oneComparison->getValue()->isEmpty()) {
$comparisonName = $oneComparison->getName() ?: '';
if (str_starts_with($comparisonName, '-')) {
$comparisons['must_not'][]= $oneComparison->getComparison($this->makeComparisonFormatter());
} else {
$comparisons['must'][]= $oneComparison->getComparison($this->makeComparisonFormatter());
}
}
}
$geodistFormatter = new ElasticsearchGeodistFormatter($this->identity);
if ($this->identity->hasCoordinatesMin()) {
$comparisons['must_not'][] = $geodistFormatter->formatMin();
}
if ($this->identity->hasRawQuery()) {
$comparisons['must'][]= $this->identity->getRawQuery();
}
if (empty($comparisons)) {
$comparisons = ['must' => ['match_all' => new \stdClass()]];
}
$comparisons['filter'] = $geodistFormatter->format();
return $this->addConsistentRandomKey(['bool' => $comparisons]);
}
/**
* @param array $data
* @return array
*/
private function addConsistentRandomKey(array $data)
{
return $this->identity->hasConsistentRandomKey()
? (new Comparison(
$this->identity->getConsistentRandomKey(),
new Operator(Operator::CONSISTENT_RANDOM_KEY),
new SingleValue($data)
))
->getComparison($this->makeComparisonFormatter())
: $data;
}
private function makeComparisonFormatter()
{
return new ElasticsearchComparisonFormatter($this->identity);
}
/**
* @param Sort $sortData
* @return ElasticsearchGeodistSortFormatter|ElasticsearchSortingFormatter
*/
private function makeSortingFormatter(Sort $sortData)
{
return $sortData instanceof ElasticsearchGeodistSort
? new ElasticsearchGeodistSortFormatter($this->identity)
: new ElasticsearchSortingFormatter();
}
}