Creating an Entry Indexer

Enabling Search and Indexing for Guestbook Entries
Step 1 of 2

The EntryIndexer class you’ll complete here is very similar to the GuestbookIndexer class you completed in the previous section. Therefore, the instructions here only point out differences between the indexing of guestbooks and entries.

Follow these steps to create the entry indexer:

  1. In the package of your guestbook-service module project’s src/main/java folder, create a new
    class called EntryIndexer that extends Replace the default contents of with the following code:

    @Component(immediate = true, service = Indexer.class)
    public class EntryIndexer extends BaseIndexer<Entry> {
        public static final String CLASS_NAME = Entry.class.getName();
        public EntryIndexer() {
                Field.COMPANY_ID, Field.ENTRY_CLASS_NAME, Field.ENTRY_CLASS_PK,
                Field.UID, Field.SCOPE_GROUP_ID, Field.GROUP_ID);
            setDefaultSelectedLocalizedFieldNames(Field.TITLE, Field.CONTENT);
        public String getClassName() {
            return CLASS_NAME;
        public boolean hasPermission(
            PermissionChecker permissionChecker, String entryClassName,
            long entryClassPK, String actionId)
            throws Exception {
            return EntryPermission.contains(
                permissionChecker, entryClassPK, ActionKeys.VIEW);
        public void postProcessContextBooleanFilter(
            BooleanFilter contextBooleanFilter, SearchContext searchContext)
            throws Exception {
            addStatus(contextBooleanFilter, searchContext);
        public void postProcessSearchQuery(
            BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
            SearchContext searchContext)
            throws Exception {
            addSearchLocalizedTerm(searchQuery, searchContext, "guestbookName", false);
            addSearchLocalizedTerm(searchQuery, searchContext, Field.TITLE, false);
            addSearchLocalizedTerm(searchQuery, searchContext, Field.CONTENT, false);
        protected void doDelete(Entry entry)
            throws Exception {
            deleteDocument(entry.getCompanyId(), entry.getEntryId());
        protected Document doGetDocument(Entry entry)
            throws Exception {
            Document document = getBaseModelDocument(CLASS_NAME, entry);
            document.addDate(Field.MODIFIED_DATE, entry.getModifiedDate());
            document.addText("email", entry.getEmail());
            Locale defaultLocale =
            String localizedTitle = LocalizationUtil.getLocalizedName(
                Field.TITLE, defaultLocale.toString());
            String localizedMessage = LocalizationUtil.getLocalizedName(
                Field.CONTENT, defaultLocale.toString());
            document.addText(localizedTitle, entry.getName());
            document.addText(localizedMessage, entry.getMessage());
            long guestbookId = entry.getGuestbookId();
            Guestbook guestbook = _guestbookLocalService.getGuestbook(guestbookId);
            String guestbookName= guestbook.getName();
            String localizedGbName = LocalizationUtil.getLocalizedName(
                "guestbookName", defaultLocale.toString());
            document.addText(localizedGbName, guestbookName);
            return document;

    This is not all the code, but it contains the heart of the functionality: the doGetDocument method and its helper methods. The email, date, localized title, and message fields (based on the site’s default language) are indexed. Finally, you get the entry’s guestbook and index the localized version of the guestbookName field. Always support localization where possible–this ensures your entities are searchable in any language.

  2. The rest of the code is very similar to the GuestbookIndexer. Paste in the following code to finish the entry indexer class:

        protected Summary doGetSummary(
            Document document, Locale locale, String snippet,
            PortletRequest portletRequest, PortletResponse portletResponse)
            throws Exception {
            Summary summary = createSummary(document);
            return summary;
        protected void doReindex(Entry entry)
            throws Exception {
            Document document = getDocument(entry);
                getSearchEngineId(), entry.getCompanyId(), document,
        protected void doReindex(String className, long classPK)
            throws Exception {
            Entry entry = _entryLocalService.getEntry(classPK);
        protected void doReindex(String[] ids)
            throws Exception {
            long companyId = GetterUtil.getLong(ids[0]);
        protected void reindexEntries(long companyId)
            throws PortalException {
            final IndexableActionableDynamicQuery indexableActionableDynamicQuery =
                new ActionableDynamicQuery.PerformActionMethod<Entry>() {
                    public void performAction(Entry entry) {
                        try {
                            Document document = getDocument(entry);
                        catch (PortalException pe) {
                            if (_log.isWarnEnabled()) {
                                    "Unable to index entry " + entry.getEntryId(),
        private static final Log _log = LogFactoryUtil.getLog(EntryIndexer.class);
        protected IndexWriterHelper indexWriterHelper;
        private EntryLocalService _entryLocalService;
        private GuestbookLocalService _guestbookLocalService;

As with the guestbook, you must update the entry’s service layer to support indexing when its service methods are called. That’s your next step.

« Enabling Search and Indexing for Guestbook EntriesHandling Indexing in the Entry Service Layer »
Was this article helpful?
0 out of 0 found this helpful