Handling Indexing in the Guestbook Service Layer

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.

  1. Open GuestbookLocalServiceImpl in the guestbook-service module’s com.liferay.docs.guestbook.service.impl package, and add the following annotation above the method signature for the addGuestbook and updateGuestbook 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 GuestbookIndexer controls exactly how the indexing happens. Setting the @Indexable annotation type to IndexableType.REINDEX updates the document in the index that corresponds to the updated guestbook.

  2. 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.

  3. Add the necessary imports:

    import com.liferay.portal.kernel.search.Indexable;
    import com.liferay.portal.kernel.search.IndexableType;
    

    Save the file.

  4. In the Gradle Tasks pane on the right-hand side of Liferay Developer Studio, double-click buildService in guestbook-servicebuild. This re-runs Service Builder to incorporate your changes to GuestbookLocalServiceImpl.

Great! Next, you’ll enable search and indexing for guestbook entries.

« Creating a Guestbook IndexerEnabling Search and Indexing for Guestbook Entries »
この記事は役に立ちましたか?
0人中0人がこの記事が役に立ったと言っています