Find the number of where statements that matched in postgresQL Right now I have a query that looks something like this 

SELECT id 
FROM products 
WHERE products.details-&gt;'make' = ? 
    OR products.details-&gt;'model' = ? 
    OR products.details-&gt;'year' = ?


I'd like to sort the results by how many of the where statements matched, so for example if the three terms that were used in the query were ford, f150, 1985, and I got two results. One that had all three terms in it (a ford f150 from 1985), and a second result that had only two matches (a ford f150 from 1991).

Is there a way for me to do this with postgreSQL 9.3, or will I need to use my application layer to figure out how many matches there were?
One way:

SELECT id 
FROM   products 
WHERE  details-&gt;'make' = ? 
    OR details-&gt;'model' = ? 
    OR details-&gt;'year' = ?
ORDER  BY CASE WHEN details-&gt;'make' = ?  THEN 0 ELSE 1 END
        + CASE WHEN details-&gt;'model' = ? THEN 0 ELSE 1 END
        + CASE WHEN details-&gt;'year' = ?  THEN 0 ELSE 1 END;


It's a bit simpler to invert the logic:
NULL and FALSE results increase the value (and decrease the ranking).