I have a query that's supposed to return related articles to the current article that aren't the current article but are the same category. However, it keeps including the current article in the results. It uses this predicate:
var predNotCurrentItem = PredicateBuilder
.True<BlogSearchResult>()
.And(x => x.ItemId.Guid != currentArticle._Id);
var predPrimaryCategory = predNotCurrentItem.And(x => x.PrimaryCategory == currentArticle.PrimaryCategory._Id);
dest = _blogSearch.GetMostRecentArticles(predPrimaryCategory, count);
Which, by way of the GetMostRecentArticles
function, calls this:
protected TList Search(Expression<Func<TSingle, bool>> predicate = null)
{
TList results;
using (IProviderSearchContext context = SearchIndex.CreateSearchContext())
{
IQueryable<TSingle> query;
if (predicate != null)
{
query = context.GetQueryable<TSingle>().Filter(predicate);
}
else
{
query = context.GetQueryable<TSingle>();
}
results = new TList { Results = query.Where(x => !x.ExcludeFromSearch).ToList().Distinct() };
}
return results;
}
I've confirmed that x.ItemId.Guid
and currentArticle._Id
are returning the correct values, and they're both GUIDs so the comparison should work correctly. I've confirmed that it gets to the Filter()
call as expected.
What am I doing wrong that prevents it from filtering out the article with matching GUID as expected?
(Note: I suspect this is a C# thing rather than a Sitecore thing specifically, but if it matters we're using Sitecore 8.1u3 with Lucene 3.0.3.)
edit 1: adding configuration
Relevant portion of the search configuration:
<fieldMap type="Sitecore.ContentSearch.FieldMap, Sitecore.ContentSearch">
<fieldNames hint="raw:AddFieldByFieldName">
<field fieldName="Article Title" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
<field fieldName="Author" storageType="YES" indexType="UNTOKENIZED" vectorType="NO" boost="1f" type="System.Guid" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
<field fieldName="Primary Category" storageType="YES" indexType="UNTOKENIZED" vectorType="NO" boost="1f" type="System.Guid" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
<field fieldName="Additional Categories" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
<field fieldName="States" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration.Sitecore.ContentSearch.LuceneProvider" />
<field fieldName="Publish Date" storageType="YES" indexType="UNTOKENIZED" vectorType="NO" type="System.DateTime" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
<field fieldName="Keywords" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
<field fieldName="Include on Blog Homepage" storageType="YES" indexType="UNTOKENIZED" vectorType="NO" boost="1f" type="System.Boolean" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
<field fieldName="Include in RSS Feeds" storageType="YES" indexType="UNTOKENIZED" vectorType="NO" boost="1f" type="System.Boolean" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
<field fieldName="Exclude from Related Blog Posts" storageType="YES" indexType="UNTOKENIZED" vectorType="NO" boost="1f" type="System.Boolean" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
</fieldNames>
</fieldMap>
<documentOptions type="Sitecore.ContentSearch.LuceneProvider.LuceneDocumentBuilderOptions, Sitecore.ContentSearch.LuceneProvider">
<indexAllFields>false</indexAllFields>
<include hint="list:AddIncludedTemplate">
<BlogArticle>(guid omitted)</BlogArticle>
</include>
<fields hint="raw:AddComputedIndexField">
<field fieldName="_content" type="MyCompany.Library.Services.Search.Fields.RenderingContentsField, MyCompany.Library" />
</fields>
<include hint="list:AddIncludedField">
<Keywords>(guid omitted)</Keywords>
<ArticleTitle>(guid omitted)</ArticleTitle>
<Author>(guid omitted)</Author>
<PrimaryCategory>(guid omitted)</PrimaryCategory>
<AdditionalCategories>(guid omitted)</AdditionalCategories>
<States>(guid omitted)</States>
<PublishDate>(guid omitted)</PublishDate>
<IncludeOnBlogHomepage>(guid omitted)</IncludeOnBlogHomepage>
<IncludeInRssFeeds>(guid omitted)</IncludeInRssFeeds>
<ExcludeFromRelatedBlogPosts>(guid omitted)</ExcludeFromRelatedBlogPosts>
</include>
</documentOptions>
815912 13:38:30 INFO ExecuteQueryAgainstLucene (custom_blog): +(-exclude_from_search:1 +*:*) - Filter : +(-exclude_from_related_blog_posts:1 +*:*) +(-_group:168a759392f24d099cdc5499256e97e8 +*:*)