elasticsearchluceneliferayliferay-dxp

How to set the Mapping Type in Liferay DXP while Indexing a document?


We have a Custom entity in Liferay, also we are able to index it in the elastic search. By default, liferay sets the mapping type of all the indexed document as "LiferayDocumentType" in elastic search, we need to change it as "PublicationType". The following picture shows our document in elastic search and the highlighted field is what we need to change.

enter image description here

The following class is our indexer class for publication.

@Component(
immediate = true,
property  = { "indexer.class.name=com.ctc.myct.bo.model.Publication" },
service   = Indexer.class)

public class StanlyPublicationIndexer extends BaseIndexer<Publication> {

private static final String CLASS_NAME = Publication.class.getName();
private static final String PORTLET_ID = "Corporate Portlet";
private static final Log    _log       = LogFactoryUtil.getLog(StanlyPublicationIndexer.class);
String                      example    = "This is an example";
byte[]                      bytes      = example.getBytes();

public StanlyPublicationIndexer() {
    setFilterSearch(true);
    setPermissionAware(true);
}

@Override
protected void doDelete(Publication object) throws Exception {
    Document doc = getBaseModelDocument(PORTLET_ID, object);

    IndexWriterHelperUtil.deleteDocument(this.getSearchEngineId(), object.getCompanyId(), object.getUuid(), true);
}

@Override
protected Document doGetDocument(Publication object) throws Exception {

    Document doc      = getBaseModelDocument(PORTLET_ID, object);
    User     user     = UserLocalServiceUtil.getUser(PrincipalThreadLocal.getUserId());
    long     userid   = user.getUserId();
    String   username = user.getScreenName();

    object.setUserId(userid);
    object.setUserName(username);
    doc.addKeyword(Field.USER_ID, userid);
    doc.addText(Field.USER_NAME, username);
    doc.addText("title", object.getTitle());
    doc.addText("firstName", object.getFirstName());
    doc.addText("lastName", object.getLastName());
    doc.addText("additional_Information", object.getAdditionalInformation());
    doc.addKeyword("roleId", object.getRoleId());
    doc.addNumber("publicationId", object.getPublicationId());
    doc.addNumber("articleId", object.getJournalArticleId());

    Field field = new Field("_type");

    _log.info("The document with title" + " " + object.getTitle() + " " + "and firstName" + " "
              + object.getFirstName() + " " + "has been created successfully ");

    return doc;
}

@Override
protected Summary doGetSummary(Document document, Locale locale, String snippet, PortletRequest portletRequest,
                               PortletResponse portletResponse)
        throws Exception {
    return null;
}

@Override
protected void doReindex(Publication object) throws Exception {
    _log.info("doReindex2 is  Executing");

    Document doc = this.doGetDocument(object);

    try {
        IndexWriterHelperUtil.addDocument(this.getSearchEngineId(), object.getCompanyId(), doc, true);
        IndexWriterHelperUtil.commit(this.getSearchEngineId(), object.getCompanyId());
        ;
    } catch (Exception e) {
        e.printStackTrace();
    }

    _log.info("Publication document with publicationId " + " " + object.getPublicationId() + " "
              + " has been successfully reIndexed into the elastic search");
}

@Override
protected void doReindex(String[] arg0) throws Exception {

    // TODO Auto-generated method stub
}

@Override
protected void doReindex(String arg0, long arg1) throws Exception {

    // TODO Auto-generated method stub
}

@Override
public Hits search(SearchContext searchContext) throws SearchException {
    return super.search(searchContext);
}

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

It's possible to change the type using Java API but we are looking for a solution based on Liferay Elastic API's. I really appreciate your time and valuable information about this.


Solution

  • It isn't possible to change Elasticsearch document type without modifying Liferay search module code, see:

    com/liferay/portal/search/elasticsearch/internal/util/DocumentTypes.java

    com/liferay/portal/search/elasticsearch/internal/index/LiferayTypeMappingsConstants.java

    We can see document type is a constant used in elasticsearch API calls. If you change it, the change will apply to all indexes. I don't think it is possible to change it to only one entity without rewriting a lot of code