SharePoint 2013 SearchServiceException “The maximum allowed value is 4096”

Recently I investigated an issue where a customer was using SharePoint 2013 Search REST APIs and making a post request to get results from SharePoint 2013.  The query worked just fine.  They also had good number of filters (properties) the end-users can use and they were using the HiddenConstraints property to filter the search results based on what the end-user selects.

The problem was that whenever the list of filters increased beyond a point, we hit the following error:

 Unable to retrieve search results: {"error":{"code":"-1, Microsoft.Office.Server.Search.REST.SearchServiceException","message":{"lang":"en-US","value":"The maximum allowed value is 4096\r\nParameter name: HiddenConstraints"}}}

We were able to figure out that this length (in KB) is governed by a property called MaxKeywordQueryTextLength of the search service application (also in KeywordQueryProperties).  Luckily the MaxKeywordQueryTextLength property is “settable” using SharePoint 2013 Management Shell and we increased the value from 4 KB to 8 KB using the below commands.

 PS C:\Users\sridhara> $search = Get-SPServiceApplication | where {$_.displayname -contains "search"}
 PS C:\Users\sridhara> $search.MaxKeywordQueryTextLength
 4096
 PS C:\Users\sridhara> $search.MaxKeywordQueryTextLength = 8192
 PS C:\Users\sridhara> $search.Update()PS C:\Users\sridhara> $search.MaxKeywordQueryTextLength
 8192
  
 ## May not be needed but to ensure everything is intact
  
 PS C:\Users\sridhara> Restart-Service spsearchhostcontroller
 PS C:\Users\sridhara> IISRESET

This change helped in fixing the error “The maximum allowed value is 4096” in this specific scenario.

NOTE that the value for MaxKeywordQueryTextLength can be increased to up to 20 KB ($search.MaxKeywordQueryTextLength = 20480).  Increasing this value beyond 20 KB is not supported.

Here are a few references to leverage SharePoint 2013 Search REST APIs.

REST API reference and samples

SharePoint Search REST API overview

Using the SharePoint 2013 search Query APIs

SharePoint 2013 Search REST API

Good luck with searching using SharePoint 2013 Search REST APIs!