Newer
Older
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
<?php
namespace IQDEV\ElasticSearch\Search;
use IQDEV\ElasticSearch\Esable;
use IQDEV\ElasticSearch\Search\Aggs\AggsCollection;
use IQDEV\ElasticSearch\Search\BoolQuery\Query;
use IQDEV\ElasticSearch\Search\Sorting\SortingCollection;
final class Request implements Esable
{
private ?Query $query = null;
private ?Query $postFilter = null;
private ?AggsCollection $aggs = null;
private ?Pagination $pagination = null;
private ?SortingCollection $sorting = null;
private array $match = [];
/**
* @param Pagination|null $pagination
* @return Request
*/
public function setPagination(?Pagination $pagination): self
{
$this->pagination = $pagination;
return $this;
}
/**
* @param SortingCollection|null $sorting
* @return $this
*/
public function setSorting(?SortingCollection $sorting): self
{
$this->sorting = $sorting;
return $this;
}
/**
* @return Query
*/
public function getQuery(): Query
{
if ($this->query === null) {
$this->query = new Query();
}
return $this->query;
}
/**
* @return Query
*/
public function getPostFilter(): Query
{
if ($this->postFilter === null) {
$this->postFilter = new Query();
}
return $this->postFilter;
}
public function getAggs(): AggsCollection
{
if ($this->aggs === null) {
$this->aggs = new AggsCollection();
}
return $this->aggs;
}
/**
* @return Pagination|null
*/
public function getPagination(): ?Pagination
{
return $this->pagination;
}
/**
* @return SortingCollection|null
*/
public function getSorting(): ?SortingCollection
{
return $this->sorting;
}
public function addMatch(string $key, array $param): self
{
$this->match[$key] = $param;
return $this;
}
public function es(): array
{
$request = [
'_source' => ['id', 'data.*']
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
];
if (isset($this->postFilter) && $this->postFilter->isEmpty() === false) {
$request['post_filter'] = $this->postFilter->es()['query'];
}
if (isset($this->query) && $this->query->isEmpty() === false) {
$request['query'] = $this->query->es()['query'];
}
if (empty($this->match) === false) {
foreach ($this->match as $key => $value) {
$request['query']['match'][$key] = $value;
}
}
if ($this->aggs) {
$request['aggs'] = $this->aggs->es()['aggs'];
}
if ($this->pagination) {
$request = array_merge($request, $this->pagination->es());
}
if ($this->sorting) {
$request['sort'] = $this->sorting->es();
}
return $request;
}
}