Search Extensions - Whole Word Matching

less than a minute read

Search text for whole word matches

I recently received a request from a user to looking to be able to search for terms whilst not returning partial matches. For instance, given I am searching for the word "tension", I would not expect "extension" to be returned

In version 1.6 it is now possible to restrict your search to only return records where whole words are matched.

var result = data.Search(x => Name)
                 .Matching(SearchType.WholeWords)
                 .Containing("tension");

We can also mix the search types. The following example matches "tension" against whole words only but then reverts a search for "search" to match against any occurrence,

var result = data.Search(x => Name)
                 .Matching(SearchType.WholeWords)
                 .Containing("tension");
                 .Matching(SearchType.AnyOccurrence)
                 .Containing("search");

The above code will return "tension research" but would not return "extension research"

This feature is also available to .StartsWith() and .EndsWith() methods.


Thanks to Rob for getting in touch via a previous post.
This feature has come about purely because he got in touch.

If you have a new feature request, please get in touch by adding a comment below, contact me on twitter (@ninjanye) or you can raise an issue on the github project page

<p class="nuget-badge"><code>PM> Install-Package NinjaNye.SearchExtensions</code></p>