0

I'm trying to write a query that searches only items in the media library. The problem appears to be related to the space character in the phrase "media library", which is causing the code to return zero results. When I use a path that doesn't have a space in it, it returns results just fine. How do I query a path and escape that pesky space? Below is a simplified version of the code I'm using. I'm using Sitecore 9 and Solr 6.6.2. Thank you!

using (var context = Sitecore.ContentSearch.ContentSearchManager.CreateSearchContext(new Sitecore.ContentSearch.SitecoreIndexableItem(Sitecore.Context.Item)))
{
    var solrQuery = context.GetQueryable<SearchResultItem>().Where(i => i.Path.StartsWith("/sitecore/media library/subdirectory")).Filter(i => i.Language == "en");
    var results = solrQuery.GetResults();
    count = results.Hits.Count();
    if (count > 0)
    {
        // We've found at least 1!
    }
}
5
  • Which version of Sitecore and Solr do you use? Jun 8 '19 at 4:55
  • Can you please update your question with entry from ContentSearch log at the time you run that code? Jun 8 '19 at 4:56
  • I'm not sure the space is your problem. Jun 8 '19 at 6:31
  • Can this be a cache issue? Jun 8 '19 at 14:29
  • 1
    Just use ID of your subdirectory item instead of path: Where(i => i.Paths.Contains(subridectory.ID)). Jun 9 '19 at 8:22
0

After manually querying Solr, I found that it doesn't actually store the full path of media library items. If Sitecore says the item's path is /sitecore/media library/mysite/public/documents, then the path is actually stored in Solr as /mysite/public/documents, with the /sitecore/media library portion of the path chopped off.

I'm not sure if everyone's Sitecore/Solr installation does this, but this is the way ours works. I've also confirmed that Marek Musielak's suggestion of using

Where(i => i.Paths.Contains(subdirectory.ID))

also works and I may end up using that since it's probably more efficient, so thank you as well!

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.