OutOfMemory exception when evaluating NumFound

435 Views Asked by At

I have implemented Solr.net search in our project, and currently there are 660k docs in the index (size ~185 Mb).

Unfortunately, solr throws OutOfMemory exception every now and then:

Error searching for contacts System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at System.Text.StringBuilder.ToString() at System.IO.StreamReader.ReadToEnd() at SolrNet.Impl.SolrConnection.ReadResponseToString(IHttpWebResponse response) in c:\prg\SolrNet\SolrNet\Impl\SolrConnection.cs:line 213 at SolrNet.Impl.SolrConnection.GetResponse(IHttpWebRequest request) in c:\prg\SolrNet\SolrNet\Impl\SolrConnection.cs:line 199 at SolrNet.Impl.SolrConnection.Get(String relativeUrl, IEnumerable'1 parameters) in c:\prg\SolrNet\SolrNet\Impl\SolrConnection.cs:line 149 at SolrNet.Impl.SolrQueryExecuter'1.Execute(ISolrQuery q, QueryOptions options) in c:\prg\SolrNet\SolrNet\Impl\SolrQueryExecuter.cs:line 672 at SolrNet.Impl.SolrBasicServer'1.Query(ISolrQuery query, QueryOptions options) in c:\prg\SolrNet\SolrNet\Impl\SolrBasicServer.cs:line 98 at SolrNet.Impl.SolrServer'1.Query(ISolrQuery query, QueryOptions options) in c:\prg\SolrNet\SolrNet\Impl\SolrServer.cs:line 49 at SolrNet.Impl.SolrServer`1.Query(ISolrQuery q) in c:\prg\SolrNet\SolrNet\Impl\SolrServer.cs:line 88 at SearchService.Search.SolrSearch.SearchCount(String queryString, Boolean exactSearch) in c:\Projects\SearchService\Search\SolrSearch.cs:line 240

But the code is very simple:

public ISolrOperations<ContactForSearch> SolrInstance
{
    get
    {
        if (!_initialized)
        {
            Startup.Init<ContactForSearch>(ConfigurationManager.AppSettings.Get("SolrPath"));
            _initialized = true;
        }

        return ServiceLocator.Current.GetInstance<ISolrOperations<ContactForSearch>>();
    }
}

public virtual int SearchCount(string queryString, bool exactSearch)
{
    return SolrInstance.Query(GetGeneralSearchQuery(queryString, exactSearch)).NumFound;
}

It is possible that there are thousands of contacts to be returned with some queries, but I thought that NumFound should just get the count? So, how can I fix this problem? Thanks in advance for all advices!

1

There are 1 best solutions below

1
Daniel On BEST ANSWER

It looks like you're using the .NET client. In my experience, when you do a Solr query using the .NET client it will return the entire document from Solr not just the bit of the query you're interested in. You can try limiting your query to return 0 actual rows as this should still return the correct NumFound value.

Eg:

return SolrInstance.Query(query, new QueryOptions{ Rows = 0 }).NumFound;