This is the third episode in the series “
SharePoint Search Queries Explained - The Series”. See the intro post for links to all episodes.
The basis for doing query transformations in Result Sources, Query Rules and in search web parts is to specify KQL queries, and doing so dynamically is a big bonus. This is where Query Variables come into play. Knowing KQL syntax and variable usage will enable you to craft a query for just about anything – as long as you also know the managed properties to query against which is a topic by itself.
As an example the
Local SharePoint Results result source is specified as
{?{searchTerms} -ContentClass=urn:content-class:SPSPeople}
The variable parts are the ones enclosed with braces {}.
{searchTerms} will for example be substituted with the query terms coming in. If you search for
author:"mikael svenson"
the final query sent to the search engine will be expanded to
author:"mikael svenson" -ContentClass=urn:content-class:SPSPeople
As for the
{?..} notation enclosing the full statement, this means that if the variables inside this statement are blank or empty then everything between
{? and the matching end brace
} will be omitted from the expanded query. If an empty query comes in, the final query is blank and not
-ContentClass=urn:content-class:SPSPeople
as everything is removed due to
{searchTerms} being empty. This is a very powerful and important notation to know. Use it around any variable which might be
empty to ensure a
valid query syntax, and I will gives several samples later in this episode as well.