Search code examples
indexingconfigurationsphinx

Sphinx search engine: Different min_prefix_len value for Different fields


In sphinx I can set Minimum word prefix length to index with min_prefix_len property.

I want to set different min_prefix_len value for different fields. For example I have a field 'Name' that I want to set min_prefix_len = 5 and another field 'Class' that should be set to 3. In other words, I want to set min_prefix_len per field instead of per index. Is it possible to do that in sphinx search engine configuration?


Solution

  • min_prefix_len can be set per index only, but if you don't worry about spending additional resources you can set min_prefix_len = 3 for the whole index and then just control the behavior in your app to not allow @name to be searched in case the keyword's length is less than 5. E.g.:

    Keyword = abc:

    mysql> select * from idx_min where match('@class abc*|@name abc');
    +------+--------+--------+
    | id   | class  | name   |
    +------+--------+--------+
    |    1 | abcdef | ghijkl |
    +------+--------+--------+
    1 row in set (0.01 sec)
    

    Keyword = abcde:

    mysql> select * from idx_min where match('@class abc*|@name (abcde|abcde*)');
    +------+--------+--------+
    | id   | class  | name   |
    +------+--------+--------+
    |    1 | abcdef | ghijkl |
    +------+--------+--------+
    1 row in set (0.00 sec)
    

    Keyword = ghi:

    mysql> select * from idx_min where match('@class ghi*|@name ghi');
    Empty set (0.00 sec)
    

    Keyword = ghijk:

    mysql> select * from idx_min where match('@class ghi*|@name (ghijk|ghijk*)');
    +------+--------+--------+
    | id   | class  | name   |
    +------+--------+--------+
    |    1 | abcdef | ghijkl |
    +------+--------+--------+
    1 row in set (0.00 sec)
    

    I.e. just remember in this case that you have prefixes >= 3 chars for @name too and add logic to your app to put * when needed.