RE: Override layout indexer in Liferay Quarterly release

Jamie Sammons, modified 1 Year ago. New Member Posts: 4 Join Date: 3/19/24 Recent Posts

Hi Team,

I am trying to override indexer for Layout(Pages). This is what I have written in order to override Indexer. 

@Component(immediate = true, service =Indexer.class)
public class LayoutIndexer extends BaseIndexer<Layout> {

	private static final String NODE = "node";
	private static final String INDEXABLE = "Indexable";
	private static final String ROBOTS = "robots";
	private static final String LAYOUT_ID = "layoutId";
	private static final String SEO_KEYWORDS = "seo_keywords";
	private static final String FRIENDLY_URL = "friendlyUrl";
	private static final String CLASS_NAME = Layout.class.getName();
	
	public static final String FIELD_ENTRY_MODEL_CLASS_NAME =
			"entryModelClassName";

	private static final Log _log = LogFactoryUtil.getLog(LayoutIndexer.class);

	public LayoutIndexer() {
		setDefaultSelectedFieldNames(LAYOUT_ID, Field.COMPANY_ID, FRIENDLY_URL, Field.ENTRY_CLASS_NAME,
				Field.ENTRY_CLASS_PK, Field.GROUP_ID, Field.MODIFIED_DATE, Field.SCOPE_GROUP_ID, Field.NAME, Field.UID);
		setFilterSearch(true);
		setPermissionAware(true);

	}

	@Override
	public String getClassName() {
		return CLASS_NAME;
	}

	@Override
	public void postProcessSearchQuery(BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
			SearchContext searchContext) throws Exception {

		addSearchTerm(searchQuery, searchContext, FRIENDLY_URL, false);
		addSearchTerm(searchQuery, searchContext, LAYOUT_ID, false);
		addSearchTerm(searchQuery, searchContext, Field.NAME, true);
		addSearchTerm(searchQuery, searchContext, Field.TITLE, true);
		addSearchTerm(searchQuery, searchContext, Field.DESCRIPTION, true);
		addSearchTerm(searchQuery, searchContext, SEO_KEYWORDS, true);
		addSearchTerm(searchQuery, searchContext, ROBOTS, true);
		
		addSearchTerm(searchQuery, searchContext, INDEX_ENABLE_FIELD_NAME,true );  
		//super.postProcessSearchQuery(searchQuery, fullQueryBooleanFilter, searchContext);
	}

	@Override
	protected void doDelete(Layout layout) throws Exception {
		deleteDocument(layout.getCompanyId(), layout.getPlid());
		if (_log.isDebugEnabled()) {
			_log.debug("Deleting Layout " + layout.getNameCurrentValue());
		}
	}

	@Override
	protected Document doGetDocument(Layout layout) throws Exception {

		if (_log.isDebugEnabled()) {
			_log.debug("Indexing Layout " + layout);
		}

		Document document = getBaseModelDocument(CLASS_NAME, layout);

		document.addKeyword(FRIENDLY_URL, layout.getFriendlyURL());
		document.addLocalizedKeyword(Field.NAME, layout.getNameMap());
		document.addLocalizedKeyword(Field.TITLE, layout.getTitleMap());
		document.addLocalizedKeyword(Field.DESCRIPTION, layout.getDescriptionMap());
		document.addLocalizedKeyword(SEO_KEYWORDS, layout.getKeywordsMap());
		document.addKeyword(INDEX_ENABLE_FIELD_NAME, true);  
		document.addLocalizedKeyword(ROBOTS, layout.getRobotsMap());
		
		if (_log.isDebugEnabled()) {
			_log.debug("Document " + layout + " indexed successfully");
		}
		return document;
	}

	@Override
	protected Summary doGetSummary(Document document, Locale locale, String snippet, PortletRequest portletRequest,
			PortletResponse portletResponse) throws Exception {
		Summary summary = createSummary(document);
		summary.setMaxContentLength(200);
		return summary;
	}

	@Override
	protected void doReindex(String className, long classPK) throws Exception {
		Layout layout = LayoutLocalServiceUtil.fetchLayout(classPK);
		_log.info("Inside reindex method");
		doReindex(layout);
	}

