Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement a search box in an ASP.NET MVC application?

I need to implement a "Search" box on a C# MVC application that I'm writting.

I've never had to implement a "Search" box before and I've been looking for some best practices and I'm not quite finding what I'm looking for.

I really like how the search works on stackoverflow.

If I type in a few random words, it navigates to the url http://stackoverflow/search?q=few+random+words.

If I type in title:random, it navigates to the url https://stackoverflow.com/search?q=title%3Arandom

What is happening both on the client (when I hit the enter key) and on the server to make the search happen?

I've purposely left out any thoughts I've already had on what is happening because I don't want to bias the answers (or show my ignorance).

EDIT: I'm adding some specifics to this question.

  1. Where and how are the search terms transformed into the querystring parameters? ie few random words transformed into few+random+words,title:random transformed into title%3Arandom

  2. Where and how is few+random+words tranformed into variables used in a query?

  3. Is the query just one big Where clause that keeps appending "and" for each item that lands between the + signs?

I guess you could parse through the strings and do some replaces to achieve 1 and 2 but it sure looks like there is something already available that would automatically convert (and revert) the search strings. I'm trying to be prepared for my user's typing ANYTHING in the search box.

like image 605
SLoret Avatar asked Jan 19 '23 23:01

SLoret


2 Answers

These posts will help you in understanding how it works

https://blog.stackoverflow.com/2008/10/stack-overflow-search-now-51-less-crappy/

https://blog.stackoverflow.com/2009/07/stack-overflow-search-now-61-less-crappy/

https://blog.stackoverflow.com/2011/01/stack-overflow-search-now-81-less-crappy/

like image 184
Haris Hasan Avatar answered Jan 21 '23 12:01

Haris Hasan


Those URL's use what is called a query string. It is a "GET" request that allows the client script (javascript) as well as the back end code retrieve the users "query". In a URL whenever you see a '?' it is the beginning of the query string. This allows somebody to be like:

http://google.com?q=Stuff%20to%20Search%20here Multiple parameters can be added via &anothercommand=somethingelse

Thus allowing a program or script to invoke a search on google without having to type anything into the box.

You can get access to the query string using the C# "Request.QueryString["parameter"]" where in this case the parameter for those stack overflow URL's would be "q".

After that, you query your database and return the results. Since I'm not sure how good at coding you are, I am not sure if you're trying to ask for the Web site, or the C# SQL side. If I am wrong, apologies.

On the Client: The way I imagine it is happening on the client is that the script on the textbox when the form is submitted redirects to the url you mentioned and adds in those query parameters into the url string. Don't forget to url encode. This is built into javascript. i.e. space ' ' becomes '%20'

When the form submits, the server code does a check to see if there are any query string parameters of the form "q". If there are, and it is not null, it would query the database, returning that in one of a few ways, most likely through a server control.

1) That is what URL encoding is. It is a list of characters that are not supported in a URL. Thus they need to be changed. There is a standard set such as %20 for space. In javascript, you would redirect to the results page with the query string you want. prior to redirecting, use the information here to encode it. i.e. change ' ' into + or %20 (it really should be %20, I find + is usually the internet explorer way. )

2) Query string works like a hashtable of key pair values. Using the Request.QueryString, you can select the key "q" and receive the string "few random words". That would then get substituted into your SQL query. This is done on the C# side as a very first check to see if the parameter q exists.

3) you can do your query many different ways. However, searching for "and" etc will give you many different results. What you can do is parse out a list of common words, and then rank results based on the number of results of each word. i.e. in the most simplistic of searches which would be ill advised for LARGE databases "..... Where like '%word% or '%word2%' etc. To get each word, do a string.split.

like image 35
DFTR Avatar answered Jan 21 '23 13:01

DFTR