Indexing Entries

Enabling Search and Indexing for Entries

Step 2 of 5

To control how Entry objects are translated into search engine documents, create these classes in the search package:

  1. ModelDocumentContributor: The main searchable fields for Entries are Name and Message. The Guestbook name associated with the entry is indexed, too.

  2. ModelIndexerWriterContributor configures the batch indexing behavior for Entries. This code is executed when Entries are re-indexed from the Search administration section of the Control Panel.

  3. A new interface, EntryBatchReindexer, with its implementation, EntryBatchReindexerImpl. These classes contain code to ensure that Entries are re-indexed when their Guestbook is updated.

Implementing ModelDocumentContributor

Create EntryModelDocumentContributor and populate it with this:

        immediate = true,
        property = "",
        service = ModelDocumentContributor.class
public class EntryModelDocumentContributor
    implements ModelDocumentContributor<Entry> {

    public void contribute(Document document, Entry entry) {
        try {
            Locale defaultLocale = PortalUtil.getSiteDefaultLocale(

            document.addDate(Field.MODIFIED_DATE, entry.getModifiedDate());
            document.addText("entryEmail", entry.getEmail());

            String localizedTitle = LocalizationUtil.getLocalizedName(
    Field.TITLE, defaultLocale.toString());
            String localizedContent = LocalizationUtil.getLocalizedName(
    Field.CONTENT, defaultLocale.toString());

            document.addText(localizedTitle, entry.getName());
            document.addText(localizedContent, entry.getMessage());

            long guestbookId = entry.getGuestbookId();

            Guestbook guestbook = _guestbookLocalService.getGuestbook(

            String guestbookName = guestbook.getName();

            String localizedGbName = LocalizationUtil.getLocalizedName(
    Field.NAME, defaultLocale.toString());

            document.addText(localizedGbName, guestbookName);
        } catch (PortalException pe) {
            if (_log.isWarnEnabled()) {
                _log.warn("Unable to index entry " + entry.getEntryId(), pe);
        } catch (Exception e) {

    private static final Log _log = LogFactoryUtil.getLog(

    private GuestbookLocalService _guestbookLocalService;


As with Guestbooks, add the localized values for fields that might be translated. The Site locale is appended to the field (e.g., title_en_US), so the field gets passed to the search engine and goes through the right analysis and tokenization.

Implementing ModelIndexerWriterContributor

Create EntryModelIndexerWriterContributor and populate it with this:

        immediate = true,
        property = "",
        service = ModelIndexerWriterContributor.class
public class EntryModelIndexerWriterContributor
    implements ModelIndexerWriterContributor<Entry> {

    public void customize(
        BatchIndexingActionable batchIndexingActionable,
        ModelIndexerWriterDocumentHelper modelIndexerWriterDocumentHelper) {

        batchIndexingActionable.setPerformActionMethod((Entry entry) -> {
            Document document = modelIndexerWriterDocumentHelper.getDocument(


    public BatchIndexingActionable getBatchIndexingActionable() {
        return dynamicQueryBatchIndexingActionableFactory.getBatchIndexingActionable(

    public long getCompanyId(Entry entry) {
        return entry.getCompanyId();

    protected DynamicQueryBatchIndexingActionableFactory

    protected EntryLocalService entryLocalService;


The interesting work is done in the customize method, where all Entries are retrieved and added to a collection.

Implementing EntryBatchReindexer

Create a new interface class, EntryBatchReindexer, with one method called reindex:


public interface EntryBatchReindexer {

    public void reindex(long guestbookId, long companyId);


Then create the implementation class, EntryBatchReindexerImpl:

@Component(immediate = true, service = EntryBatchReindexer.class)
public class EntryBatchReindexerImpl implements EntryBatchReindexer {

    public void reindex(long guestbookId, long companyId) {
        BatchIndexingActionable batchIndexingActionable =

        batchIndexingActionable.setAddCriteriaMethod(dynamicQuery -> {
            Property guestbookIdPropery = PropertyFactoryUtil.forName(



        batchIndexingActionable.setPerformActionMethod((Entry entry) -> {
            Document document = indexerDocumentBuilder.getDocument(entry);



    @Reference(target = "(")
    protected IndexerDocumentBuilder indexerDocumentBuilder;

    @Reference(target = "(")
    protected IndexerWriter<Entry> indexerWriter;


The `reindex` method of the interface is called when a Guestbook is updated.
The entry documents are re-indexed to include the update Guestbook title.

Once the re-indexing behavior is in place, move on to the code for controlling how Entry documents are queried from the search engine.

« Registering Entries with the Search FrameworkQuerying for Entry Documents »
Was this article helpful?
0 out of 0 found this helpful