	@Override
	protected void doReindex(Layout layout) throws Exception {
		_log.debug("do Reindex Method called ::");
		if (!layout.getType().equals(NODE)) {
			_log.info("Do Reindex method Layout URL::"+layout.getFriendlyURL());
			if (isIndexable(layout)) {
				_log.info("Adding Index for" + layout.getNameCurrentValue());
				Document document = getDocument(layout);
				//Document document = doGetDocument(layout);
				//#7.4-changes chaged method updateDocument to IndexWriterHelperUtil.updateDocument
				_indexWriterHelper.updateDocument(layout.getCompanyId(), document);
				
				/*
				 * updateDocument(getSearchEngineId(), layout.getCompanyId(), document,
				 * isCommitImmediately());
				 */

			} else {
				_log.debug("Removing Index for" + layout.getNameCurrentValue());
				deleteDocument(layout.getCompanyId(), layout.getPlid());
			}
		}
	}

	private boolean isIndexable(Layout layout) {
		_log.debug("Check layout is indexable or not :: "+layout.getFriendlyURL());
		boolean isIndexable = false;
		try {
			Serializable dataSerializable = ExpandoValueLocalServiceUtil.getData(layout.getCompanyId(),
					Layout.class.getName(), ExpandoTableConstants.DEFAULT_TABLE_NAME, INDEXABLE, layout.getPlid());
			if (dataSerializable != null) {

				isIndexable = (boolean) dataSerializable;
			}
		} catch (PortalException e) {
			isIndexable = false;
			_log.error("Exception Occurred while retrieving expando value: ", e);
		}
		if (_log.isDebugEnabled()) {
			_log.debug("Layout " + layout.getNameCurrentValue() + " isIndexable: " + isIndexable);
		}
		return isIndexable;
	}

	@Override
	protected void doReindex(String[] ids) throws Exception {
		long companyId = GetterUtil.getLong(ids[0]);

		reIndexLayouts(companyId);
	}

	private void reIndexLayouts(long companyId) throws PortalException {
		_log.debug("Re-Index Layouts Method called ::");
		final IndexableActionableDynamicQuery indexableActionableDynamicQuery = _layoutLocalService
				.getIndexableActionableDynamicQuery();

		indexableActionableDynamicQuery.setCompanyId(companyId);
		indexableActionableDynamicQuery
				.setPerformActionMethod(
						(Layout layout) -> {

						try {
							if (!layout.getType().equals(NODE)) {
							_log.debug("Layout Eligible for index" + layout.getFriendlyURL());
								if (isIndexable(layout)) {
								_log.debug("Adding Index for" + layout.getNameCurrentValue());
									indexableActionableDynamicQuery.addDocuments(
											getDocument(layout));
								} else {
							_log.debug("Removing Index for" + layout.getNameCurrentValue());
									deleteDocument(layout.getCompanyId(), layout.getPlid());
								}
							}
						} catch (PortalException pe) {
							if (_log.isWarnEnabled()) {
								_log.warn("Unable to index layout " + layout.getNameCurrentValue(), pe);
							}
						} catch (Exception e) {
							if (_log.isWarnEnabled()) {
								_log.warn("Failed to delete index for  " + layout.getNameCurrentValue(), e);
							}
						}
				});
		indexableActionableDynamicQuery.performActions();
	}
	
	@Reference
	private LayoutLocalService _layoutLocalService;
	
	@Reference
	private IndexWriterHelper _indexWriterHelper;
}

Problem is this indexer is called only at the time of doing full reindex. If I am trying to update any page this indexer is not getting called. Even Just indexing layout as well is also not going into this indexer.

 

Any help is appreciated.

Thanks,

Hetal Prajapati

Gustavo Lima, modified 1 Year ago. New Member Post: 1 Join Date: 6/8/22 Recent Posts

Hey Hetal, I tested using the same code and it is working as expected, which version are you working?

Olaf Kock, modified 7 Months ago. New Member Posts: 4 Join Date: 3/19/24 Recent Posts

Hi Gustavo,

I am testing it in 2024.q1.4 quarterly release. Problem is it's calling indexer only at the time of full reindex when we change single page it's not going into custom indexer.

Regards,

Hetal