Skip to content

Commit cf24ec5

Browse files
committed
docs(filters): add documentation for new search filters
Document the newly introduced search filters (`IriFilter`, `ExactFilter`, `PartialSearchFilter`, `FreeTextQueryFilter`, `OrFilter`) and clarify the deprecation of the `SearchFilter`. Encourage users to migrate to `QueryParameter` attributes for better configurability and explicitness.
1 parent 0b71dc0 commit cf24ec5

File tree

1 file changed

+197
-1
lines changed

1 file changed

+197
-1
lines changed

core/doctrine-filters.md

Lines changed: 197 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,21 @@ For the sake of consistency, we're using the attribute in the below documentatio
171171
For MongoDB ODM, all the filters are in the namespace `ApiPlatform\Doctrine\Odm\Filter`. The filter
172172
services all begin with `api_platform.doctrine_mongodb.odm`.
173173

174-
## Search Filter
174+
## Search Filter (not recommended)
175+
176+
> [!WARNING]
177+
> Instead of using the deprecated `SearchFilter` its recommended to use the new search filters with QueryParameter attributes
178+
179+
### Built-in new Search Filters (API Platform >= 4.2)
180+
181+
To add some search filters, choose over this new list:
182+
- [IriFilter](#iri-filter) (filter on IRIs)
183+
- [ExactFilter](#exact-filter) (filter with exact value)
184+
- [PartialSearchFilter](#partial-search-filter) (filter using a `LIKE %value%``)
185+
- [FreeTextQueryFilter](#free-text-query-filter) (allows you to apply multiple filters to multiple properties of a resource at the same time, using a single parameter in the URL)
186+
- [OrFilter](#or-filter) (apply a filter using `orWhere` instead of `andWhere` )
187+
188+
### Legacy SearchFilter (API Platform < 4.2))
175189

176190
If Doctrine ORM or MongoDB ODM support is enabled, adding filters is as easy as registering a filter service in the
177191
`api/config/services.yaml` file and adding an attribute to your resource configuration.
@@ -293,6 +307,188 @@ Using a numeric ID is also supported: `http://localhost:8000/api/offers?product=
293307

294308
The above URLs will return all offers for the product having the following IRI as JSON-LD identifier (`@id`): `http://localhost:8000/api/products/12`.
295309

310+
## Iri Filter
311+
312+
The iri filter allows filtering a resource using IRIs.
313+
314+
Syntax: `?property=value`
315+
316+
The value can take any [IRI(Internationalized Resource Identifier) ](https://en.wikipedia.org/wiki/Internationalized_Resource_Identifier).
317+
318+
Like other [new search filters](#built-in-new-search-filters-api-platform--42) it can be used on the ApiResource attribute
319+
or in the operation attribute, for e.g., the `#GetCollection()` attribute:
320+
321+
```php
322+
// api/src/ApiResource/Chicken.php
323+
324+
#[GetCollection(
325+
parameters: [
326+
'chickenCoop' => new QueryParameter(filter: new IriFilter()),
327+
],
328+
)]
329+
class Chicken
330+
{
331+
//...
332+
}
333+
```
334+
335+
Given that the endpoint is `/chickens`, you can filter chickens by chicken coop with the following query:
336+
`/chikens?chickenCoop=/chickenCoop/1`.
337+
338+
It will return all the chickens that live the chicken coop number 1.
339+
340+
## Exact Filter
341+
342+
The exact filter allows filtering a resource using exact values.
343+
344+
Syntax: `?property=value`
345+
346+
The value can take any scalar value or array of values.
347+
348+
Like other [new search filters](#built-in-new-search-filters-api-platform--42) it can be used on the ApiResource attribute
349+
or in the operation attribute, for e.g., the `#GetCollection()` attribute:
350+
351+
```php
352+
// api/src/ApiResource/Chicken.php
353+
354+
#[GetCollection(
355+
parameters: [
356+
'name' => new QueryParameter(filter: new ExactFilter()),
357+
],
358+
)]
359+
class Chicken
360+
{
361+
//...
362+
}
363+
```
364+
365+
Given that the endpoint is `/chickens`, you can filter chickens by name with the following query:
366+
`/chikens?name=Gertrude`.
367+
368+
It will return all the chickens that are exactly named _Gertrude_.
369+
370+
## Partial Search Filter
371+
372+
The partial search filter allows filtering a resource using partial values.
373+
374+
Syntax: `?property=value`
375+
376+
The value can take any scalar value or array of values.
377+
378+
Like other [new search filters](#built-in-new-search-filters-api-platform--42) it can be used on the ApiResource attribute
379+
or in the operation attribute, for e.g., the `#GetCollection()` attribute:
380+
381+
```php
382+
// api/src/ApiResource/Chicken.php
383+
384+
#[GetCollection(
385+
parameters: [
386+
'name' => new QueryParameter(filter: new PartialSearchFilter()),
387+
],
388+
)]
389+
class Chicken
390+
{
391+
//...
392+
}
393+
```
394+
395+
Given that the endpoint is `/chickens`, you can filter chickens by name with the following query:
396+
`/chikens?name=tom`.
397+
398+
It will return all chickens where the name contains the substring _tom_.
399+
400+
> [!NOTE]
401+
> This filter performs a case-insensitive search. It automatically normalizes both the input value and the stored data
402+
> (e.g., by converting them to lowercase) before making the comparison.
403+
404+
## Free Text Query Filter
405+
406+
The free text query filter allows filtering allows you to apply a single filter across a list of properties. Its primary
407+
role is to repeat a filter's logic for each specified field.
408+
409+
Syntax: `?property=value`
410+
411+
The value can take any scalar value or array of values.
412+
413+
Like other [new search filters](#built-in-new-search-filters-api-platform--42) it can be used on the ApiResource attribute
414+
or in the operation attribute, for e.g. the `#GetCollection()` attribute:
415+
416+
```php
417+
// api/src/ApiResource/Chicken.php
418+
419+
#[GetCollection(
420+
parameters: [
421+
'q' => new QueryParameter(
422+
filter: new FreeTextQueryFilter(new PartialSearchFilter()),
423+
properties: ['name', 'ean']
424+
),
425+
],
426+
)]
427+
class Chicken
428+
{
429+
//...
430+
}
431+
```
432+
433+
Given that the endpoint is `/chickens`, you can filter chickens by name with the following query:
434+
`/chikens?q=tom`.
435+
436+
**Result**:
437+
438+
This request will return all chickens where:
439+
440+
- the `name` is exactly "FR123456"
441+
- **AND**
442+
- the `ean` is exactly "FR123456".
443+
444+
For the `OR` option refer to the [OrFilter](#or-filter).
445+
446+
## Or Filter
447+
448+
The or filter allows you to explicitly change the logical condition used by the filter it wraps. Its sole purpose is to
449+
force a filter to combine its criteria with OR instead of the default AND.
450+
451+
It's the ideal tool for creating a search parameter that should find a match in any of the specified fields,
452+
but not necessarily all of them.
453+
454+
Syntax: `?property=value`
455+
456+
The value can take any scalar value or array of values.
457+
458+
The `OrFilter` is a decorator: it is used by "wrapping" another, more specific filter (like for e.g. `PartialSearchFilter`
459+
or `ExactFilter`).
460+
461+
The real power emerges when you combine these decorators. For instance, to create an "autocomplete" feature that finds
462+
exact matches in one of several fields. Example of usage:
463+
464+
```php
465+
// api/src/ApiResource/Chicken.php
466+
467+
#[GetCollection(
468+
parameters: [
469+
'autocomplete' => new QueryParameter(
470+
filter: new FreeTextQueryFilter(new OrFilter(new ExactFilter())),
471+
properties: ['name', 'ean']
472+
),
473+
],
474+
)]
475+
class Chicken
476+
{
477+
//...
478+
}
479+
```
480+
481+
Given that the endpoint is `/chickens`, you can filter chickens by name with the following query:
482+
`/chikens?autocomplete=tom`.
483+
484+
**Result**:
485+
486+
This request will return all chickens where:
487+
488+
- the `name` is exactly "FR123456"
489+
- OR
490+
- the `ean` is exactly "FR123456".
491+
296492
## Date Filter
297493

298494
The date filter allows filtering a collection by date intervals.

0 commit comments

Comments
 (0)