我的总体目标是使用资产条目标签进行分类。我通过向每个具有标签等的页面添加一个 portlet 来实现这一点。
现在我已经开始工作了,我开始实现一个索引器,以便我们可以轻松地搜索页面。我做了如下:
public class TaxonomyMetaIndexer extends com.liferay.portal.kernel.search.BaseIndexer {
public static final String[] CLASS_NAMES = {AssetEntry.class.getName()};
@Override
public String[] getClassNames() {
return CLASS_NAMES;
}
@Override
public String getPortletId() {
return "taxonomymeta_WAR_Portlets61201";
}
@Override
protected void doDelete(Object obj) throws Exception {
AssetEntry entry = (AssetEntry)obj;
// TODO Auto-generated method stub
long companyId = entry.getCompanyId();
long resourcePrimKey = entry.getEntryId();
deleteDocument(companyId,resourcePrimKey);
}
@Override
protected Document doGetDocument(Object obj) throws Exception {
AssetEntry entry = (AssetEntry)obj;
long[] assetCategoryIds = entry.getCategoryIds();
List<AssetCategory> categories = entry.getCategories();
String[] assetCategoryNames = StringUtil.split(ListUtil.toString(categories, "name"));
String[] assetTagNames =entry.getTagNames();
Document document = getBaseModelDocument(getPortletId(), entry);
document.addText(Field.DESCRIPTION, entry.getDescription());
document.addText(Field.TITLE, entry.getTitle());
document.addKeyword(Field.ENTRY_CLASS_PK, Long.toString(entry.getEntryId()));
document.addKeyword(Field.ENTRY_CLASS_NAME,AssetEntry.class.getName());
document.addKeyword(Field.COMPANY_ID, entry.getCompanyId());
document.addKeyword(Field.GROUP_ID, entry.getGroupId());
document.addKeyword(Field.PORTLET_ID, getPortletId());
document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
document.addKeyword(Field.ASSET_CATEGORY_TITLES, assetCategoryNames);
document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
System.out.println();
System.out.println();
System.out.println();
System.out.println(AssetEntry.class.getName());
System.out.println(entry.getTitle());
System.out.println(entry.getDescription());
System.out.println();
System.out.println();
System.out.println();
return document;
}
@Override
protected Summary doGetSummary(Document document, Locale arg1, String snippet,
PortletURL portletURL) throws Exception {
String title = document.get(Field.TITLE);
String content = snippet;
if (Validator.isNull(snippet)) {
content = document.get(Field.DESCRIPTION);
if (Validator.isNull(content)) {
content = StringUtil.shorten(
document.get(Field.CONTENT), 200);
}
}
String resourcePrimKey = document.get(Field.ENTRY_CLASS_PK);
portletURL.setParameter("jspPage", "/html/taxonomymeta/view.jsp");
portletURL.setParameter("resourcePrimKey", resourcePrimKey);
return new Summary(title, content, portletURL);
}
@Override
protected void doReindex(Object obj) throws Exception {
AssetEntry entry = (AssetEntry) obj;
Document document = doGetDocument(entry);
//SearchEngineUtil.updateDocument(lo.getCompanyId(), document);
SearchEngineUtil.updateDocument(SearchEngineUtil.GENERIC_ENGINE_ID, entry.getCompanyId(), document);
}
@Override
protected void doReindex(String[] arg0) throws Exception {
long companyId = GetterUtil.getLong(arg0[0]);
reindexRoot(companyId);
}
@Override
protected void doReindex(String arg0, long classPK) throws Exception {
AssetEntry entry = AssetEntryServiceUtil.getEntry(classPK);
doReindex(entry);
}
@Override
protected String getPortletId(SearchContext arg0) {
return getPortletId();
}
protected void reindexRoot(long companyId) throws Exception {
int groupCount = GroupLocalServiceUtil.getCompanyGroupsCount(companyId);
int groupPages = groupCount / Indexer.DEFAULT_INTERVAL;
for (int i = 0; i <= groupPages; i++) {
int groupStart = (i * Indexer.DEFAULT_INTERVAL);
int groupEnd = groupStart + Indexer.DEFAULT_INTERVAL;
reindexRoot(companyId, groupStart, groupEnd);
}
}
protected void reindexRoot(long companyId, int groupStart, int groupEnd)
throws Exception {
List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(companyId,
groupStart, groupEnd);
for (Group group : groups) {
long groupId = group.getGroupId();
// long folderId =
// BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;
int entryCount =AssetEntryServiceUtil.getCompanyEntriesCount(group.getCompanyId());
int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
for (int i = 0; i <= entryPages; i++) {
int entryStart = (i * Indexer.DEFAULT_INTERVAL);
int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
reindexEntries(companyId, groupId, entryStart, entryEnd);
}
}
}
protected void reindexEntries(long companyId, long groupId, int entryStart,
int entryEnd) throws Exception {
List<AssetEntry> entries = AssetEntryServiceUtil.getCompanyEntries(companyId, entryStart, entryEnd);
if (entries.isEmpty()) {
return;
}
Collection<Document> documents = new ArrayList<Document>();
for (AssetEntry entry : entries) {
Document document = getDocument(entry);
documents.add(document);
}
//SearchEngineUtil.updateDocuments(companyId, documents);
SearchEngineUtil.updateDocuments(SearchEngineUtil.GENERIC_ENGINE_ID, companyId, documents);
}
}
我使用的索引调用是:
String cat = "";
Enumeration<String> names=actionRequest.getParameterNames();
while(names.hasMoreElements())
{
String param = (String) names.nextElement();
if(param.contains("assetCategoryIds"))
{
cat = ParamUtil.getString(actionRequest, param);
}
}
String tag = ParamUtil.getString(actionRequest, "assetTagNames");
String[] tags = tag.split(",");
String[] cats = cat.split(",");
long[] catIds = new long[cats.length];
if(cat.length() > 1)
{
for(int i =0;i < cats.length; i++)
{
catIds[i] = Long.parseLong(cats[i]);
}
}
if(tag.length() == 0)
{
tags = new String[0];
}
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
long id = themeDisplay.getPlid();
long user = Long.parseLong(actionRequest.getRemoteUser());
Layout layout = themeDisplay.getLayout();
long group = layout.getGroupId();
String pageName = layout.getName(themeDisplay.getLocale());
String desc = layout.getDescription();
String url = PortalUtil.getLayoutActualURL(layout);
try {
url = PortalUtil.getLayoutFullURL(themeDisplay);
} catch (PortalException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SystemException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String layoutUuid = layout.getUuid();
try {
AssetEntry entry = AssetEntryLocalServiceUtil.updateEntry(user,group,"Page",id,"",0, catIds,tags,true,null,null,new Date(),null,ContentTypes.TEXT_HTML,pageName,desc,desc,url, layoutUuid, 0,0,0,false);
TaxonomyMetaIndexer index = new TaxonomyMetaIndexer();
Document doc = null;
try {
doc = index.doGetDocument(entry);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
SearchEngineUtil.addDocument(SearchEngineUtil.GENERIC_ENGINE_ID, layout.getCompanyId(), doc);
} catch (PortalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我也尝试过使用
TaxonomyMetaIndexer index = new TaxonomyMetaIndexer();
index.doReindex(entry);
而不是 searchengineutil.addDocument
这似乎运行良好,没有错误。我有 3 个带有资产条目的基本测试页面。
但是,当使用 lukeall 查看 lucene 文件夹索引时,我看不到任何索引。:(
我已经在调试模式下运行了服务器,我可以看到代码运行到索引器中,但没有创建任何索引。
任何帮助深表感谢 !!!!!
谢谢,亚历克斯