Registering Guestbooks with the Search Framework

Enabling Search and Indexing for Guestbooks

Step 2 of 6

First, update your build.gradle to have all of the necessary imports.

  1. Open the build.gradle file in your guestbook-service project.

  2. Add the Search Service Provider Interface and API dependencies to the build.gradle file:

    compileOnly group: "com.liferay", name: "", version: "2.0.0"
    compileOnly group: "com.liferay", name: "", version: "2.0.0"
  3. Save the file and run Refresh Gradle Project.

Once the dependency is configured, register the Search services that build the entity’s ModelSearchDefinition.

A *SearchRegistrar specifies the classes that the entity uses to contribute to building a ModelSearchDefinition. Activation of the SearchRegistrar component results in a cascade of activity in the search framework, culminating with the building of a DefaultIndexer. The DefaultIndexer is registered under the class name defined in the registrar, and then used for indexing/searching objects of that class.

Create the GuestbookSearchRegistrar:

  1. Create a new package in the guestbook-service module project’s src/main/java folder called In this package, create a new class called GuestbookSearchRegistrar and populate it with two methods, activate and deactivate.

    @Component(immediate = true)
    public class GuestbookSearchRegistrar {
        protected void activate(BundleContext bundleContext) {
            _serviceRegistration = modelSearchRegistrarHelper.register(
                Guestbook.class, bundleContext, modelSearchDefinition -> {
                        Field.ASSET_TAG_NAMES, Field.COMPANY_ID, Field.CONTENT,
                        Field.ENTRY_CLASS_NAME, Field.ENTRY_CLASS_PK,
                        Field.GROUP_ID, Field.MODIFIED_DATE, Field.SCOPE_GROUP_ID,
                        Field.TITLE, Field.UID);
        protected void deactivate() {

    The annotations @Activate and Deactivate ensure each method is invoked as soon as the Component is started (activated) or when it’s about to be stopped (deactivated). On activation of the Component, a chain of search and indexing classes is registered for the Guestbook entity. Set the default selected field names used to retrieve results documents from the search engine. Then set the contributors used to build a model search definition.

  2. Specify the service references for the class:

        @Reference(target = "(")
        protected ModelIndexerWriterContributor<Guestbook> modelIndexWriterContributor;
        protected ModelSearchRegistrarHelper modelSearchRegistrarHelper;
        @Reference(target = "(")
        protected ModelSummaryContributor modelSummaryContributor;
        private ServiceRegistration<?> _serviceRegistration;

    Target the Guestbook model while looking up a reference to the contributor classes. Later, when you create these contributor classes, you’ll specify the model name again to complete the circle.

  3. Add the imports by Organizing Imports (Ctrl-Shift-O).

  4. Export the package in the guestbook-service module’s bnd.bnd file. The export section should look like this:


The Guestbook search and indexing class registration is completed. Next write the search and indexing logic.

« Understanding Search and IndexingIndexing Guestbooks »
Was this article helpful?
0 out of 0 found this helpful