Skip to content

Commit c9c0b19

Browse files
Add Static Factory methods for Conditions and Facets (#600)
Current state is that for a good `DX` developers need to import `CmsIg\Seal\Search\Condition` and use `new Condition\...`. This looks like this: ```php use CmsIg\Seal\Search\Condition; $result = $this->engine->createSearchBuilder('blog') ->addFilter(new Condition\AndCondition( new Condition\EqualCondition('tags', 'Tech'), new Condition\OrCondition( new Condition\EqualCondition('tags', 'UX'), new Condition\EqualCondition('isSpecial', true), ), )) ->getResult(); ``` But some Code Styles prevent that and so they require to import every conditions by themselves. In that case the Developer Experience is bad as autocomplete don't work. For core based filters we could maybe create a additional `Condition` class which is a helper to quickly create the conditions without the need for `new SomeCondition`, this could look like this: ```php use CmsIg\Seal\Search\Condition\Condition; $result = $this->engine->createSearchBuilder('blog') ->addFilter(Condition::and( Condition::equal('tags', 'Tech'), Condition::or( Condition::equal('tags', 'UX'), Condition::equal('isSpecial', true), ), )) ->getResult(); ``` The query builder would so still be PHP and static code analyzer like phpstan can validate most params, but without the force of every conditions being imported by itself. ## Todo - [x] Add Condition class - [x] Add Facet class - [x] Update Tests - [x] Update Documentation --- fixes #481
1 parent 556752a commit c9c0b19

File tree

6 files changed

+266
-104
lines changed

6 files changed

+266
-104
lines changed

docs/getting-started/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,7 +1851,7 @@ many exists in the given index.
18511851
public function someMethod()
18521852
{
18531853
$result = $this->engine->createSearchBuilder('blog')
1854-
->addFilter(new \CmsIg\Seal\Search\Condition\SearchCondition('first'))
1854+
->addFilter(\CmsIg\Seal\Search\Condition\Condition::search('first'))
18551855
->getResult();
18561856
18571857
foreach ($result as $document) {
@@ -1883,7 +1883,7 @@ we will filter by the ``tags`` field and get all documents which have the tag ``
18831883
public function someMethod()
18841884
{
18851885
$result = $this->engine->createSearchBuilder('blog')
1886-
->addFilter(new \CmsIg\Seal\Search\Condition\EqualCondition('tags', 'UI'));
1886+
->addFilter(\CmsIg\Seal\Search\Condition\Condition::equal('tags', 'UI'));
18871887
->getResult();
18881888
18891889
foreach ($result as $document) {

docs/search-and-filters/index.rst

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The following shows the basic usage as already shown in the "Getting Started" do
1414
1515
<?php
1616
17-
use CmsIg\Seal\Search\Condition;
17+
use CmsIg\Seal\Search\Condition\Condition;
1818
1919
$result = $this->engine->createSearchBuilder('blog')
2020
->addFilter(/* ... */)
@@ -42,10 +42,10 @@ The ``SearchCondition`` is the most basic condition and can be used to search fo
4242
4343
<?php
4444
45-
use CmsIg\Seal\Search\Condition;
45+
use CmsIg\Seal\Search\Condition\Condition;
4646
4747
$result = $this->engine->createSearchBuilder('blog')
48-
->addFilter(new Condition\SearchCondition('Search Term'))
48+
->addFilter(Condition::search('Search Term'))
4949
->getResult();
5050
5151
The condition does only search on fields which are marked as ``searchable`` in the index configuration.
@@ -59,10 +59,10 @@ The ``EqualCondition`` is used to filter the result by a specific field value ma
5959
6060
<?php
6161
62-
use CmsIg\Seal\Search\Condition;
62+
use CmsIg\Seal\Search\Condition\Condition;
6363
6464
$result = $this->engine->createSearchBuilder('blog')
65-
->addFilter(new Condition\EqualCondition('tags', 'UI'))
65+
->addFilter(Condition::equal('tags', 'UI'))
6666
->getResult();
6767
6868
The field is required to be marked as ``filterable`` in the index configuration, it can be also
@@ -77,10 +77,10 @@ The ``NotEqualCondition`` is used to filter the result by a specific field value
7777
7878
<?php
7979
80-
use CmsIg\Seal\Search\Condition;
80+
use CmsIg\Seal\Search\Condition\Condition;
8181
8282
$result = $this->engine->createSearchBuilder('blog')
83-
->addFilter(new Condition\NotEqualCondition('tags', 'UI'))
83+
->addFilter(Condition::notEqual('tags', 'UI'))
8484
->getResult();
8585
8686
The field is required to be marked as ``filterable`` in the index configuration, it can be also
@@ -97,10 +97,10 @@ then using a ``EqualCondition``.
9797
9898
<?php
9999
100-
use CmsIg\Seal\Search\Condition;
100+
use CmsIg\Seal\Search\Condition\Condition;
101101
102102
$result = $this->engine->createSearchBuilder('blog')
103-
->addFilter(new Condition\IdentifierCondition('23b30f01-d8fd-4dca-b36a-4710e360a965'))
103+
->addFilter(Condition::identifier('23b30f01-d8fd-4dca-b36a-4710e360a965'))
104104
->getResult();
105105
106106
GreaterThanCondition
@@ -113,10 +113,10 @@ the given value.
113113
114114
<?php
115115
116-
use CmsIg\Seal\Search\Condition;
116+
use CmsIg\Seal\Search\Condition\Condition;
117117
118118
$result = $this->engine->createSearchBuilder('blog')
119-
->addFilter(new Condition\GreaterThanCondition('rating', 2.5))
119+
->addFilter(Condition::greaterThan('rating', 2.5))
120120
->getResult();
121121
122122
The field is required to be marked as ``filterable`` in the index configuration.
@@ -131,10 +131,10 @@ the given value.
131131
132132
<?php
133133
134-
use CmsIg\Seal\Search\Condition;
134+
use CmsIg\Seal\Search\Condition\Condition;
135135
136136
$result = $this->engine->createSearchBuilder('blog')
137-
->addFilter(new Condition\GreaterThanEqualCondition('rating', 2.5))
137+
->addFilter(Condition::greaterThanEqual('rating', 2.5))
138138
->getResult();
139139
140140
The field is required to be marked as ``filterable`` in the index configuration.
@@ -149,10 +149,10 @@ the given value.
149149
150150
<?php
151151
152-
use CmsIg\Seal\Search\Condition;
152+
use CmsIg\Seal\Search\Condition\Condition;
153153
154154
$result = $this->engine->createSearchBuilder('blog')
155-
->addFilter(new Condition\LessThanCondition('rating', 2.5))
155+
->addFilter(Condition::lessThan('rating', 2.5))
156156
->getResult();
157157
158158
The field is required to be marked as ``filterable`` in the index configuration.
@@ -167,10 +167,10 @@ the given value.
167167
168168
<?php
169169
170-
use CmsIg\Seal\Search\Condition;
170+
use CmsIg\Seal\Search\Condition\Condition;
171171
172172
$result = $this->engine->createSearchBuilder('blog')
173-
->addFilter(new Condition\LessThanEqualCondition('rating', 2.5))
173+
->addFilter(Condition::lessThanEqual('rating', 2.5))
174174
->getResult();
175175
176176
The field is required to be marked as ``filterable`` in the index configuration.
@@ -184,10 +184,10 @@ The ``GeoDistanceCondition`` is used to filter results within a radius by specif
184184
185185
<?php
186186
187-
use CmsIg\Seal\Search\Condition;
187+
use CmsIg\Seal\Search\Condition\Condition;
188188
189189
$result = $this->engine->createSearchBuilder('restaurants')
190-
->addFilter(new Condition\GeoDistanceCondition('location', 45.472735, 9.184019, 2000))
190+
->addFilter(Condition::geoDistance('location', 45.472735, 9.184019, 2000))
191191
->getResult();
192192
193193
The field is required to be marked as ``filterable`` in the index configuration.
@@ -201,10 +201,10 @@ The ``GeoBoundingBoxCondition`` is used to filter results within a bounding box
201201
202202
<?php
203203
204-
use CmsIg\Seal\Search\Condition;
204+
use CmsIg\Seal\Search\Condition\Condition;
205205
206206
$result = $this->engine->createSearchBuilder('restaurants')
207-
->addFilter(new Condition\GeoBoundingBoxCondition('location', 45.494181, 9.214024, 45.449484, 9.179175))
207+
->addFilter(Condition::geoBoundingBox('location', 45.494181, 9.214024, 45.449484, 9.179175))
208208
->getResult();
209209
210210
@@ -224,12 +224,12 @@ The ``OrCondition`` is used to filter by two or more conditions where at least o
224224
225225
<?php
226226
227-
use CmsIg\Seal\Search\Condition;
227+
use CmsIg\Seal\Search\Condition\Condition;
228228
229229
$result = $this->engine->createSearchBuilder('blog')
230-
->addFilter(new Condition\OrCondition(
231-
new Condition\GreaterThanCondition('rating', 2.5),
232-
new Condition\EqualCondition('isSpecial', true),
230+
->addFilter(Condition::or(
231+
Condition::greaterThan('rating', 2.5),
232+
Condition::equal('isSpecial', true),
233233
))
234234
->getResult();
235235
@@ -246,14 +246,14 @@ in combination with ``OrCondition`` filters.
246246
247247
<?php
248248
249-
use CmsIg\Seal\Search\Condition;
249+
use CmsIg\Seal\Search\Condition\Condition;
250250
251251
$result = $this->engine->createSearchBuilder('blog')
252-
->addFilter(new Condition\AndCondition(
253-
new Condition\EqualCondition('tags', 'Tech'),
254-
new Condition\OrCondition(
255-
new Condition\EqualCondition('tags', 'UX'),
256-
new Condition\EqualCondition('isSpecial', true),
252+
->addFilter(Condition::and(
253+
Condition::equal('tags', 'Tech'),
254+
Condition::or(
255+
Condition::equal('tags', 'UX'),
256+
Condition::equal('isSpecial', true),
257257
),
258258
))
259259
->getResult();
@@ -290,10 +290,10 @@ Need to be queried this way `<object>.<field>`:
290290
291291
<?php
292292
293-
use CmsIg\Seal\Search\Condition;
293+
use CmsIg\Seal\Search\Condition\Condition;
294294
295295
$result = $this->engine->createSearchBuilder('blog')
296-
->addFilter(new Condition\LessThanEqualCondition('rating.value', 2.5))
296+
->addFilter(Condition::lessThanEqual('rating.value', 2.5))
297297
->getResult();
298298
299299
To filter on ``Typed`` objects also the `.` symbol is used but the type name need to be included as well.
@@ -317,10 +317,10 @@ Need to be queried this way `<object>.<type>.<field>`:
317317
318318
<?php
319319
320-
use CmsIg\Seal\Search\Condition;
320+
use CmsIg\Seal\Search\Condition\Condition;
321321
322322
$result = $this->engine->createSearchBuilder('blog')
323-
->addFilter(new Condition\EqualCondition('header.image.media', 21))
323+
->addFilter(Condition::equal('header.image.media', 21))
324324
->getResult();
325325
326326
Also nested objects and types can be queried the same way.
@@ -376,7 +376,7 @@ your results but also ``sort`` them by a given field.
376376
377377
<?php
378378
379-
use CmsIg\Seal\Search\Condition;
379+
use CmsIg\Seal\Search\Condition\Condition;
380380
381381
$result = $this->engine->createSearchBuilder('blog')
382382
->addSortBy('rating', 'desc')
@@ -386,7 +386,7 @@ your results but also ``sort`` them by a given field.
386386
387387
<?php
388388
389-
use CmsIg\Seal\Search\Condition;
389+
use CmsIg\Seal\Search\Condition\Condition;
390390
391391
$result = $this->engine->createSearchBuilder('blog')
392392
->addSortBy('rating', 'asc')
@@ -405,10 +405,10 @@ The abstraction can also be used to highlight the search term in the result.
405405
406406
<?php
407407
408-
use CmsIg\Seal\Search\Condition;
408+
use CmsIg\Seal\Search\Condition\Condition;
409409
410410
$result = $this->engine->createSearchBuilder('blog')
411-
->addFilter(new Condition\SearchCondition('Search Term'))
411+
->addFilter(Condition::search('Search Term'))
412412
->highlight(['title'], '<mark>', '</mark>');
413413
->getResult();
414414
@@ -446,19 +446,18 @@ search engines support:
446446
447447
<?php
448448
449-
use CmsIg\Seal\Search\Facet\MinMaxFacet;
450-
use CmsIg\Seal\Search\Facet\CountFacet;
449+
use CmsIg\Seal\Search\Facet\Facet;
451450
452451
$result = $this->engine->createSearchBuilder('blog')
453-
->addFacet(new MinMaxFacet('age'))
454-
->addFacet(new CountFacet('tags'))
452+
->addFacet(Facet::minMax('rating'))
453+
->addFacet(Facet::count('tags'))
455454
->getResult();
456455
457456
$facets = $result->facets(); // Output depends on the facet type
458457
459458
.. note::
460459

461-
For ``->addFacet()`` to work, your fields (``age`` and ``tags`` in our our example) have to be configured using
460+
For ``->addFacet()`` to work, your fields (``rating`` and ``tags`` in our our example) have to be configured using
462461
`facet: true` in the index schema.
463462

464463
.. note::
@@ -479,10 +478,10 @@ for example. Instead of displaying all the product variants, you may group them
479478
480479
<?php
481480
482-
use CmsIg\Seal\Search\Condition;
481+
use CmsIg\Seal\Search\Condition\Condition;
483482
484483
$result = $this->engine->createSearchBuilder('blog')
485-
->addFilter(new Condition\SearchCondition('product title'))
484+
->addFilter(Condition::search('product title'))
486485
->distinct('product_id')
487486
->getResult();
488487
}

0 commit comments

Comments
 (0)