How to integrate NinjaNye.SearchExtensions Ranked search
I have recently updated my site with a few design tweaks and changes. In addition to that I thought it would be a good idea to integrate a nuget package I created called NinjaNye.SearchExtensions
. It is pretty simply to integrate, here is how.
####Installing NinjaNye.SearchExtensions nuget package
This part is really simple. Open Package Manager Console from within Visual Studio and run the following command
The SearchExtensions nuget package is also available by running the following
<p class="nuget-badge"><code>PM> Install-Package NinjaNye.SearchExtensions</code></p>If you would prefer to use the Nuget GUI then you can do so by opening your package manager and searching for NinjaNye.SearchExtensions
.
Integrating Ranked searches
One of the latest features of the search extensions is the ability to retrieve a ranked search result. Implementing this is as easy as performing a regular search, here is how:
var searchResult = this.postRepository.RetrieveAll()
.RankedSearch(searchTerms, p => p.Title, p => p.Body)
.OrderByDescending(r => r.Hits)
.ThenByDescending(r => r.Item.Views)
.ToList();
Once you have performed a ranked search, you can then order your results by the Hits
property of IRanked
or any other property of the original item. Above, I am first ordering by the most hits, then by the post with the most views.
This process is simply building up an expression tree which means, when you call ToList()
, the expression tree is converted to the specific data provider that sits behind the ORM and only matching results are returned from the data server
Try it out
This code is now live on my blog, so why not try a search using the search text box in the top right of the page, or download the package and have a go for yourself
I hope this is useful to someone, please give feedback using the comments form below.
Full source code for NinjaNye.SearchExtensions can be found here: https://github.com/ninjanye/searchextensions