Skip to content

Instantly share code, notes, and snippets.

@mpchadwick
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpchadwick/301a44554906a8d30989 to your computer and use it in GitHub Desktop.
Save mpchadwick/301a44554906a8d30989 to your computer and use it in GitHub Desktop.
Mageto Improved Autocomplete Query
#################################################################
# ORIGINAL
# Then the term matching the search is plucked to the top in PHP
# This eliminates the ability to put a "limit" on the result set
#################################################################
SELECT
DISTINCT IFNULL(synonym_for, query_text) AS `query`,
`main_table`.`num_results`,
`main_table`.*
FROM `catalogsearch_query` AS `main_table`
WHERE
(num_results > 0 AND display_in_terms = 1 AND query_text LIKE '%tissue box%')
AND (store_id = 1)
ORDER BY `popularity` DESC
#################################################################
# IMPROVED
# We push our search term to the front in the order by
# This allows us to use a LIMIT in the query
#################################################################
SELECT
DISTINCT IFNULL(synonym_for, query_text) AS `query`,
`main_table`.`num_results`,
`main_table`.*
FROM `catalogsearch_query` AS `main_table`
WHERE
(num_results > 0 AND display_in_terms = 1 AND query_text LIKE '%tissue box%')
AND (store_id = 1)
ORDER BY
CASE
WHEN `query` = 'tissue box' THEN 0
ELSE 1
END, `popularity` DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment