How to create a search engine in MVC

Home > Search > How-to
  by

Before creating a search engine in MVC, you first need to be able to retrieve data from a database. After configuring MVC to retrieve data from a database, let's say the name of your controller is SampleController, and www.example.com/Sample/Index will display every record from column1 in the SQL database.

@model IEnumerable<www.example.com.Models.Sample>
@Html.DisplayFor(modelItem => item.column1)

 


VIEW

To avoid issues with MVC Routing, it is typically a good idea to create a separate page for the search engine. Let's assume the search engine will be at www.example.com/Sample/Search. Update /View/Sample/Search.cshtml to have the following.

  • Since the search engine is on the www.example.com/Sample/Search page, Html.BeingForm has "Search", "Sample". 
  • The first field in the Html.TextBox can contain any word. "MySearch" was used in this example.
@model IEnumerable<www.example.com.Models.Sample>

@using (Html.BeginForm("Search", "Sample", FormMethod.Get)) {
  @Html.TextBox("MySearch", null, new { @placeholder = "search . . ." })
}

@Html.DisplayFor(modelItem => item.column1)

 

This will produce the following HTML.

<form action="/Sample/Search" method="get">
<input id="MySearch" name="MySearch" placeholder="search . . ." type="text" value="">

 


CONTROLLER

Since the first field in the Html.TextBox in the view is "MyKeyword", in the SampleController, add string MySearch to the Index ActionResult. Find the results in the table where column1 contains the keyword, and return these results to /View/Sample/Search

public ActionResult Index(string MySearch) {   
    return View(db.table_name.Where(x => x.column1.Contains(MySearch)).ToList());    
}

 

It is not uncommon to search for two or more columns. For example, to search for column1 or column2.

public ActionResult Index(string MySearch) {   
    return View(db.table_name.Where(x => x.column1.Contains(MySearch) || x.column2.Contains(MySearch)).ToList());    
}

 

If SQL Server is not configured with a fulltext catalog, the search engine will only return results that match a string exactly. As an example, let's say there is a record in the database with string "Hello world how are you today."  If SQL Server is not configured with a fulltext catalog, searching with keywords "Hello today" will not return the result with "Hello world how are you today." Follow the instructions in the article on how to set up a fulltext search in SQL Server and MVC.

 

 


Search Filter

Let's say we want to add a search filter, so that we can search by only column1. 

 

VIEW

Add @Html.RadioButton to the view.

@model IEnumerable<www.example.com.Models.Sample>

@using (Html.BeginForm("Search", "Sample", FormMethod.Get)) {
  @Html.RadioButton("searchBy", "column1") <text> Column 1 </text>
  @Html.TextBox("MySearch", null, new { @placeholder = "search . . ." })
}

@Html.DisplayFor(modelItem => item.column1)

 

CONTROLLER

Add string searchBy, and also add an if / else statement. The if statement will only search column1. The else statement will search every column.

public ActionResult Index(string searchBy, string MySearch) {   
  if (searchBy == "column1")
    return View(db.table_name.Where(x => x.column1.Contains(MySearch)).ToList());    
  else
    return View(db.table_name.Where(x => x.column1.Contains(MySearch) || x.column2.Contains(MySearch)).ToList());   
}

 


Search not exact strings

Let’s say there is a table named MyTable with a column named Column1 in SQL, and one of the records in Column1 is Hello world how are you today. The following SQL statement would return the record containing Hello world how are you today: Select * from MyTable where column1 like ‘%Hello%’ and title like ‘%today%’

In the above C#, if MySearch contains “Hello world”, then the record containing Hello world how are you today will be returned to the View, because “Hello world” is an exact string of data in Column1. However, if MySearch contains “Hello today”, the record containing Hello world how are you today will not be returned to the view, because “Hello today” is not an exact string of data in Column1.

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments