Enabling Search and Indexing for Guestbooks
Step 2 of 6
First, update your build.gradle
to have all of the necessary imports.
-
Open the
build.gradle
file in yourguestbook-service
project. -
Add the Search Service Provider Interface and API dependencies to the
build.gradle
file:compileOnly group: "com.liferay", name: "com.liferay.portal.search.spi", version: "2.0.0" compileOnly group: "com.liferay", name: "com.liferay.portal.search.api", version: "2.0.0"
-
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
:
-
Create a new package in the
guestbook-service
module project’ssrc/main/java
folder calledcom.liferay.docs.guestbook.search
. In this package, create a new class calledGuestbookSearchRegistrar
and populate it with two methods,activate
anddeactivate
.@Component(immediate = true) public class GuestbookSearchRegistrar { @Activate protected void activate(BundleContext bundleContext) { _serviceRegistration = modelSearchRegistrarHelper.register( Guestbook.class, bundleContext, modelSearchDefinition -> { modelSearchDefinition.setDefaultSelectedFieldNames( 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); modelSearchDefinition.setModelIndexWriteContributor( modelIndexWriterContributor); modelSearchDefinition.setModelSummaryContributor( modelSummaryContributor); }); } @Deactivate protected void deactivate() { _serviceRegistration.unregister(); }
The annotations
@Activate
andDeactivate
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. -
Specify the service references for the class:
@Reference(target = "(indexer.class.name=com.liferay.docs.guestbook.model.Guestbook)") protected ModelIndexerWriterContributor<Guestbook> modelIndexWriterContributor; @Reference protected ModelSearchRegistrarHelper modelSearchRegistrarHelper; @Reference(target = "(indexer.class.name=com.liferay.docs.guestbook.model.Guestbook)") 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. -
Add the imports by Organizing Imports (Ctrl-Shift-O).
-
Export the
com.liferay.docs.guestbook.search
package in theguestbook-service
module’sbnd.bnd
file. The export section should look like this:Export-Package: com.liferay.docs.guestbook.search
The Guestbook search and indexing class registration is completed. Next write the search and indexing logic.