2013-05-08 108 views
2

我試圖通過AssetEntries實現自定義搜索portlet。目前AssetEntryQuery不允許使用關鍵字進行搜索。我試圖通過FacetedSearcher進行搜索。按關鍵字搜索似乎沒問題。但是,當我試圖通過AssetTagNames搜索時Liferay searchContext按AssetTags和關鍵字搜索

searchContext.setAssetTagNames(assetTagNames) 

它根本不起作用。 這裏是我的一段代碼

SearchContext searchContext = new SearchContext(); 
     Facet assetEntriesFacet = new AssetEntriesFacet(searchContext); 
     assetEntriesFacet.setStatic(true); 
     searchContext.addFacet(assetEntriesFacet); 

     /*MultiValueFacet multiValueFacet=new MultiValueFacet(searchContext); 
     multiValueFacet.setFieldName("assetTagNames"); 
     multiValueFacet.setStatic(false); 
     searchContext.addFacet(multiValueFacet);*/ 

     searchContext.setCompanyId(themeDisplay.getCompanyId()); 
     String []assetTagNames=new String[1]; 
     assetTagNames[0]= assetTagName; 
     searchContext.setAssetTagNames(assetTagNames); 
     searchContext.setKeywords(keywords); 
     String[] entryClassName = {JournalArticle.class.getName()}; 
     searchContext.setEntryClassNames(entryClassName); 

     Indexer indexer = FacetedSearcher.getInstance(); 
     // searchContext.setAndSearch(true); 
     Hits hits = indexer.search(searchContext); 
     System.out.println("Hits: " + hits.getLength()); 

的要求 searchKeyword結果查詢:key1key1 assetTagName:sometag

+(+(companyId:1) +((+(entryClassName:com.liferay.portlet.journal.model.JournalArticle) +(status:0)))) +(assetCategoryTitles:*key1key1* assetCategoryTitles_en_US:*key1key1* assetTagNames:*key1key1* comments:key1key1 content:key1key1 description:key1key1 properties:key1key1 title:key1key1 url:key1key1 userName:*key1key1* classPK:key1key1 content_en_US:key1key1 description_en_US:key1key1 entryClassPK:key1key1 title_en_US:key1key1 type:key1key1) 

正如你看到的ASSETTAG不應用於查詢。 我已經試圖通過

searchContext.setAttribute("assetTagNames",assetTagName); 

進行設置,並評論MultiValueFacet代碼,但王氏沒有結果。 爲了進一步我需要通過dateRange和類別搜索。有沒有人有任何想法?

回答

5

幸運的是解決了這個問題。

如果您想搜索標籤,您必須爲此使用單獨的方面,例如

 MultiValueFacet assetTagsFacet = new MultiValueFacet(searchContext); 
     assetTagsFacet.setFieldName(Field.ASSET_TAG_NAMES); 
     searchContext.addFacet(assetTagsFacet); 

還利用searchContext.setAttribute("assetTagNames", assetTagName);代替searchContext.setAssetTagNames(assetTagName);

對於通過分類搜索同樣的事情:

 MultiValueFacet assetCategoriesFacet = new MultiValueFacet(searchContext); 
     assetCategoriesFacet.setFieldName("assetCategoryTitles"); 
     searchContext.addFacet(assetCategoriesFacet); 
     searchContext.setAttribute("assetCategoryTitles", assetCategoryName); 

此外,我希望通過JournalArticle的自定義類型進行搜索,我已經創造了這個方面,但在查詢中有兩次「類型」。作爲一個解決方案,我使用MultiValueFacet代替AssetEntriesFacet在設置入口時類別名稱

MultiValueFacet assetEntriesFacet = new MultiValueFacet(searchContext); 
    assetEntriesFacet.setFieldName("entryClassName"); 
    searchContext.setAttribute("entryClassName",JournalArticle.class.getName()); 
    searchContext.addFacet(assetEntriesFacet);