I’m working on a search engine using CakePHP 2.0 and am having difficulty finding the most efficient way to get the result I’m wanting.
Say I’m querying people and I get a set of 20 results with 5 age 20, 10 age 30 and 5 age 40. In addition, 15 of these people have brown eyes, 3 have blue and 2 have green. I want to find the most efficient way to get those specific counts. I’ll then display these results on the page so that users can see what’s in the results with those parameters. They will then be able to click on one of them to add that search parameter to the current query.
This isn’t something that I can store in a database or cache at all because each search could be different and could/will return different results.
If I’m not explaining what I’m trying to do (this may be likely) there are several websites that do this. Cars.com uses this method when searching for cars. You search a generic search and then links on the side allow you to narrow your results. These links include counts of the current result set that fall within the specific parameter.
An idea has been to get the full result set and then parse through it generating the counts and this would work, but in my specific project I’m dealing with thousands of records and it seems like this could add additional load time to the page and/or strain on the server.
Here’s a visual example:

Cars.com is likely using associated tags for each feature that is counted. With associated tags a table record has many and belongs to many feature tags.
So that they don’t have to create a tag for each car price. They create price range tags.
For every car record there are associated tag records that hold all features of that car. You can then cache a count in the tag record of how many cars have that feature.
The SQL table structure might be something like this.
For every Car record there can be multiple Feature records. These are associated to each Car via the cars_features table. When someone searches and finds Car XXXX you can then look up the Features of that car, and also display a cached count of how many cars have that feature.
EDIT:
To narrow the counts so that they are limited to only the cars that were discovered in the search. You’ll need to first get a list of all the Car IDs and then perform a COUNT using a JOIN between the cars_features table and features.
Here is some sample data.
Assuming we searched for that returned two items so that our Car IDs were (1,2). We could find a feature count using the following SQL query.
This will report that count for each Feature limited to just the Car records found.
I’ll try to write the above in CakePHP model format.