views:

327

answers:

6

I am working an a search page that allows users to search for houses for sale. Typical search criteria include price/zip code/# bedrooms/etc.

I would like to allow the user to save this criteria in a database and email new homes daily.

I could either:

1) Serialize a "SavedSearch" object into a string and save that to the database, then deserialize as needed.

2) Have a list of columns in a tblSavedSearch corresponding to the search criteria - price/zip/# bedrooms/etc.

I am concerned that if I choose option 1, my saved search criteria will change and leave the searialized objects in the database invalid, but option 2 doesn't feel like an optimal solution either.

How have others solved this problem?

A: 

If I can suggest, have a html page with search results (if it contains a moderate number of records). And, store the path to it alongwith search criteria in the DB

This will avoid querying the DB.

Edit: I am assuming records won't change as frequently. If it does, it is better to store search criteria in database and query it when asked by the user.

shahkalpesh
If they're emailing a list of new homes for sale every day, that suggests that the data is changing fairly frequently.
DOK
Not necessarily. It depends on the user criteria.If its ebay kind of site, I agree that data will change very frequently.The choice can be based on what is the frequency of data change?That said, running a query every time won't be too much as well.
shahkalpesh
+1  A: 

table Users

table Criteria (= the list of provided search criteria)

table SavedSearch (detail of Users)

table SavedSearchCriteria, detail of SavedSearch, referencing Criteria, column SearchValue holds the value entered by the user for each of the criteria entered

devio
A: 

I'd go with #1. If you're really worried about the criteria changing, you can store it with a "search version" attribute and massage the serialized representation when necessary.

#2 won't scale to any kind of usefulness. For instance, if you want to do any kind of boolean grouping of search criteria, your DB schema is going to light itself on fire and run screaming into the woods.

I generally solve search problems by yanking the indexing/search out of the database. That may be overkill for what you're talking about, but RDBMS's aren't that great for searching.

MrKurt
+2  A: 

I assume you will need to re-run the search daily in order to find new additions to the results. Maybe it is possible to make sure that you search form specifies a get method so that the search criteria is appended to the url as a query string then save the entire querystring in the database.

So if you have a search program called search.action you will request the search like this:

search.action?price=20000&rooms=3

You can save the price=20000&rooms=3 part into the database. To retrieve this search simply append the query string onto the url and request the seach page again.

The only caveat is as the search action changes you have to make intelligent defaults to avoid breaking old searches. For example, suppose you start searching by color, none of the old searches will have a color criteria so your search action will have to cater for this and make sure that something reasonable like ALL colors is used instead.

Vincent Ramdhanie
I ended up going with this approach. It's not as sexy as serializing the query, but since I run the saved search each night, it was only a few more lines of code for me and as a lazy programmer, it was "best" for me.Thank you Vincent!
Julius Seizure
I'd just like add that even though it might *seem* sexy to serialise the query and store it, this can lead to serious headaches if the structure of the query object is a) badly designed, or b) when the object gets more complex e.g. Updating serialized objects in the db because they don't support reasonable defaults (that you'll have to code around) can get very messy.And note how other sites/services (Google, Yahoo!, Globrix (good example in their saved searches), etc!) use a more RESTful Architecture, bringing the raw benefits of URLs and HTTP.
a darren
So the cron script would make an HTTP request for each saved search? Isn't a full HTTP request a lot of overhead just to get the core data? Also, doesn't this assume that the output (HTML,I'm guessing) rendered for a web request is suitable for use in email? I guess you could pass a flag that changes the output, a kind of context-change. But that still doesn't address the HTTP overhead. Whaddya think?
David Weinraub
A: 

You could save the dynamic SQL itself in the database as a text string. Then you won't have to do all the machinations of recreating the WHERE and IN and other SQL from the saved key-value pairs.

You can use the saved SQL and run it when you generate the email.

If your database design is not stable enough to be saving query information, you may need to defer the search design until your database design is more mature.

DOK
A: 

I think both approaches make sense, and all depends on requirements (current and prospective).

For example, if the number of searches is high, and if you need to analyze them (e.g. answer questions like "How often do the people search for each number of bedrooms?"), storing searches and their criteria in relational form (your option #2) would be more than appropriate.

But if you don't have any imminent requirements that dictate use of option #2, there is nothing wrong with serializing, IMHO. Just make sure you do NOT break compatibility with existing data as the structure of your search criteria changes over time. (BTW, backward compatibility issues are not specific to option #1 - they may occur even while using option #2.) The key point is: you can always switch from option #1 to option #2, and you can procrastinate with such switching for as long as deems practical.

Yarik