Completed
Push — develop ( b1a53e...8d34c6 )
by
unknown
12s
created

Search::addAggregations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 1
1
<?php namespace Nord\Lumen\Elasticsearch\Search;
2
3
use Nord\Lumen\Elasticsearch\Search\Aggregation\Aggregation;
4
use Nord\Lumen\Elasticsearch\Search\Aggregation\AggregationCollection;
5
use Nord\Lumen\Elasticsearch\Search\Query\QueryDSL;
6
7
class Search
8
{
9
    /**
10
     * @var string
11
     */
12
    private $index;
13
14
    /**
15
     * @var string
16
     */
17
    private $type;
18
19
    /**
20
     * @var QueryDSL
21
     */
22
    private $query;
23
24
    /**
25
     * @var Sort
26
     */
27
    private $sort;
28
29
    /**
30
     * @var AggregationCollection
31
     */
32
    private $aggregations;
33
34
    /**
35
     * @var int
36
     */
37
    private $from = 0;
38
39
    /**
40
     * @var int
41
     */
42
    private $size = 100;
43
44
    /**
45
     * @var int
46
     */
47
    private $page = 1;
48
49
50
    /**
51
     * Constructor.
52
     */
53
    public function __construct()
54
    {
55
        $this->aggregations = new AggregationCollection();
56
    }
57
58
59
    /**
60
     * @param $index
61
     * @return Search
62
     */
63
    public function setIndex($index)
64
    {
65
        $this->index = $index;
66
        return $this;
67
    }
68
69
70
    /**
71
     * @return string
72
     */
73
    public function getIndex()
74
    {
75
        return $this->index;
76
    }
77
78
79
    /**
80
     * @param $type
81
     * @return Search
82
     */
83
    public function setType($type)
84
    {
85
        $this->type = $type;
86
        return $this;
87
    }
88
89
90
    /**
91
     * @return string
92
     */
93
    public function getType()
94
    {
95
        return $this->type;
96
    }
97
98
99
    /**
100
     * @param QueryDSL $query
101
     * @return Search
102
     */
103
    public function setQuery(QueryDSL $query)
104
    {
105
        $this->query = $query;
106
        return $this;
107
    }
108
109
110
    /**
111
     * @return QueryDSL
112
     */
113
    public function getQuery()
114
    {
115
        return $this->query;
116
    }
117
118
119
    /**
120
     * @param Sort $sort
121
     * @return Search
122
     */
123
    public function setSort(Sort $sort)
124
    {
125
        $this->sort = $sort;
126
        return $this;
127
    }
128
129
130
    /**
131
     * @return Sort
132
     */
133
    public function getSort()
134
    {
135
        return $this->sort;
136
    }
137
138
139
    /**
140
     * @return AggregationCollection
141
     */
142
    public function getAggregations()
143
    {
144
        return $this->aggregations;
145
    }
146
147
148
    /**
149
     * @param Aggregation $aggregation
150
     * @return Search
151
     */
152
    public function addAggregation(Aggregation $aggregation)
153
    {
154
        $this->aggregations->add($aggregation);
155
        return $this;
156
    }
157
158
159
    /**
160
     * @param array $aggregations
161
     * @return Search
162
     */
163
    public function addAggregations(array $aggregations)
164
    {
165
        foreach ($aggregations as $aggregation) {
166
            if ($aggregation instanceof Aggregation) {
167
                $this->addAggregation($aggregation);
168
            }
169
        }
170
        return $this;
171
    }
172
173
174
    /**
175
     * @param int $page
176
     * @return Search
177
     */
178
    public function setPage($page)
179
    {
180
        $this->page = (int)$page;
181
        return $this;
182
    }
183
184
    /**
185
     * @return int
186
     */
187
    public function getFrom()
188
    {
189
        return $this->from;
190
    }
191
192
    /**
193
     * @param int $from
194
     *
195
     * @return Search
196
     */
197
    public function setFrom($from)
198
    {
199
        $this->from = $from;
200
201
        return $this;
202
    }
203
204
205
    /**
206
     * @return int
207
     */
208
    public function getSize()
209
    {
210
        return $this->size;
211
    }
212
213
214
    /**
215
     * @param int $size
216
     * @return Search
217
     */
218
    public function setSize($size)
219
    {
220
        $this->size = (int)$size;
221
        return $this;
222
    }
223
224
225
    /**
226
     * @return int
227
     */
228
    public function getPage()
229
    {
230
        return $this->page;
231
    }
232
233
234
    /**
235
     * @return array
236
     */
237
    public function buildBody()
238
    {
239
        $body = [];
240
241
        if (($query = $this->getQuery())) {
242
            if (!empty($query)) {
243
                $body['query'] = $query->toArray();
244
            }
245
        }
246
        if (empty($body['query'])) {
247
            $body['query'] = ['match_all' => []];
248
        }
249
250
        if (($sort = $this->getSort())) {
251
            if (!empty($sort)) {
252
                $body['sort'] = $sort->toArray();
253
            }
254
        }
255
256
        $aggregations = $this->getAggregations();
257
        if ($aggregations->count() > 0) {
258
            $body['aggs'] = $aggregations->toArray();
259
        }
260
261
        // Set how many results to return.
262
        if ($this->getSize() > 0) {
263
            $body['size'] = $this->getSize();
264
        }
265
266
        // Use "from" to determine "from, if it's not set we determine it from the "page"
267
        if ($this->getFrom() > 0) {
268
            $from = $this->getFrom();
269
        } else {
270
            $from = ($this->getPage() - 1) * $this->getSize();
271
        }
272
273
        $body['from'] = $from;
274
275
        return $body;
276
    }
277
}
278