Search code examples
mysqladvanced-search

Advanced search architecture advice neeeded


I have a database full of items, which should be searchable by any or all of the following criteria:

item_id, supplier, price range, color

Now, that's easy - user's selection of checkboxes is POSTed, and I turn it into appropriate query.

QUESTION: how to make it so that search dynamically modifies itself. What I mean is - if you select a certain price range, all suppliers and colors that don't conform to it are removed from selection, and any selected items are deselected as well.

Likewise, if user checks certain color, all suppliers that don't offer it, and all price ranges that don't have it are removed from selection. And also all items a user may have previously selected, but which are not in that color.

Sub-question: is this even good idea at all, if you're against it, please let me know why?

NOTE: Architectural advice is all I need, how to approach it, and answer is HIGHLY appreciated, I'm bashing my head over this for 2 days.

Won't refuse code as well, and it may be in any language or pseudo code, I need to decode the way of thinking here.

EDIT: Here's an example - by clicking any of the search criteria on the left, you affect search results:

http://www.wanajob.com/emploi?search=informatique&fpc=&fpr=


Solution

  • I'm not an expert on facets so I defer to Brent's answer in that direction but what you are describing is fairly easy to implement in MySQL for your basic search data.

    Let's say the customer searches on "red" being the colour. You could then construct the list of suppliers and price ranges within the search results like this

    SELECT DISTINCT supplier FROM items WHERE color = 'red';
    SELECT DISTINCT price_range FROM items WHERE color = 'red';
    

    Note that these would have to be separate queries as

    SELECT DISTINCT supplier,price_range FROM items WHERE color = 'red';
    

    would return all DISTINCT combinations of the two fields.

    If you require a COUNT of each criteria then you can use GROUP BY

    SELECT supplier,COUNT(*) FROM items WHERE color = 'red' GROUP BY supplier;
    SELECT price_range,COUNT(*) FROM items WHERE color = 'red' GROUP BY price_range;