Skip to content
This repository was archived by the owner on Nov 4, 2021. It is now read-only.

Commit eaf1008

Browse files
committed
Added collapsing by field
1 parent f6746d7 commit eaf1008

File tree

7 files changed

+80
-12
lines changed

7 files changed

+80
-12
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ App\MyModel::search('phone')
195195
->where('color', 'red')
196196
// sort
197197
->orderBy('price', 'asc')
198+
// collapse by field
199+
->collapse('brand')
198200
// set offset
199201
->from(0)
200202
// set limit

src/Builders/FilterBuilder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ class FilterBuilder extends Builder
1515

1616
public $offset;
1717

18+
/**
19+
* @var string
20+
*/
21+
public $collapse;
22+
1823
public function __construct($model, $callback = null)
1924
{
2025
$this->model = $model;
@@ -235,4 +240,15 @@ public function paginate($perPage = null, $pageName = 'page', $page = null)
235240

236241
return $paginator;
237242
}
243+
244+
/**
245+
* @param string $field
246+
* @return $this
247+
*/
248+
public function collapse(string $field)
249+
{
250+
$this->collapse = $field;
251+
252+
return $this;
253+
}
238254
}

src/ElasticEngine.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,12 @@ protected function buildSearchQueryPayload(Builder $builder, $queryPayload, arra
7676

7777
$payload = (new TypePayload($builder->model))
7878
->setIfNotEmpty('body.query.bool', $queryPayload)
79+
->setIfNotEmpty('body.collapse.field', $builder->collapse)
7980
->setIfNotEmpty('body.sort', $builder->orders)
8081
->setIfNotEmpty('body.explain', $options['explain'] ?? null)
81-
->setIfNotEmpty('body.profile', $options['profile'] ?? null);
82-
83-
if (isset($builder->offset)) {
84-
$payload->set('body.from', $builder->offset);
85-
}
86-
87-
if (isset($builder->limit)) {
88-
$payload->set('body.size', $builder->limit);
89-
}
82+
->setIfNotEmpty('body.profile', $options['profile'] ?? null)
83+
->setIfNotNull('body.from', $builder->offset)
84+
->setIfNotNull('body.size', $builder->limit);
9085

9186
return $payload->get();
9287
}

src/Payloads/RawPayload.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ public function setIfNotEmpty($key, $value)
2424
return $this->set($key, $value);
2525
}
2626

27+
/**
28+
* @param string $key
29+
* @param mixed $value
30+
* @return $this
31+
*/
32+
public function setIfNotNull(string $key, $value)
33+
{
34+
if (is_null($value)) {
35+
return $this;
36+
}
37+
38+
return $this->set($key, $value);
39+
}
40+
2741
public function has($key)
2842
{
2943
return array_has($this->payload, $key);

tests/Builders/FilterBuilderTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,12 @@ public function testWhereGeoPolygon()
316316
{
317317
$this
318318
->builder
319-
->whereGeoPolygon('foo', [[-70, 40],[-80, 30],[-90, 20]]);
319+
->whereGeoPolygon('foo', [[-70, 40], [-80, 30], [-90, 20]]);
320320

321321
$this->assertEquals(
322322
[
323323
'must' => [
324-
['geo_polygon' => ['foo' => ['points' => [[-70, 40],[-80, 30],[-90, 20]]]]]
324+
['geo_polygon' => ['foo' => ['points' => [[-70, 40], [-80, 30], [-90, 20]]]]]
325325
],
326326
'must_not' => []
327327
],
@@ -373,4 +373,16 @@ public function testFrom()
373373
$this->builder->offset
374374
);
375375
}
376+
377+
public function testCollapse()
378+
{
379+
$this
380+
->builder
381+
->collapse('foo');
382+
383+
$this->assertEquals(
384+
'foo',
385+
$this->builder->collapse
386+
);
387+
}
376388
}

tests/ElasticEngineTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testBuildSearchQueryPayloadCollection()
3535

3636
$searchBuilder = (new SearchBuilder($model, 'foo'))
3737
->rule(SearchRule::class)
38-
->rule(function(SearchBuilder $searchBuilder) {
38+
->rule(function (SearchBuilder $searchBuilder) {
3939
return [
4040
'must' => [
4141
'match' => [
@@ -46,6 +46,7 @@ public function testBuildSearchQueryPayloadCollection()
4646
})
4747
->where('id', '>', 20)
4848
->orderBy('id', 'asc')
49+
->collapse('brand')
4950
->take(10)
5051
->from(100);
5152

@@ -81,6 +82,9 @@ public function testBuildSearchQueryPayloadCollection()
8182
]
8283
]
8384
],
85+
'collapse' => [
86+
'field' => 'brand'
87+
],
8488
'sort' => [
8589
[
8690
'id' => 'asc'
@@ -116,6 +120,9 @@ public function testBuildSearchQueryPayloadCollection()
116120
]
117121
]
118122
],
123+
'collapse' => [
124+
'field' => 'brand'
125+
],
119126
'sort' => [
120127
[
121128
'id' => 'asc'

tests/Payloads/RawPayloadTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ public function testSetIfNotEmpty()
3434
);
3535
}
3636

37+
public function testSetIfNotNull()
38+
{
39+
$payload = (new RawPayload())
40+
->setIfNotNull('null', null)
41+
->setIfNotNull('false', false)
42+
->setIfNotNull('zero', 0)
43+
->setIfNotNull('empty_array', [])
44+
->setIfNotNull('empty_string', '')
45+
->setIfNotNull('foo', 'bar');
46+
47+
$this->assertEquals(
48+
[
49+
'false' => false,
50+
'zero' => 0,
51+
'empty_array' => [],
52+
'empty_string' => '',
53+
'foo' => 'bar'
54+
],
55+
$payload->get()
56+
);
57+
}
58+
3759
public function testHas()
3860
{
3961
$payload = (new RawPayload())

0 commit comments

Comments
 (0)