Content Tree Search may be slow and cause OutOfMemoryException


Description

The Content Tree Search performance depends on the size of the data stored in the search index. Search operations might take dozens of seconds if a search condition is too generic. In an extreme case, a search operation might cause an unhandled exception. The following message can be found in the logs:

ERROR Invalid search query: sitecore
Exception: System.OutOfMemoryException
Message: Exception of type 'System.OutOfMemoryException' was thrown.
Source: mscorlib
   at System.Text.StringBuilder.ToString()
   at SolrNet.Impl.SolrConnection.ReadResponseToString(IHttpWebResponse response)
   at SolrNet.Impl.SolrConnection.GetResponse(IHttpWebRequest request)
   at SolrNet.Impl.SolrConnection.Get(String relativeUrl, IEnumerable`1 parameters)
   at SolrNet.Impl.SolrQueryExecuter`1.Execute(ISolrQuery q, QueryOptions options)
   at Sitecore.ContentSearch.SolrProvider.LinqToSolrIndex`1.ExecuteQuery(SolrCompositeQuery compositeQuery, QueryOptions options)
   at Sitecore.ContentSearch.SolrProvider.LinqToSolrIndex`1.FindElements[TElement](SolrCompositeQuery compositeQuery)
   at Sitecore.ContentSearch.Linq.Parsing.IndexQueryable`2.GetEnumerator()
   at System.Linq.Enumerable.<TakeWhileIterator>d__27`1.MoveNext()
   at Sitecore.ContentSearch.Client.Pipelines.Search.SearchContentSearchIndex.ExecuteQuery(SearchArgs args, IQueryable`1 query)
   at Sitecore.ContentSearch.Client.Pipelines.Search.SearchContentSearchIndex.PerformSearch(SearchArgs args)

Solution

Option 1

To limit the maximum number of search results in a response, reduce the value of the ContentSearch.SearchMaxResults setting. We recommend that you set the value to 500 or less by creating a configuration patch file in the \App_Config\Include\zzz folder. For example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" >
  <sitecore role:require="Standalone or ContentManagement">
    <settings>
      <setting name="ContentSearch.SearchMaxResults" set:value="500" />
    </settings>
  </sitecore>
</configuration>

Note that the change affects all search queries generated by Sitecore. Make sure that the logic of your application does not rely on fetching more results in one request.

Option 2

Use the Search Tab instead of Content Tree Search. The Search Tab uses paging that allows to load search results as required. By default, the Search Tab returns 20 results per a search request.

Note that the Search Tab limits the search scope to the subtree of the selected item. To search through the entire Сontent Tree, select the /sitecore root item.

Option 3

To resolve the issue: