索引文档时如何在 Liferay DXP 中设置映射类型?

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

我们在 Liferay 中有一个自定义实体,我们也可以在弹性搜索中对其进行索引。 liferay在elasticsearch中默认设置所有索引文档的映射类型为"LiferayDocumentType",我们需要将其改为"PublicationType"。下图是我们在elasticsearch中的文档,高亮的字段就是我们需要修改的。

以下class是我们的索引器class发布。

@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;
}

可以使用 Java API 更改类型,但我们正在寻找基于 Liferay Elastic API 的解决方案。非常感谢您抽出宝贵的时间和宝贵的信息。

不修改 Liferay 搜索模块代码就无法更改 Elasticsearch 文档类型,请参阅:

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

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

我们可以看到文档类型是 elasticsearch API 调用中使用的常量。 如果更改它,更改将应用​​于所有索引。我认为不重写大量代码就可以将其更改为仅一个实体