0
我有一個名爲職業的頁面,列出了所有與他們的標籤發佈的工作。點擊工作並轉到他們的描述後,我想要顯示類似工作列表。爲此,我嘗試使用Orchard的標籤模塊獲取所有標籤名稱並將其放入搜索框中。如何使用標籤服務獲取與Orchard CMS中的內容類型相關聯的標籤?
namespace SimpleJobs.Handlers
{
public class SimpleJobsHandler : ContentHandler
{
//adding IContentManager and IIndexmanager which performs actual search
private readonly IContentManager _contentManager;
private readonly IIndexManager _indexManager;
private readonly ITagService _tagService;
public SimpleJobsHandler(IRepository<SimpleJobsPartRecord> simpleJobsPartRepository,
IContentManager contentManager,
IIndexManager indexManager,
ITagService tagService
)
{
_contentManager = contentManager;
_indexManager = indexManager;
_tagService = tagService;
Filters.Add(StorageFilter.For(simpleJobsPartRepository));
}
//building display shape
protected override void BuildDisplayShape(BuildDisplayContext context)
{
if (context.ContentItem.ContentType == "Jobs")
{
var similarJobs = GetSimilarJobs(context.ContentItem);
if (similarJobs.Any())
{
context.Shape.SimilarJobs = context.New.SimilarJobs(Jobs: similarJobs);
}
}
}
//getting similar jobs using tags
private IEnumerable<ContentItem> GetSimilarJobs(ContentItem displayedJob)
{
StringBuilder tagStringBuilder = new StringBuilder();
var searchBuilder = GetSearchBuilder();
var simpleJobsTags = _tagService.GetTags();
foreach (var tag in simpleJobsTags){
tagStringBuilder.Append(" ");
tagStringBuilder.Append(tag.TagName);
}
var similarItemsIds = searchBuilder
.WithField("type", "Jobs").Mandatory().ExactMatch()
.Parse("simplejobs-tags", tagStringBuilder.ToString()).Mandatory()
.Search()
.Where(h => h.ContentItemId != displayedJob.Id)
.Select(h => h.ContentItemId)
.Take(5).ToList();
return _contentManager.GetMany<ContentItem>(similarItemsIds, VersionOptions.Published, QueryHints.Empty);
}
//to check if the user has lucene type implementer enabled otherwise
//return a nullsearchbuilder instead of an error
private ISearchBuilder GetSearchBuilder()
{
return _indexManager.HasIndexProvider()
? _indexManager.GetSearchIndexProvider().CreateSearchBuilder("Search")
: new NullSearchBuilder();
}
}
}
但是這段代碼似乎不起作用。請指教。
在此先感謝!
感謝您的幫助! –
很高興爲你效勞! – Piedone