Skip to content

Commit c3696f8

Browse files
wackerl91ruflin
authored andcommitted
Added Collapse DSL to QueryBuilder (#1724)
That's going to be a bit awkward now: in the PR that added Field Collapsing Support (#1653) I forgot to check in the DSL classes, which made working with it a bit clumsy compared to the usual query methods. I.e. - as with aggregations - it's now possible to do stuff like this: ``` $collapse ->setFieldname('product_id') ->setInnerHits( $qb->collapse->inner_hits() // instead of (new Collapse\InnerHits()) ) ``` Let me know if there need to be any changes. Cheers :)
1 parent a068238 commit c3696f8

File tree

8 files changed

+79
-1
lines changed

8 files changed

+79
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ All notable changes to this project will be documented in this file based on the
2525
* Launch tests with PHP 7.4
2626
* Added `nullable_type_declaration_for_default_null_value`, `no_alias_functions` CS rules [#1706](https://github.com/ruflin/Elastica/pull/1706)
2727
* Configured `visibility_required` CS rule for constants [#1723](https://github.com/ruflin/Elastica/pull/1723)
28+
* Added `Collapse` DSL to `QueryBuilder` [#1724](https://github.com/ruflin/Elastica/pull/1724)
2829

2930
### Deprecated
3031

lib/Elastica/QueryBuilder.php

+11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function __construct(?Version $version = null)
3434
$this->addDSL(new DSL\Query());
3535
$this->addDSL(new DSL\Aggregation());
3636
$this->addDSL(new DSL\Suggest());
37+
$this->addDSL(new DSL\Collapse());
3738
}
3839

3940
/**
@@ -91,4 +92,14 @@ public function suggest()
9192
{
9293
return $this->_facades[DSL::TYPE_SUGGEST];
9394
}
95+
96+
/**
97+
* Collapse DSL.
98+
*
99+
* @return DSL\Collapse
100+
*/
101+
public function collapse()
102+
{
103+
return $this->_facades[DSL::TYPE_COLLAPSE];
104+
}
94105
}

lib/Elastica/QueryBuilder/DSL/Collapse.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Elastica\QueryBuilder\DSL;
44

5-
use Elastica\Query\InnerHits;
5+
use Elastica\Collapse\InnerHits;
66
use Elastica\QueryBuilder\DSL;
77

88
/**

lib/Elastica/QueryBuilder/Version.php

+17
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ abstract class Version
3737
*/
3838
protected $suggesters = [];
3939

40+
/**
41+
* supported methods for field collapsing.
42+
*
43+
* @var array
44+
*/
45+
protected $collapsers = [];
46+
4047
/**
4148
* returns true if $name is supported, false otherwise.
4249
*/
@@ -49,6 +56,8 @@ public function supports(string $name, string $type): bool
4956
return \in_array($name, $this->aggregations, true);
5057
case DSL::TYPE_SUGGEST:
5158
return \in_array($name, $this->suggesters, true);
59+
case DSL::TYPE_COLLAPSE:
60+
return \in_array($name, $this->collapsers, true);
5261
}
5362

5463
// disables version check in Facade for custom DSL objects
@@ -78,4 +87,12 @@ public function getSuggesters(): array
7887
{
7988
return $this->suggesters;
8089
}
90+
91+
/**
92+
* @return string[]
93+
*/
94+
public function getCollapsers(): array
95+
{
96+
return $this->collapsers;
97+
}
8198
}

lib/Elastica/QueryBuilder/Version/Version700.php

+4
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,8 @@ class Version700 extends Version
9797
'completion',
9898
'context',
9999
];
100+
101+
protected $collapsers = [
102+
'inner_hits',
103+
];
100104
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Elastica\Test\QueryBuilder\DSL;
4+
5+
use Elastica\Collapse;
6+
use Elastica\QueryBuilder\DSL;
7+
8+
class CollapseTest extends AbstractDSLTest
9+
{
10+
/**
11+
* @group unit
12+
*/
13+
public function testType()
14+
{
15+
$collapseDSL = new DSL\Collapse();
16+
17+
$this->assertInstanceOf(DSL::class, $collapseDSL);
18+
$this->assertEquals(DSL::TYPE_COLLAPSE, $collapseDSL->getType());
19+
}
20+
21+
/**
22+
* @group unit
23+
*/
24+
public function testInterface()
25+
{
26+
$collapseDSL = new DSL\Collapse();
27+
28+
$this->_assertImplemented($collapseDSL, 'inner_hits', Collapse\InnerHits::class, []);
29+
// Make sure collapse returns an instance of Collapse\InnerHits instead of Query\InnerHits
30+
$this->assertInstanceOf(Collapse\InnerHits::class, $collapseDSL->inner_hits());
31+
}
32+
}

test/Elastica/QueryBuilder/VersionTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function testVersions()
1717
new DSL\Query(),
1818
new DSL\Aggregation(),
1919
new DSL\Suggest(),
20+
new DSL\Collapse(),
2021
];
2122

2223
$versions = [
@@ -51,5 +52,12 @@ private function assertVersions(Version $version, array $dsl)
5152
'suggester "'.$suggester.'" in '.\get_class($version).' must be defined in '.\get_class($dsl[2])
5253
);
5354
}
55+
56+
foreach ($version->getCollapsers() as $collapser) {
57+
$this->assertTrue(
58+
\method_exists($dsl[3], $collapser),
59+
'suggester "'.$collapser.'" in '.\get_class($version).' must be defined in '.\get_class($dsl[3])
60+
);
61+
}
5462
}
5563
}

test/Elastica/QueryBuilderTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Elastica\Test;
44

55
use Elastica\Aggregation\AbstractAggregation;
6+
use Elastica\Collapse;
67
use Elastica\Exception\QueryBuilderException;
78
use Elastica\Query\AbstractQuery;
89
use Elastica\QueryBuilder;
@@ -44,6 +45,10 @@ public function testFacade()
4445
$this->assertInstanceOf(AbstractQuery::class, $qb->query()->match());
4546
$this->assertInstanceOf(AbstractAggregation::class, $qb->aggregation()->avg('name'));
4647
$this->assertInstanceOf(AbstractSuggest::class, $qb->suggest()->term('name', 'field'));
48+
49+
// Collapse is a special case of the above; it doesn't have an abstract base class for individual parts right
50+
// now because 'inner_hits' is the only thing that can be set besides field and concurrency.
51+
$this->assertInstanceOf(Collapse\InnerHits::class, $qb->collapse()->inner_hits());
4752
}
4853
}
4954

0 commit comments

Comments
 (0)