Gallery3:Modules:short search fix - Gallery Codex
Personal tools

Gallery3:Modules:short search fix

From Gallery Codex

Short Search Fix Module

Short, 2-3 character searches typically don't work with standard MySQL implementations, especially with shared hosting. This module fixes that.


The core of the search function uses MySQL do perform the search query. However, MySQL system variables limit the minimum word length (ft_min_word_len) and exclude some words (ft_stopword_file). These parameters are often read-only for shared hosting accounts (e.g. GoDaddy). This seriously sucks, especially if you have a bunch of 2-3 letter tags.

So, this module pads all search terms and search queries with a prefix to make them longer than 2-3 letters. To the user, the search queries and results are the same, but to MySQL, they've all gotten longer. In this way, we circumvent the limitations.


  • Changes search record and search query functions to append a prefix to each word
  • Provides an admin setting screen to allow the user to change the prefix ("1Z" by default) and to force a rebuild of the search records
  • Correctly strips out extra whitespaces to avoid extraneous search queries

Tested and functioning well on Gallery v3.0.2.


  • Download and extract short_search_fix, then put the short_search_fix folder into your Gallery modules folder.
  • Log into your Gallery as an administrator and activate the module in the Admin -> Modules menu. Installation will automatically flag all search records for rebuild.
  • Run the Update Search Records function in the Admin -> Maintenance menu. This might take awhile, especially for large galleries.

Note that this module alters the search_records table in your SQL database. Because of that, during deactivation or uninstallation, the module will automatically flag all search records for rebuild. Afterward, you'll need to run the Update Search Records function once again before the standard search function will work properly.

Also note that this module must be ordered after anything that generates search records (e.g. comments and tag modules). This is likely to occur by default, but if not, get the moduleorder module [1].

Admin settings screen

For most users, the default settings should work fine. However, you can change the prefix as you desire. Here are some pointers:

  • Prefix length: the length should be the MySQL minimum length (ft_min_word_len) minus your desired minimum length. Since the typical limit is four characters, a two-character prefix would allow two-character searches. Likewise, a three-character prefix would allow one-character searches.
  • Prefix characters: the characters chosen for the prefix should be completely nonsensical. In this way, you can circumvent any possibility that the result would be on the excluded words list (ft_stopword_file). For example, if you choose "a" as a prefix, then searching for "nd" will result in a MySQL query of "and" which is likely to be on an excluded list.

The default of "1Z" was chosen since typically, it allows two-character searches and is unlikely to match any excluded words.

Note that anytime you change the prefix, you must rebuild the search_records database. Check the appropriate box on the admin settings screen and hit save. Then, run the Update Search Records function in the Admin -> Maintenance menu.

The screen also allows you to flag all search records as up-to-date, which could be useful as an "undo" of the previous step. However, beware: if before you marked them all for rebuild there in fact were a few that were out of date, that information will be lost! When in doubt, don't use this feature.


  • 2012/10/08, version 2

Made compatible will all boolean SQL special characters (ref and

  • 2012/04/03, version 1

Initial release