Sitecore 8.1: Switching from Lucene to SOLR - Getting no results from SearchContext

380 Views Asked by At

I have switched to SOLR search in Sitecore 8.1 and everything is fine (indices rebuild normally). However, when I search for items using the SearchContext I get no results when I filter according to item.Path.Contains("") although it worked using Lucene. If I remove the item.Path.Contains(""), I get results. So, why doesn't it work? This is the code used:

        List<NewsSearchResultItem> results = new List<NewsSearchResultItem>();
        var index = ContentSearchManager.GetIndex("sitecore_web_index");

        using (IProviderSearchContext context = index.CreateSearchContext())
        {
            var tempResults = context.GetQueryable<NewsSearchResultItem>()
                   .Where(item => item.Path.Contains("/sitecore/content/News"))
                   .Take(10);

            results = tempResults.ToList();
        }

        return results.Select(s => new NewsViewModel(s.GetItem())).ToList();
2

There are 2 best solutions below

0
On BEST ANSWER

Solr with Sitecore stores text fields as lowercase fields by default.

Change your Where clause to use path/toLower():

var pathToLower = "/sitecore/content/news";
... .Where(item => item.Path.Contains(pathToLower))

Also remember to add filters to use your current language only - there might be some documents in Solr for Sitecore items despite the fact that they don't have any versions in a particular language.

0
On

This could be as Marek said, a lower case issue, However you can also try to use Paths property instead of Path, as following:

var tempResults = context.GetQueryable<NewsSearchResultItem>()
                   .Where(item => item.Paths.Contains(Sitecore.Data.ID.Parse("News Item ID (GUID)")))
                   .Take(10);