简体   繁体   中英

Converting custom search filters to dynamic SQL

I'm writing a custom search page that will allow people search for artwork, but I want to allow users to enter custom commands/tags into the search box to let them have direct control on filtering their results without having to use anything but the search box. Let me give an example...

I want the user to be able to type something like the following into a textbox:

Textbox Input: color=red|blue|green value>=5.00+<=10.00

Which would convert to the following SQL that I could peg onto the end of my SQL query:

AND (Artwork.Color = 'red' OR Artwork.Color = 'blue' OR Artwork.Color = 'green')
AND (Artwork.Price >= 50.00 AND Artwork.Price <= 100.00)

What do you think about...

1) Allowing people to type these custom commands into a textbox and converting these commands to SQL, is this normal practise? If not, how else would I allow advanced search options to be passed in, whilst keeping the page simple?

2) The best way to convert these custom commands into SQL, how do I convert an unknown number of filters split by potentially multiple operators (+,|)?

This is a web project built using ASP.NET (C#) and MSSQL. Thanks in advance: :)

Personally I've always gone down the route of creating a UI for all the search options wiht controls for valid input (checkbox lists,date pickers etc). Although this sounds a pain in the first place I think it works out the same once you've come up wiht the SQL ish systax, converted it and made it safe. Its also much more user friendly to non-geeks.

You can always hide the options in a hidden panel labelled "Search Options", then it can pop out when needed, and free up the screen when not.

  1. Yes it is quite a common practice . But of course not all websites allow this.
  2. Come up with your own standardized way of defining search criteria. See the links in number 1.

Take a look at ASP .NET Query Extender

To answer your first question, I don't think this is normal practice. It'll be difficult for normal users to remember this very specific search syntax. You may want to look at Google's advanced search page to see how they did something like this. That's the approach I have seen and used. If you are going to implement something like this, I think it'll be wise to still have a simplify free form text search. That way new users don't have to learn your syntax before being able to use your site.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM