tags:

views:

103

answers:

3

I have a question about a database schema for a certain website where people can sell and buy stuff.

When someone adds an ad to the database, he will choose the category and fills out the filters that belong to the selected category, so anyone can easily find his ad by browsing to a category and choosing filters like:

[bluetooth] yes

[camera] 3 - 5 megapixels.

Et cetera

What is the best way to store these filters in the database? This is what I have so far:

Schema

This is just an idea, but it will never send the ads back with value for filter "megapixels" between 3 and 5, since filter_value is of type VARCHAR.

A: 

I'd do with tables for:

  • ads
  • filter names (category is just one of filters)
  • filter values
  • association between ad and filter values

Store it any way you like but for efficient querying based on these filters set up Solr and use faceted searching.

Kamil Szot
This isn't really helpful. It just begs more questions.
Ben S
I guess you are right. This is not answer. That's just a good advice for later. I am going to edit this to include actual answer.
Kamil Szot
A: 

The design works pretty fine according to me, I am curious as to why you have included an additional id field in the join table, e.g between ads and categories. I think you could do without them. The design on the whole is pretty good because it lets you make changes to the base tables e.g ads later on.

Another thing What is filter_value? and why cant it go in the filters table?

Shiv
The single column primary keys make programming the front end much easier.The filter_value is the value specified by the person placing the ad that describes the individual item being advertised. The camera I place on the site might have "Yes" for "Bluetooth", but yours might have "No".
Larry Lustig
A: 

Have you considered an additional table, filter_value_list with columns id, filter_id, and value? This table would contain a list of VARCHAR answers that would be supplied to the user in the form of a drop down for each filter linked to the category of the ad that is being placed.

For the "Bluetooth" filter, the possible values would be "Yes" and "No". For the "Camera" filter, the possible values might be "Less than 1 megapixel", "1 or 2 megapixels", "3 to 5 megapixels", "5 to 10 megapixels", "More than 10 megapixels" (we're planning for future technology here).

The advantages:

  1. User selects, rather than types, their answer.

  2. Each filter is guaranteed to be understood by your system.

  3. Consistency across all advertisements.

  4. The possible value list can also be used in an "add/remove filters" sidebar like you see on many sites these days.

Larry Lustig
Hey Larry, thanks for your comment!So, what you said was:There will be no specific types of filter values like INT and VARCHAR, but VARCHAR only. And you're right, is has these four advantages. However, people are forced to add the exact value for number of megapixels (e.g. "3.2MP") in the description, since they only can choose for "3 to 5 megapixels". It's not as easy as it was to add a slider on the ads page where you can select the products with megapixels from 2 - 8, or 6 - 8. Of course, it's not only about megapixels ;)So, thanks for your idea, but maybe there's even a better one :]
Harmen
Yes, it's true that the user can't add in random values. However, you can create values for all the common resolutions instead of using ranges if you prefer. The important thing is whether it's more important to you that the answers always fit a defined category or not.
Larry Lustig