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

时间:2017-08-15 17:15:03

标签: elasticsearch lucene liferay liferay-dxp

我们在Liferay中有一个自定义实体,我们也可以在弹性搜索中对其进行索引。默认情况下,liferay在弹性搜索中将所有索引文档的映射类型设置为“LiferayDocumentType”,我们需要将其更改为“PublicationType”。下图显示了弹性搜索中的文档,突出显示的字段是我们需要更改的内容。

enter image description here

以下类是我们用于发布的索引器类。

@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的解决方案。我非常感谢您的时间和宝贵的信息。

1 个答案:

答案 0 :(得分:0)

在不修改Liferay搜索模块代码的情况下更改Elasticsearch文档类型是不可能的,请参阅:

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

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

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