5

I'm using Sitecore 8.2 Update 1 as PaaS on Azure. I've added some new code that is giving me a strange error.

Edit: Added more complete code

using (var context = ContentSearchManager.CreateSearchContext(new SitecoreIndexableItem(Sitecore.Context.Item)))
{
    var query = context.GetQueryable<SearchResultItem>()
        .Where(item => item.TemplateId == SitecoreItemIds.ISSUE_TEMPLATE &&
                    item.Path.StartsWith("/sitecore/content/Home/" + issueTypePath))
        .OrderByDescending(item => item["Date"])
        .FirstOrDefault();

        if (query != null)
            return query.GetItem();

        return null;
}

And I'm getting this YSOD error:

Index (EMPTY) was not found

Description: An unhandled exception occurred.

Exception Details: Sitecore.ContentSearch.Exceptions.IndexNotFoundException: Index (EMPTY) was not found

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

I'm using Azure Search as the search provider. This code works on my local machine, but it's not hitting Azure Search.

I have not made any changes to files on Azure that would affect the search.

5
  • 2
    Can you reveal a little more code please? Can you show us how you're getting the index for the context var? Commented Feb 13, 2017 at 21:06
  • 1
    Have you tried rebuilding the search index as well?
    – Trayek
    Commented Feb 14, 2017 at 14:05
  • @PeteNavarra I added additional code that should cover everything. Trayek - I tried rebuilding the sitecore_master_index but I keep getting out of memory exceptions. I may need to bump up my instance to rebuild the index.
    – Kevin
    Commented Feb 15, 2017 at 11:42
  • FYI ... I was able to reindex after switching to an S3 instance.
    – Kevin
    Commented Feb 15, 2017 at 14:47
  • @Kevin have you found solution to that issue ? it's happening for us as well, not consistently but randomly sometimes. I assumed that in some specific scenario when index is not initialized then it's happening and after some refresh it start working mostly after 2-3 refresh. Commented Dec 16, 2018 at 7:01

1 Answer 1

2

The issue that I believe you are running into, is that no index is being defined. As part of the Search context, an index has to be identified.

Here's an example query highlighting the index selection:

var index  = ContentSearchManager.GetIndex(“sitecore_web_index”);
using (var searchContext = index.CreateSearchContext())
{
var blogPosts = searchContext.GetQueryable<SearchResultItem>()
.Where(i => i.TemplateName == “Blog Post”);
}

Notice how I'm first defining which index I need to use, and then from the index, creating a search context.

Hopefully this helps you.

8
  • An index is dynamically resolved from the SitecoreIndexableItem and CreateSearchContext() calls GetIndex so the correct context should be returned. The code the OP posted is correct and should work. getfishtank.ca/en/blog/…
    – jammykam
    Commented Feb 15, 2017 at 12:23
  • The clue i was basing off of is the error. It seems that it can't resolve the index. I agree that it should work, but what I'm thinking is that because it can't resolve the index, one has to be specified. Commented Feb 15, 2017 at 12:37
  • Agree that specifying the index may solve the issue, but it does not resolve the underlying cause.
    – jammykam
    Commented Feb 15, 2017 at 13:24
  • Agreed. I'll do some testing my Azure instance to see if I can replicate issue. Thanks for your insight. Commented Feb 15, 2017 at 13:43
  • I updated my code according to your suggestion and it seems to have resolved that particular error, but now I'm not getting any results. This does not happen locally and I cannot attach a remote debugging session.
    – Kevin
    Commented Feb 15, 2017 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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