Linq (or SQL): Get a search query and sort it order by best result [closed]
I want to create a search page for my website. The logic is the user enters some keywords and I should search those words then sort the result by best matches. i.e: The user enters: "MVC Microsoft WPF ASP.Net". I want to show those results which contain most matches, such as:
Best Matches:
Learning Microsoft ASP.Net MVC How to host a WPF form in Microsoft ASP.NetPartial matches:
Microsoft MVC Microsoft WPF Microsoft ASP.Net ASP.Net MVCKeyword Matches:
MVC Microsoft WPF ASP.NetTry out Lucene.NET, it sorts search results by relevance by default
Finally I found the actual solution.
That's "FullText Index".
A complete tutorial is here:
http://blog.sqlauthority.com/2008/09/05/sql-server-creating-full-text-catalog-and-index/
Depending on what / how you are searching, a simple solution is to build your website so it is fully indexed on Google and take advantage of Google Custom Search API.
Here's a basic strategy; it would require a stored proc:
- Split your keyword string on spaces, and pad with '%'s.
- For each search term, perform an insert-select to the same in-memory temp table. You'll get duplicate rows, one per record per "hit" on a search term.
- Select from this temp table each distinct result, with a count of the number of times that result appears in the temp table. This is your "relevance", by which you can sort.
精彩评论