views:

419

answers:

2

I am working with Sphinx and would like to implement string sorting. I understand that this can be accomplished using attributes and String Ordinals, however, I also want to implement Live Index Updates and string ordinals do not work with multiple indexes.

What would be the best way to approximate string sorting with multiple indexes? I am thinking along the lines of generating an integer from the first few letters of the string, for example:

select concat(ord('t'),ord('e'),ord('s'));

would allow me to add the first three characters of the string 'test' to an integer attribute (assuming that it would be added to sphinx as an integer even though it is a string in MySQL). This would give me approximate sorting, which is probably good enough.

A: 

Hi,

Be aware that for instance ord('&') is 38 and ord('a') is 97 so if your words are [a-z][A-Z] that's ok but if you have something like h&b it will be before hub for instance

Manfred

Manfred Dardenne
+1  A: 

I ended up creating a MySQL function that converts the string to an ordinal:

CREATE DEFINER=`root`@`localhost` 
    FUNCTION `stringToOrd`(str varchar(100)) RETURNS int(11)
READS SQL DATA
DETERMINISTIC
SQL SECURITY INVOKER
BEGIN

    DECLARE ordinal INT;
    SELECT ((ORD(SUBSTRING(str,1,1)) * 16777216) 
        + (ORD(SUBSTRING(str,2,1)) * 65536)
        + (ORD(SUBSTRING(str,3,1)) * 256) + (ORD(SUBSTRING(str,4,1)))) 
        into ordinal;
    return ordinal;
END 

The function only uses the first four characters of the string, so the sorting will be approximate. This function is called during the sphinx indexing query (in the sphinx config file). The attribute is then used for sorting during the sphinx search call.

This has worked successfully in a production environment for over 6 months now.

jonstjohn