Given a table like this:
Product
Column/Field | Description | Type |
---|---|---|
Id | Unique identifier | |
Name | A short name | string |
Description | Long description | string |
Tags | A string of keywords separated by commas | string |
How do I perform a search in SQL Server using ASP.Net Core 3.1 MVC or above that returns results in the order of most matches?
The user will enter one or more search keywords in the UI. The fields to be searched to test if any of the keywords match are Name, Description and Tags.
Searching will be case-insensitive. The Description field is a free form text string field, so some words may have pre or post punctuation marks.
And I need the results returned in order of “best match first”, which is defined as how many of the columns/fields in a given row contain the search keywords.
For example, if there were only 3 records in the Product table like this: -
Id | Name | Description | Tags |
---|---|---|---|
1 | A phone | iPhone 4 | Mobile Phone |
2 | iPhone | iPhone 5 | iPhone mobile phone |
3 | Mobile | Cell phone | iPhone |
If a user entered “iPhone” and that occurs in all three fields, this should be top of the list. So the results should look like this:
Id | Name | Description | Tags |
---|---|---|---|
2 | iPhone | iPhone 5 | iPhone mobile phone |
1 | A phone | iPhone 4 | Mobile Phone |
3 | Mobile | Cell phone | iPhone |
That is, with the best matches at the top.
How do I achieve this with SQL Server using ASP.Net Core 3.1 (or above) MVC and C#?