Forum Discussion
Ways to reduce size of SharePoint Online Search KQL (Keyword Query Language) POST query?
Hi all,
I've set up a SharePoint Online (not on-site) Search API POST HTTP request using Keyword Query Language for the query text.
The problem is; the query is quite large, and I'm trying to think of ways to reduce the size of the query whilst still maintaining the correct search results.
Reason being is there's a 4096 character limit on KQL Search queries, stated here:
https://learn.microsoft.com/en-gb/sharepoint/search-limits?redirectSourcePath=%252fen-us%252farticle%252fSearch-limits-for-SharePoint-Online-7c06e9ed-98b6-4304-a900-14773a8fa32f
An example of the query (it's searching for specific items within the same List and NOT returning those ones, unless the user is linked to that item.
For example, let's say valueX is a user of which an item is linked to (RefinableString1), and RefinableString2 is the state of the item, and there are many different states an item can be in, hence the value1,2,3 etc. Thus, we only want to return items in all these states that are linked to valueX user, non that are linked to others, and then we want to return all other items that are in a state not listed in the query that are linked to both valueX and other users):
SPSiteURL:\"https://<tenant>.sharepoint.com/sites/<site>\"
AND
(NOT((NOT (RefinableString1:\"valueX\"))
AND
(RefinableString2=\"value1\")
AND
(RefinableString2=\"value2\")
AND
(RefinableString2=\"value3\")
AND
(RefinableString2=\"value4\")
AND
(RefinableString2=\"value5\")
AND
(RefinableString2=\"value6\")
AND
(RefinableString2=\"value7\")
AND
(RefinableString2=\"value8\")
AND
(RefinableString2=\"value9\")
AND
(RefinableString2=\"value10\")
AND
(RefinableString2=\"value11\")
AND
(RefinableString2=\"value12\")
AND
(RefinableString2=\"value13\")
AND
(RefinableString2=\"value14\")
AND
(RefinableString2=\"value15\")
AND
(RefinableString2=\"value19\")
AND
(RefinableString2=\"value16\")
AND
(RefinableString2=\"value17\")
AND
(RefinableString2=\"value18\")))"
and this goes on until it goes past the 4096 character limit for the query text. Is there a way to achieve the same thing but not be required to list the managed property multiple times per operand and just list the managed property once in the query? e.g.:
RefinableString2=\"value18\" OR \"value17\" OR \"value16\"
etc.
Obviously that doesn't work, but is there something similar? Or any other tips that could reduce the
query length to consistently keep it well under 4096 characters?
I've tried inversing the query but that would make the query longer.
Please let me know if you need more clarification.
Thank you.
Edit: To add, I have tried wildcards (*), the problem is each state (RefinableString2) begins with a different value but the end of the value is the same. Unfortunately, KQL doesn't allow prefix wildcards, only suffixed.