Search Extensions - Whole Word Matching

by John Nye

07 Sep
2015

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

PM> Install-Package NinjaNye.SearchExtensions

Comments 0 * Be the first to comment!

Leave a message...

28 May
2017