0

I'm using Sitecore 9.0.2 with Azure Search and Solr.

I have the weirdest search issue I've seen in a while. Take the following steps:

  1. Restart the application (iisreset or similar)
  2. Rebuild the search index (completes successfully)
  3. Change an item and publish
  4. View the index directly and see the item was updated
  5. Load a page that executes a search query and see the expected items returned
  6. Change an item and publish
  7. View the index directly again and see the item was not updated
  8. Restart the application again
  9. View the index directly and see the item was updated

I could execute steps 3 and 4 over and over and the updates continue to flow to the index until I execute step 5.

Below is my code:

public IEnumerable<ISitecoreItem> GetRelatedItems(IDataSource dataSource)
{
    var relatedItems = new List<ISitecoreItem>();
    using (var index = ContentSearchManager.GetIndex(indexName))
    {
        var remainingMaxItems = MaxItems;
        foreach (var root in dataSource.RelatedRootItems)
        {
            relatedItems.AddRange(GetItemsFromRoot(index, root, remainingMaxItems, currentItem.Language.Name));
            if (relatedItems.Count >= MaxItems)
            {
                break;
            }

            remainingMaxItems = MaxItems - relatedItems.Count;
        }
    }
    return relatedItems;
}

private IEnumerable<ISitecoreItem> GetItemsFromRoot(ISearchIndex index, ISitecoreItem root, int maxItems, string language)
{
    var relatedItems = new List<ISitecoreItem>();
    using (var context = index.CreateSearchContext())
    {
        var filterPredicate = PredicateBuilder.True<SearchResultItem>();

        // filter to only items based on the current page's language
        filterPredicate = filterPredicate.And(x => x.Language == language);

        // ensure root node
        filterPredicate = filterPredicate.And(x => x.Path.StartsWith(root.FullPath));

        // apply filters
        var query = context.GetQueryable<SearchResultItem>().Filter(filterPredicate);

        // apply paging (get number of results)
        query = query.Page(0, maxItems);

        // get results and map
        var results = query.GetResults();
        var relatedItemResults = results.Hits.Select(x => MapSearchResultToGlassSitecoreItem(x.Document)).ToList();
        relatedItems.AddRange(relatedItemResults);
    }
    return relatedItems;
}

And my index configuration:

<index id="local_azure_web_index" type="Sitecore.ContentSearch.Azure.CloudSearchProviderIndex, Sitecore.ContentSearch.Azure" patch:source="Sitecore.ContentSearch.Azure.Index.Web.ExcludeFields.config">
    <param desc="name">$(id)</param>
    <param desc="connectionStringName">cloud.search</param>
    <param desc="totalParallelServices">5</param>
    <param ref="contentSearch/indexConfigurations/databasePropertyStore" desc="propertyStore" param1="$(id)"/>
    <configuration ref="contentSearch/indexConfigurations/defaultCloudIndexConfiguration">
        <documentOptions ref="contentSearch/indexConfigurations/defaultCloudIndexConfiguration/documentOptions" patch:source="Sitecore.ContentSearch.Azure.Index.Web.ExcludeFields.config">
            <exclude hint="list:AddExcludedField">
                <!-- omitted for brevity -->
            </exclude>
        </documentOptions>
    </configuration>
    <schemaBuilder ref="contentSearch/searchServiceSchemaBuilder"/>
    <searchService type="Sitecore.ContentSearch.Azure.Http.CompositeSearchService, Sitecore.ContentSearch.Azure"/>
    <strategies hint="list:AddStrategy">
        <strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/onPublishEndAsync"/>
    </strategies>
    <commitPolicyExecutor type="Sitecore.ContentSearch.CommitPolicyExecutor, Sitecore.ContentSearch">
        <policies hint="list:AddCommitPolicy">
            <policy type="Sitecore.ContentSearch.ModificationCountCommitPolicy, Sitecore.ContentSearch">
                <Limit>1000</Limit>
            </policy>
        </policies>
    </commitPolicyExecutor>
    <locations hint="list:AddCrawler">
        <crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
            <Database>web</Database>
            <Root>/sitecore</Root>
        </crawler>
    </locations>
    <enableItemLanguageFallback>false</enableItemLanguageFallback>
    <enableFieldLanguageFallback>false</enableFieldLanguageFallback>
</index>

What I've tried:

  • Rebuilding the index after the issue happens (it just returns immediately with no items updated)
  • Changing the indexing strategy
  • Disabling switch on rebuild
  • Waiting a really long time to see if the index ever updates (overnight; it didn't)

What could be causing this?

0

The Solution

The issue is with this line of code:

using (var index = ContentSearchManager.GetIndex(indexName))

Remove its using block and it will work:

var index = ContentSearchManager.GetIndex(indexName);

Why?

When the using block exits it will call .Dispose() on the index which stops indexing:

From Sitecore.ContentSearch.AbstractSearchIndex

public void Dispose()
{
    this.Dispose(true);
    this.isDisposed = true;
    GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool isDisposing)
{
    if (this.isDisposed)
    {
        return;
    }
    this.StopIndexing();
    TypeActionHelper.Call<IDisposable>((IDisposable d) => d.Dispose(), this.crawlers);
    this.isDisposed = true;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.