Introduction
eZ Platform exposes very powerful Search API, allowing both Full Text search and querying the content repository using several built-in Criteria and Sort Clauses. These are supported across several search engines, allowing you to plug in another search engine without changing your code.
Available Search Engines
Currently 3 search engines exists on their own eZ Platform Bundles:
- Legacy, a database-powered search engines for basic needs.
- Solr, an integration providing better overall performance, much better scalability and support for more advance search capabilities RECOMMENDED
- ElasticSearch, similar to Solr integration, however currently not under active development EXPERIMENTAL, NOT SUPPORTED
Usage
Search Criteria and Sort Clauses
Search Criteria and Sort Clauses are value
object classes used for building Search Query, to define filter criteria and ordering of the result set. eZ Platform provides a number of standard Criteria and Sort Clauses that you can use out of the box and that should cover the majority of use cases.
Search Engine Handling of Search Criteria and Sort Clauses
As Search Criteria and Sort Clauses are value
objects which are used to define the Query from API perspective, they are are common for all storage engines. Each storage engine needs to implement its own handler
for corresponding Criterion and Sort Clause value
object, which will be used to translate the value object into storage specific search query.
Custom Criteria and Sort Clauses
Sometimes you will find that standard Search Criteria and Sort Clauses provided with eZ Publish are not sufficient for you needs. Most often this will be the case if you have developed a custom Field Type using external storage, which therefore can not be searched using standard Field Criterion.
In this case you can implement a custom Criterion or Sort Clause, together with the corresponding handlers for the storage engine you are using.
Difference between Content and Location Search
These are two basic types of searches, you can either search for Locations or for Content. Each has dedicated methods in Search Service:
Type of search | Method in Search Service |
---|
Content | findContent() |
Content | findSingle() |
Location | findLocations() |
All Criteria and Sort Clauses will be accepted with Location Search, but not all of them can be used with Content Search. Reason for this is that while one Location always has exactly one Content item, one Content item can have multiple Locations. In this context some Criteria and Sort Clauses would produce ambiguous queries and such will therefore not be accepted by Content Search.
Content Search will explicitly refuse to accept Criteria and Sort Clauses implementing these abstract classes:
After you have implemented your Criterion / Sort Clause and its handler, you will need to configure the handler for the service container using dedicated service tags for each type of search. Doing so will automatically register it and handle your Criterion / Search Clause when it is given as a parameter to one of the Search Service methods.
Available tags for Criterion handlers in Legacy Storage Engine are:
ezpublish.search.legacy.gateway.criterion_handler.content
ezpublish.search.legacy.gateway.criterion_handler.location
Available tags for Sort Clause handlers in Legacy Storage Engine are:
ezpublish.search.legacy.gateway.sort_clause_handler.content
ezpublish.search.legacy.gateway.sort_clause_handler.location
Example of registering ContentId Criterion handler, common for both Content and Location Search
Example of registering Depth Sort Clause handler for Location Search
Criteria Independence Example
With eZ Publish you can have multiple location content. Criterions do not relate to others criterion you can use the Public API and Criterion to search this content, it can lead to a comportement you are not expecting.
Example of Criterions not relating to each other:
There is a Content with two Locations: Location A and Location B
- Location A is visible
- Location B is hidden
Searching with LocationId criterion with Location B id and Visibility criterion with Visibility::VISIBLE will return the Content because conditions are satisfied:
- Content has Location B
- Content is visible (it has Location A that is visible)
Reindexing
To (re)create the search engine index for configured search engines (per siteaccess repository), use the php app/console ezplatform:reindex
command.
Reference
Search Criteria Reference
See the dedicated page with Search Criteria reference.
Sort Clauses Reference
See the dedicated page with Sort Clauses reference.