Did you know that you can refine your search queries in SharePoint?
That’s possible with the use of the Keyword Query Language. Unfortunately, not too many people know about this awesome feature.
In this article, I’ll walk you through the important bits of using the Keyword Language Query or KLQ in SharePoint.
Let’s get started.
Table of Contents:
What is the Keyword Query Language (KQL)?
According to Microsoft:
KQL is the default query language for building search queries. Using KQL, you specify the search terms or property restrictions that are passed to the SharePoint search service.
Basically, it’s a language you can use for searching things in SharePoint by using search terms and property restrictions.
A KQL query needs to have at least:
- Free keywords (words or phrases)
- Property restrictions
As for where you can use Keyword Query Language:
- You can use KQL in any search box within SharePoint.
- The other location you can use KQL is in the query form in the highlighted content web part.
Now, using the Keyword Query Language can get quite technical. However, I will only touch on those that you will probably use.
Note: If you want to learn more about searching in SharePoint, check out my article on how SharePoint search works.
How to use the Keyword Query Language
When you use the search feature in SharePoint normally — what you’re actually doing is making use of free-text expressions.
For example, if I search for “sample”, the search results will return all the files and folders that contain that word.
- Search terms (free keywords) are not case-sensitive. It doesn’t distinguish between a capital case and a small case.
- When you enter two or more words that are separated by a space, the search will return results of anything that detects those words in one file or folder name.
- If you want to search for a specific phrase, you need to put the search term in double quotation marks.
Keyword Query Language also has a wildcard feature where you can basically use an asterisk (*) to enable prefix matching.
Here’s how it works:
- You can search for anything by entering a few characters first.
- Then, enter the asterisk symbol (the wildcard operator) to have the system search for anything that starts with the characters you specified.
Unfortunately, KQL doesn’t support suffix matching. That means you can’t put an asterisk first and then search for files that end with specific characters.
On the good side, you can use the wildcard feature even with phrases. But similarly, you need the beginning word or phrase first.
Using property restrictions in KQL
The fact that you can restrict the properties means you can refine the search. This means you can find things more efficiently.
Like if you know who the author was, what type of file you’re searching for, or even the filename itself, you can use it in the search.
The syntax goes like this:
To save you some time thinking through all the syntax, here are three of them you can use:
|Search files through the author|
|Search for a specific file type|
|Search for a specific file name|
Note: When using property restriction, it’s important that there is no space between the property operator and the property value.
If there is a space in between the property operator and the operator value, the search engine will treat the property restriction as a free-text query instead.
Now, in regards to the property names, take note of these things:
- There are several managed properties for documents you can use.
- For crawled properties, you will have to map the crawled property first to a managed property before you can use it.
- Naturally, the managed property has to be a “queryable” property.
Property operators for property restrictions
The colon symbol isn’t the only property operator you can use. There are a few of them — all of which you’re already familiar with.
Below is a table that summarizes these operators:
|Operator||Function||Supported property type|
|: (colon)||Gives you results equal to the property value in the “property store” database||Text, DateTime, Integer, Decimal, Double, YesNo|
|= (equal)||Gives you results equal to the value of the property restriction||Text, DateTime, Integer, Decimal, Double, YesNo|
|< (less than)||Gives you results less than the value specified in the property restriction||DateTime, Integer, Decimal, Double|
|> (greater than)||Gives you results greater than the value specified in the property restriction||DateTime, Integer, Decimal, Double|
|<= (less than or equal to)||Gives you results less than or equal to the value specified in the property restriction||DateTime, Integer, Decimal, Double|
|>= (greater than or equal to)||Gives you results greater than or equal to the value specified in the property restriction||DateTime, Integer, Decimal, Double|
|<> (not equal)||Gives you results not equal to the value specified in the property restriction||DateTime, Integer, Decimal, Text, Double, YesNo|
|.. (double period)||Gives you results that are within the range specified in the property restriction||DateTime, Integer, Decimal, Double|
Searching for date intervals
Did you know that you can also search for date ranges using KQL?
Like you can literally search for everything that was created or modified on a set date, or in a date range that you specify.
Here are keywords you can use to signify the date:
- This week
- This month
- Last month
- This year
- Last year
Note: When using date interval keywords that have more than one word, use double quotation marks (“”).
Searching with multiple property restrictions
You can also combine different property restrictions within the same KQL query to further enhance your search.
When doing this:
- You can combine different types of properties on the search query.
- You can also use the same property multiple times in one query.
- The results depend on the operator you use.
- If you use the “AND” operator, the search will combine the property restriction you used so the results will return “true” for both restrictions.
- If you use the “OR” operator, the search will return results that are true for either of the property restrictions.
Furthermore, you can also use parentheses to group different property restrictions that are related to a specific property.
For example, instead of using:
author:”Mark Anderson” AND author:”Jane Foster”
You can simplify it to:
author:(“Mark Anderson” “Jane Foster”)
Note: Unfortunately, this only works with the “Text” property type. Furthermore, using parentheses lemmatizes the query words.
Advanced KQL Query Operators
I have already included some advanced operators earlier — like the Boolean operators — though not yet been explained thoroughly.
Starting with Boolean, you can use these operators with property restrictions and even with free text expressions.
Below is a table that summarizes each Boolean operator:
|AND||Gives results that return true for all the free text expressions or property restrictions included. Using the plus (+) symbol in the query produces the same result.|
|OR||Gives results that return true for either of the free text expressions or property restrictions included.|
|NOT||Gives results that return false for the free text expressions or property restrictions included. Using the minus (-) symbol in the query produces the same result.|
Here are examples of queries using Boolean operators:
author:”Mark Anderson” AND author:”Jane Foster”
author:”Mark Anderson” OR author:”Jane Foster”
author:”Mark Anderson” NOT filetype:docx
Another advanced KQL query operator is the synonym operator “WORDS”.
It’s used like this:
Some notes regarding this operator:
- This operator will return results that are synonyms to the terms in the query.
- You can only use the synonym operator with free text expressions.
Notes: There are more advanced KQL query operators. However, the ones I specified here are the likely ones you will be using. For more information, check out Microsoft’s documentation page.
Using Keyword Query Language in your search
Though you’re not required to learn KQL in order to use SharePoint properly, knowing KQL can make your work more efficient.
This is especially true if your work includes a lot of searching around SharePoint and using the highlighted content web part.
Do you have any questions regarding the Keyword Query Language? If so, kindly leave those questions in the comment section below.
For inquiries and other concerns, please use the site’s contact form to reach out and I’ll get back to you as soon as possible.