Enabling Search and Indexing for Guestbooks
Step 3 of 3
Whenever a guestbook database entity is added, updated, or deleted, the search
index must be updated accordingly. The Liferay DXP annotations @Indexable
and
@IndexableType
mark your service methods so documents can be updated or
deleted. You must update the addGuestbook
, updateGuestbook
, and
deleteGuestbook
service methods with these annotations.
-
Open
GuestbookLocalServiceImpl
in theguestbook-service
module’scom.liferay.docs.guestbook.service.impl
package, and add the following annotation above the method signature for theaddGuestbook
andupdateGuestbook
methods:@Indexable(type = IndexableType.REINDEX) public Guestbook addGuestbook(...) @Indexable(type = IndexableType.REINDEX) public Guestbook updateGuestbook(...)
The
@Indexable
annotation indicates that an index update is required following the method execution. TheGuestbookIndexer
controls exactly how the indexing happens. Setting the@Indexable
annotation type toIndexableType.REINDEX
updates the document in the index that corresponds to the updated guestbook. -
Add the following annotation above the method signature for the
deleteGuestbook
method:@Indexable(type = IndexableType.DELETE) public Guestbook deleteGuestbook(...)
When a guestbook is deleted from the database, its document shouldn’t remain in the search index. This ensures that it is deleted.
-
Add the necessary imports:
import com.liferay.portal.kernel.search.Indexable; import com.liferay.portal.kernel.search.IndexableType;
Save the file.
-
In the Gradle Tasks pane on the right-hand side of Liferay Developer Studio, double-click
buildService
inguestbook-service
→build
. This re-runs Service Builder to incorporate your changes toGuestbookLocalServiceImpl
.
Great! Next, you’ll enable search and indexing for guestbook entries.