Long filter expression results in Bad request from Episerver find

Vote:
 

We have a search query in which we insert a rather long filter expression using the below method:

public static ITypeSearch<TSource> ExcludeEntryCodes<TSource>(this ITypeSearch<TSource> search, IEnumerable<string> codes) where TSource : IBaseEntry
        {
            if (codes != null && codes.Any())
            {
                var codeFilter = search.Client.BuildFilter<TSource>();
                foreach (var code in codes)

                {
                    codeFilter = codeFilter.And(x => !x.Code.Match(code));
                }
                return search.Filter(codeFilter);
            }
            return search;
        }

Our issue is that if the list of "codes" are to long (around 20), Episerver Find returns a bad request. Any ways of optimizing the request?

In the error message is seems that the request towards Find gets rather verbose. Something like:

{\"term\":{\"ParentCatalogNodes.ID$$number\":1073741948}},{\"and\":[{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"9391\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"9413\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"14018\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"10117\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"10230\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"10980\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"17609\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"9926\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"10074\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"13682\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"10808\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"10739\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"10038\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"14179\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"17467\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"9513\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"9554\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"9595\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"2358\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"2361\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"9961\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"9964\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"9967\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"3936\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"3951\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"1894\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"1902\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"9931\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"11744\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"6679\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"6711\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"6441\"}}}},{\"not\":{\"filter\":{\"term\":{\"Code$$string\":\"6746\"}
#222910
May 13, 2020 16:40
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.