我们在 Liferay 中有一个自定义实体,我们也可以在弹性搜索中对其进行索引。默认情况下,liferay 在弹性搜索中将所有索引文档的映射类型设置为“LiferayDocumentType”,我们需要将其更改为“PublicationType”。下图是我们在elasticsearch中的文档,高亮的字段就是我们需要修改的地方。
以下类是我们用于发布的索引器类。
@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 的解决方案。我非常感谢您的时间和有关此的宝贵信息。