Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the filter and search query parameters in Microsoft Graph Mail API?

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.

like image 505
Stefan Aleksić Avatar asked Jun 20 '18 01:06

Stefan Aleksić


People also ask

What is the difference between search and filter?

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.

Which of the following is a key difference between a query parameter and a query filter?

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.

Which of the following are the application of query parameters?

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.


2 Answers

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".

like image 180
Marc LaFleur Avatar answered Oct 11 '22 00:10

Marc LaFleur


Both search and filter reduce the result set that you ultimately receive, however they operate in different ways.

  • Search operates on the query against the entire graph and reduces the amount of information a search query returns. This is often optimized for queries that search is good at, e.g. performing searches for items that can be indexed.
  • Filter operates on the much smaller result set returned by the search to provide more fine grain filtering. Separating this out allows filtering to perform tasks that would not be performant against the full collection.

This is indicated in Microsoft's documentation:

  • Search: Returns results based on search criteria.
  • Filter: 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.

like image 44
Grokify Avatar answered Oct 11 '22 00:10

Grokify