1
votes

Update

well turns out my configuration was just not working, i reverted back to using the defaultindexconfiguration from the contentsearch definitions by sitecore.

once i did that, everything worked magically. i just added template conditions to my queries and all is good.


been banging my head at this for 2 days now and i'm getting no where.

The scenario:

I have articles that are tagged with items from a tree/multilist.

I have an index defined to look throughout my site for a particular template type, and then store some values (name, description, path, tags). This index uses a small indexConfiguration. I am avoiding the default index configuration as that one bloats up my indexes too much, i just need a slim set of results.

<myConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneSearchConfiguration, Sitecore.ContentSearch.LuceneProvider">
    <defaultIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
        <indexAllFields>true</indexAllFields>
        <Analyzer ref="contentSearch/configuration/defaultIndexConfiguration/analyzer" />
        <fieldMap type="Sitecore.ContentSearch.FieldMap, Sitecore.ContentSearch">
            <fieldNames hint="raw:AddFieldByFieldName">
                <field fieldName="Article Tags" storageType="yes" indexType="tokenized" vectorType="no" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
                <field fieldName="Teaser Title" storageType="yes" indexType="tokenized" vectorType="no" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
                <field fieldName="Teaser Description" storageType="yes" indexType="tokenized" vectorType="no" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
                <field fieldName="Article Date" storageType="yes" indexType="tokenized" vectorType="no" boost="1f" type="System.DateTime" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
                <field fieldName="Views" storageType="yes" indexType="tokenized" vectorType="no" boost="1f" type="System.Int32" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
                <field fieldName="__Workflow state" storageType="yes" indexType="untokenized" vectorType="no" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
            </fieldNames>
        </fieldMap>
        <fields hint="raw:AddComputedIndexField">
            <field fieldName="isfinal" storageType="yes" indexType="tokenized">Core.Search.ComputedWorkflowState, Core</field>
            <field fieldName="Teaser Image" storageType="yes" indexType="tokenized">Core.Search.ComputedTeaserImage, Core</field>
            <field fieldName="Article Url" storageType="yes" indexType="tokenized">Core.Search.ComputeUrl, Core</field>
        </fields>
        <include hint="list:IncludeTemplate">
            <articlePage>{28432890-0F71-4E2F-8577-7848F90FCBCC}</articlePage>
        </include>
    </defaultIndexConfiguration>
</myConfiguration>

By looking at the index using Luke, i can see the tags are indexed properly. each tag has its own row in the document.

I create a class(ArticleItem) that extends SearchResultItem, and then i implemented the appropriate attributes with decoration.

[IndexField("article_tags")]
public List<ID> tags{get;set;}

Now i try to make a query using sitecore linq, and a predicate builder.

using (var context = ContentSearchManager.GetIndex(indexName).CreateSearchContext())
{
    IQueryable<ArticleItem> query = context.GetQueryable<ArticleItem>();

    var predicate = PredicateBuilder.True<ArticleItem>();

    foreach (var id in tags)
    {
        var tempTerm = id;
        predicate = predicate.Or(p => p.Tags.Contains(id));
    }

    var results = context.GetQueryable<ArticleItem>().Where(predicate).GetResults();

    if (results != null)
    {
        if (results.Hits.Any())
        {
            return results.Hits.Select(x => x.Document);
        }
    }
}

when i call context.GetQueryable() by itself, then iterate through the results, the tags are all present, and there are definitely matches.

have i misconfigured something?

*additional information

testing the search with the additional queries: p.Tags.Contains("3") doesnt return any results (there is definitely an item with a guid that contains the char 3.

my termcount for article tags is 0 when looking via luke.

by turning the field into a computed field, my search results are now working (although the values are stored as an entire string instead of being tokenized) and the term counts are appearing.

3

3 Answers

0
votes

If you simplify your predicate to a simple check for one of the tags, do you get any results?

var results = context.GetQueryable<ArticleItem>().Where(p=>p.Tags.Contains(tags[0])).GetResults();

This will help you identify if your predicate building is the problem.

If that also doesn't work, have you checked to make sure the values in the 'tags' collection being passed in are valid equivalences to the values stored on the object? It could be the 'Contains' check is failing because the values in your collection aren't matches to the values loaded onto your object.

Another way to debug is to remove the 'Where' clause from your queryable and get the results of the whole collection. Once you've selected all the documents and have a full list of all the items, can you build a Where clause that filters down the Item list using your tags collection?

If this also doesn't work, this is probably confirmation that something is wrong either with the input list of 'tags' or with the Where clause itself.

0
votes

I think there are a couple things to check. You mentioned that your tags field is being populated in the search Index so we know it isn't a indexing/crawling issue.

First look in your Sitecore Search log so that you can determine how the search Query is being formatted. You are defining your search field as:

<field fieldName="Article Tags" storageType="yes" indexType="tokenized" vectorType="no" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />

but then you are defining your field in your model as:

[IndexField("article_tags")]

It could just be a field name mismatch in that sitecore is searching on "articles_taqs" but the index has it stored as "Article Tags". When I mocked this up Sitecore was sending over the underscore.

The other thing to check is the format of your GUID. Using .Contains(ID) is searching on a lower case GUID with no dashes or curly braces. Using LUKE take a look at how things are stored in your index and then compare that to how Sitecore is searching for it in the Sitecore Search log.

0
votes

Change your 'tags' property to this:

[IndexField("article_tags"), TypeConverter(typeof(IndexFieldEnumerableConverter))]
public virtual System.Collections.Generic.IEnumerable<ID> tags{ get; set; }

I think this could fix your problem.