Enabling Search and Indexing for Guestbooks
Step 6 of 6
Whenever a Guestbook database entity is added, updated, or deleted, the search
index must be updated accordingly. The Liferay DXP annotation @Indexable
combines with the IndexableType
to mark your service methods so documents can
be updated or deleted. Annotate addGuestbook
, updateGuestbook
,
and deleteGuestbook
service methods.
-
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. The indexing classes control the type of index: 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 Dev Studio DXP, double-click
buildService
inguestbook-service
→build
. This re-runs Service Builder to incorporate your changes toGuestbookLocalServiceImpl
.
Next, you’ll enable search and indexing for Guestbook Entries.