Message Boards

Elastic Search 2.2.0 - Liferay DXP 7.0 not indexingh custom models.

thumbnail
Joaquin Cabal, modified 3 Years ago.

Elastic Search 2.2.0 - Liferay DXP 7.0 not indexingh custom models.

Regular Member Posts: 106 Join Date: 9/7/09 Recent Posts

Hi! 

I have created the @Indexer component to can index the custom model.

Alse created the Asset Renderer and AssetRenderer FActory for custom model-

Anyway the search portlet and the Asset públisher dont find the indexed elements.

 

I have to mention that when I reindex all models, in debug mode, is entering in Indexer class and creating the elastic search documents ok.

Here is the Indexer code.

Any help could be great!

 

package com.mediamonks.cemex.sb.search;

import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
import com.liferay.portal.kernel.dao.orm.IndexableActionableDynamicQuery;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.search.*;
import com.liferay.portal.kernel.search.filter.BooleanFilter;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.LocalizationUtil;
import com.liferay.portal.kernel.util.PortalUtil;
import com.mediamonks.cemex.sb.model.Srv;
import com.mediamonks.cemex.sb.service.SrvLocalService;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import java.util.Locale;


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

	
	private static final Log _log = LogFactoryUtil.getLog(SrvIndexer.class);
	
	public static final String CLASS_NAME = Srv.class.getName();
	
	
	@Reference
    protected IndexWriterHelper indexWriterHelper;

	@Reference
	protected SrvLocalService srvLocalService;
	
	public SrvIndexer() {

        setDefaultSelectedFieldNames(
            Field.COMPANY_ID,Field.MODIFIED_DATE,Field.COMPANY_ID,
				Field.ENTRY_CLASS_NAME, Field.ENTRY_CLASS_PK,Field.CONTENT,
            Field.UID, Field.SCOPE_GROUP_ID, Field.TITLE,
				Field.GROUP_ID);
        setDefaultSelectedLocalizedFieldNames(Field.TITLE, Field.CONTENT);
        setFilterSearch(true);
        setPermissionAware(true);
    }

	
	@Override
	public String getClassName() {
		
		return CLASS_NAME;
	}
	
	
	@Override
	protected void doDelete(Srv srv) throws Exception {
		deleteDocument(srv.getCompanyId(), srv.getSrvId());

	}

	@Override
	protected Document doGetDocument(Srv srv) throws Exception {
		Document document = getBaseModelDocument(CLASS_NAME, srv);


		long classPK = srv.getSrvId();

		document.addUID(CLASS_NAME, classPK);

        document.addDate(Field.MODIFIED_DATE, srv.getModifiedDate());
        Locale defaultLocale =
                PortalUtil.getSiteDefaultLocale(srv.getGroupId());

        
        String localizedTitleName = LocalizationUtil.getLocalizedName(
            Field.TITLE, defaultLocale.toString());
        String localizedContentName = LocalizationUtil.getLocalizedName(
            Field.CONTENT, defaultLocale.toString());

        document.addText(localizedTitleName, srv.getTitle());
        
        document.addText(localizedContentName, srv.getDescription());


        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
	public void postProcessSearchQuery(BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
                                       SearchContext searchContext) throws Exception {
		
	     addSearchLocalizedTerm(searchQuery, searchContext, Field.TITLE, false);
	     addSearchLocalizedTerm(searchQuery, searchContext, Field.CONTENT, false);
		
	}
	
	
	@Override
	public void postProcessContextBooleanFilter(BooleanFilter contextBooleanFilter, SearchContext searchContext)
			throws Exception {
		addStatus(contextBooleanFilter, searchContext);
	}


	@Override
	protected void doReindex(String className, long classPK) throws Exception {
		 
		Srv srv = srvLocalService.getSrv(classPK);
	    doReindex(srv);
		
	}


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


	@Override
	protected void doReindex(Srv srv) throws Exception {
		Document document = getDocument(srv);
        indexWriterHelper.updateDocument(
            getSearchEngineId(), srv.getCompanyId(), document,
            isCommitImmediately());
		
	}
	
	protected void reindexEntries(long companyId)
	        throws PortalException {

	        final IndexableActionableDynamicQuery indexableActionableDynamicQuery =
	        		srvLocalService.getIndexableActionableDynamicQuery();

	        indexableActionableDynamicQuery.setCompanyId(companyId);
	        
	        
	        indexableActionableDynamicQuery.setPerformActionMethod(
	            new ActionableDynamicQuery.PerformActionMethod<Srv>() {

	                @Override
	                public void performAction(Srv srv) {

	                    try {
	                        Document document = getDocument(srv);
	                        indexableActionableDynamicQuery.addDocuments(document);
	                    }
	                    catch (PortalException pe) {
	                        if (_log.isWarnEnabled()) {
	                            _log.warn(
	                                "Unable to index entry " + srv.getSrvId(),
	                                pe);
	                        }
	                    }
	                }
	            });
	        indexableActionableDynamicQuery.setSearchEngineId(getSearchEngineId());
	        indexableActionableDynamicQuery.performActions();
	    }
	
	@Override
	public boolean hasPermission(PermissionChecker permissionChecker, String entryClassName, long entryClassPK,
                                 String actionId) throws Exception {
		
		return true;
	}

	@Override
	protected boolean isVisible(int entryStatus, int queryStatus) {
		return true;
	}
}