Enabling Search and Indexing for Guestbook Entries
Step 2 of 2
Whenever a guestbook entry is added, updated, or deleted, the corresponding
document should also be updated or deleted. A minor update to each of the
addEntry
, updateEntry
, and deleteEntry
service methods for guestbook
entries is all it takes.
Follow these steps to update the methods:
-
Open
EntryLocalServiceImpl
in theguestbook-service
module’scom.liferay.docs.guestbook.service.impl
package, and add the annotation@Indexable(type = IndexableType.REINDEX)
above the signature for theaddEntry
andupdateEntry
methods:@Indexable(type = IndexableType.REINDEX) public Entry addEntry(...) @Indexable(type = IndexableType.REINDEX) public Entry updateEntry(...)
The
@Indexable
annotation indicates that an index update is required following method execution. TheEntryIndexer
controls exactly how the indexing happens. Setting the@Indexable
annotation’s type toIndexableType.REINDEX
updates the document in the index that corresponds to the updated entry. -
Add the
@Indexable(type = IndexableType.DELETE)
annotation above the signature for thedeleteEntry
method. The indexable typeIndexableType.DELETE
ensures that the entry is deleted from the index:@Indexable(type = IndexableType.DELETE) public Entry deleteEntry(...)
-
Add the required 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 toEntryLocalServiceImpl
.
Awesome! Both guestbooks and their entries now have search and indexing support in the back-end. Next, you’ll enable search in the Guestbook portlet’s front-end.