While I was looking at the documentation for query parameters here, I noticed that there were two query parameters that seemingly did the exact same thing: filter
and search
.
I'm just wondering what the difference is between them and when is one used over the other.
The difference between search and filtersFilters let you create a list of records that meet a common value. Search lets you find a single record based on a particular value.
However, there is a difference between the two parameters. Using a filter parameter alone returns search results in no specific order. Using a query parameter alone returns search results in order of relevance.
Common use cases for query params include representing the current page number in a paginated collection, filter criteria, or sorting criteria. In web development, query parameters are used within a URL as described above but can also be used in API requests that retrieve data.
While they're similar, they operate a little differently.
$search
uses Keyword Query Language (KQL) and is only supported by message
and person
collections (i.e. you can't use $search
on most endpoints). By default, it searches multiple properties. Most importantly, $search
is a "contains" search, meaning it will look for your search word/phrase anywhere within a string.
For example, /messages?$search="bacon"
will search for the word "bacon" anywhere in the from
, subject
, or body
properties.
Unlike $search
, the $filter
parameter only searches the specified property and does not support "contains" search. It also works with just about every endpoint. In most places, it supports the following operators: equals (eq
), not equals (ne
), greater than (gt
), greater than or equals (ge
), less than (lt
), less than or equals (le
), and (and
), or (or
), not (not
), and (on some endpoints) starts with (startsWith
).
For example, /messages?$filter=subject eq 'bacon'
will return only messages where the subject is "bacon".
Both search and filter reduce the result set that you ultimately receive, however they operate in different ways.
This is indicated in Microsoft's documentation:
Returns results based on search criteria.
Filters results (rows).
(results that could be returned by search)For performance purposes, it's good to use both if you can, search to narrow the results (e.g. using search indexes) and then do fine grain filtering on the returned results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With