Series 5: Sulu for developers Chapter 11 of 16

Search in Sulu 3 (SEAL)

Development
Applies to: every Sulu website As of Sulu 3.0

In short

Sulu 3 searches with SEAL. The adapter is set in SEAL_DSN, the schema website in the package search. Fields get into the index through the tag sulu.search.field, extra data through enhancers in metadata. You can only filter by resource, language, webspace and address.

Sulu 3 has rebuilt search from the ground up. The MassiveSearchBundle is gone, and the packages of Sulu now talk to the library SEAL. This chapter shows how the example website works with it and what you can find in a hit.

Helpful first: 5.1 Sulu 3 at a glance

Adapter and schema

SEAL (CmsIg\Seal) is a layer above several search engines. The environment variable SEAL_DSN decides which one runs. The example website uses Loupe, a search written in PHP on an SQLite basis, without a server of its own:

.env of the example website bash
SEAL_DSN=loupe://%kernel.project_dir%/var/indexes

The configuration is in config/packages/cmsig_seal.yaml: the directory config/schemas takes schemas of your own (in the example website it is empty), the adapter comes from SEAL_DSN. In the test environment every index gets a prefix so that parallel test runs do not get in each other's way. The schemas of Sulu are in vendor/sulu/sulu/packages/search/config/schemas: admin.php for search in the admin and website.php for the website.

vendor/sulu/sulu/packages/search/config/schemas/website.php php
return new Index('website', [
    'id' => new Field\IdentifierField('id'),
    'resourceKey' => new Field\TextField('resourceKey', searchable: false, filterable: true),
    'resourceId' => new Field\TextField('resourceId', searchable: false),
    'locale' => new Field\TextField('locale', searchable: false, filterable: true),
    'webspaces' => new Field\TextField('webspaces', multiple: true, searchable: false, filterable: true),
    'title' => new Field\TextField('title'),
    'url' => new Field\TextField('url', searchable: false, filterable: true),
    'content' => new Field\TextField('content', multiple: true),
    'mediaId' => new Field\IntegerField('mediaId'),
    'authoredAt' => new Field\DateTimeField('authoredAt'),
    'metadata' => new Field\JsonObjectField('metadata'),
]);

What goes into the index

When indexing, Sulu collects the fields of a template that carry the tag sulu.search.field (WebsitePageReindexContentEnhancer and WebsiteArticleReindexContentEnhancer). A field without the tag is not searchable. The title building block of the example website carries it, and so does the text field of the text block. With role="title", page templates mark the title field:

From config/templates/includes/groups/options/page-default.xml html
<property name="title" type="text_line" mandatory="true">
    <tag name="sulu.rlp.part"/>
    <tag name="sulu.search.field" role="title" />
</property>

Extra data in the hit

A hit is a SEAL document with the fields of the schema. Anything beyond that goes into metadata. Enhancers do this. The ArticlePreviewEnhancer of the example website implements WebsiteArticleReindexProviderEnhancerInterface and stores heading, subtitle, teaser, reading time and template of an article in metadata.preview. It needs no service entry of its own; autoconfiguration registers it.

From src/Search/ArticlePreviewEnhancer.php, shortened php
final class ArticlePreviewEnhancer implements WebsiteArticleReindexProviderEnhancerInterface
{
    public function enhanceQuery(QueryBuilder $queryBuilder): void
    {
        // request additional columns for the query
        $queryBuilder->addSelect('dimensionContent.templateData');
    }

    public function enhanceDocument(array $queryResult, array $document): array
    {
        $preview = SearchPreview::fromTemplateData($queryResult['templateData']);
        $preview['templateKey'] = $queryResult['templateKey'];

        return SearchPreview::addTo($document, $preview);
    }
}

The search page

The route sulu_search.website_search (path /search) leads to the SearchController in the package search. It reads the parameter q, filters by language and webspace, marks hits in title and content with <mark> and renders the template that the webspace names under <template type="search">. Twig receives two variables: query and hits. You register the route in config/routes_website.yaml.

Reindexing

If you change the schema, an enhancer or the security contexts of the article groups, reindex. For this, the Sulu developers name the command in UPGRADE-3.x.md, once per context:

Rebuild the index for admin and website bash
bin/adminconsole cmsig:seal:reindex
bin/websiteconsole cmsig:seal:reindex

Limits

We checked these points in the example website, in the code of Sulu 3.0.9 and 3.0.11:

  • The controller only accepts q. It offers no further filters.
  • Only resourceKey, locale, webspaces and url can be filtered. Template and category are in metadata, a JsonObjectField without filterable. You cannot filter by them.
  • SEAL knows equality, in, greater and less than, geo conditions as well as and and or, but no prefix comparison. “Everything below /insights” is therefore not possible.
  • Hits have no score. Loupe does not pass it through, and the marshaller only knows schema fields.
  • For the multi-valued field content, _formatted.content is always null. The example website therefore builds excerpt and highlighting itself in SearchResultTwigExtension.

A search limited to the handbook would be possible in two ways: a controller of your own that specifies the addresses of the chapters through Condition::in('url', ...), or a filterable field in the schema with reindexing. We have built neither.

More on this

Official documentation