我有一个名为 Careers 的页面,其中列出了所有带有标签的职位。单击一个工作并转到他们的描述后,我想显示一个类似工作的列表。为此,我正在尝试使用 Orchard 的标签模块来获取所有标签名称并将其放入搜索构建器中。
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();
}
}
}
但是,此代码似乎不起作用。请指教。
提前致谢!