Language Search Field Configuration

Several pieces of software have a language search field which attempts to make it easy for users to find their language amidst the other 7,000+ options. The search algorithm should be implemented in the following manner to ensure proper user experience:

if length of input string is <= 3:
    return languages that have a language_code or
    country_code that STARTS WITH the entered string
else:
    return languages where the entered string is found
    WITHIN language_code, language_name, language_region_name