Search & Filter Conditions#
In the Getting Started documentation we already saw how we can use the SearchBuilder
to
search for different documents in our indexes.
Beside search functionality the abstraction provides also different kind of filter conditions to build also complex overview pages for e-commerce or other kind of applications.
The following shows the basic usage as already shown in the “Getting Started” documentation. Under the
$this->engine
variable we assume that you have already injected your created Engine
instance.
<?php
use Schranz\Search\SEAL\Search\Condition;
$result = $this->engine->createSearchBuilder()
->addIndex('blog')
->addFilter(/* ... */)
->getResult();
foreach ($result as $document) {
// do something with the document
}
$total = $result->total();
Note
Currently only the Elasticsearch
and Opensearch
adapters supports to search on
multiple indexes at once. The other adapters are not yet supporting to call addIndex
multiple times and will fail so with an exception if you try to do so.
Conditions#
SearchCondition#
The SearchCondition
is the most basic condition and can be used to search for a specific:
<?php
use Schranz\Search\SEAL\Search\Condition;
$result = $this->engine->createSearchBuilder()
->addIndex('blog')
->addFilter(new Condition\SearchCondition('Search Term'))
->getResult();
The condition does only search on fields which are marked as searchable
in the index configuration.
EqualCondition#
The EqualCondition
is used to filter the result by a specific field value matching a given value.
<?php
use Schranz\Search\SEAL\Search\Condition;
$result = $this->engine->createSearchBuilder()
->addIndex('blog')
->addFilter(new Condition\EqualCondition('tags', 'UI'))
->getResult();
The field is required to be marked as filterable
in the index configuration, it can be also
used on fields which are not marked as multiple
.
NotEqualCondition#
The NotEqualCondition
is used to filter the result by a specific field value not matching a given value.
<?php
use Schranz\Search\SEAL\Search\Condition;
$result = $this->engine->createSearchBuilder()
->addIndex('blog')
->addFilter(new Condition\NotEqualCondition('tags', 'UI'))
->getResult();
The field is required to be marked as filterable
in the index configuration, it can be also
used on fields which are not marked as multiple
.
IdentifierCondition#
The IdentifierCondition
is a special kind of EqualCondition
on the identifier field,
if you want to load a document by its identifier this condition is faster in most search engines
then using a EqualCondition
.
<?php
use Schranz\Search\SEAL\Search\Condition;
$result = $this->engine->createSearchBuilder()
->addIndex('blog')
->addFilter(new Condition\IdentifierCondition('23b30f01-d8fd-4dca-b36a-4710e360a965'))
->getResult();
GreaterThanCondition#
The GreaterThanCondition
is used to filter the result by a specific field value be greater than (>
)
the given value.
<?php
use Schranz\Search\SEAL\Search\Condition;
$result = $this->engine->createSearchBuilder()
->addIndex('blog')
->addFilter(new Condition\GreaterThanCondition('rating', 2.5))
->getResult();
The field is required to be marked as filterable
in the index configuration.
GreaterThanEqualCondition#
The GreaterThanEqualCondition
is used to filter the result by a specific field value be greater than equal (>=
)
the given value.
<?php
use Schranz\Search\SEAL\Search\Condition;
$result = $this->engine->createSearchBuilder()
->addIndex('blog')
->addFilter(new Condition\GreaterThanEqualCondition('rating', 2.5))
->getResult();
The field is required to be marked as filterable
in the index configuration.
LessThanCondition#
The LessThanCondition
is used to filter the result by a specific field value be less than equal (<
)
the given value.
<?php
use Schranz\Search\SEAL\Search\Condition;
$result = $this->engine->createSearchBuilder()
->addIndex('blog')
->addFilter(new Condition\LessThanCondition('rating', 2.5))
->getResult();
The field is required to be marked as filterable
in the index configuration.
LessThanEqualCondition#
The LessThanEqualCondition
is used to filter the result by a specific field value be less than equal (<=
)
the given value.
<?php
use Schranz\Search\SEAL\Search\Condition;
$result = $this->engine->createSearchBuilder()
->addIndex('blog')
->addFilter(new Condition\LessThanEqualCondition('rating', 2.5))
->getResult();
The field is required to be marked as filterable
in the index configuration.
Filter on Objects and Typed Fields#
To filter on Objects
and Typed
fields you need to use the .
symbol
as a separator between the object and the field.
For example for a document like this where the rating value is filterable:
<?php
$document = [
'rating' => [
'value' => '1.5'
],
];
Need to be queried this way <object>.<field>:
<?php
use Schranz\Search\SEAL\Search\Condition;
$result = $this->engine->createSearchBuilder()
->addIndex('blog')
->addFilter(new Condition\LessThanEqualCondition('rating.value', 2.5))
->getResult();
To filter on Typed
objects also the . symbol is used but the type name need to be included as well.
For example for a document like this where header media is filterable:
<?php
$document = [
'header' => [
'type' => 'image',
'media' => 1
],
];
Need to be queried this way <object>.<type>.<field>:
<?php
use Schranz\Search\SEAL\Search\Condition;
$result = $this->engine->createSearchBuilder()
->addIndex('blog')
->addFilter(new Condition\EqualCondition('header.image.media', 21))
->getResult();
Also nested objects and types can be queried the same way.
Pagination#
Beside the searches and filters you can also limit the result by a given limit
and/or offset
.
<?php
$result = $this->engine->createSearchBuilder()
->addIndex('blog')
->addFilter(/* ... */)
->limit(10)
->offset(20)
->getResult();
With the limit
and offset
also a basic pagination can be created this way:
<?php
$page = 1; // get from query parameter
$pageSize = 10;
$result = $this->engine->createSearchBuilder()
->addIndex('blog')
->addFilter(/* ... */)
->limit($pageSize)
->offset(($page - 1) * $pageSize)
->getResult();
$total = $result->total();
$maxPage = ceil($total / $pageSize) ?: 1;
foreach ($result as $document) {
// do something with the document
}
Sorting#
The abstraction can also be used to create complex overview pages where you not only can search or filter
your results but also sort
them by a given field.
<?php
use Schranz\Search\SEAL\Search\Condition;
$result = $this->engine->createSearchBuilder()
->addIndex('blog')
->addSortBy('rating', 'desc')
->getResult();
<?php
use Schranz\Search\SEAL\Search\Condition;
$result = $this->engine->createSearchBuilder()
->addIndex('blog')
->addSortBy('rating', 'asc')
->getResult();
The field is required to be marked as sortable
in the index configuration.
Summary#
After reading this documentation you should have a basic understanding how to use the abstraction to manage Indexes, add and remove Documents and how to search and filter the results. You should now be ready to start using the abstraction for your different kind of needs.
Missing something? Let us know by creating an issue on our Github Repository.