Commit bec3007c authored by Laurent Sittler's avatar Laurent Sittler ©
Browse files

Add Search Match SQL file

parent 08096df4
DROP FUNCTION IF EXISTS `search_match`;
DELIMITER $$
CREATE FUNCTION `search_match`(s1 TEXT, s2 TEXT, s3 VARCHAR(1))
RETURNS INT
DETERMINISTIC
BEGIN
SET @needle = s1;
SET @haystack = s2;
SET @splitChar = s3;
SET @comma = LOCATE(@splitChar, @needle);
SET @comp = LOCATE(@splitChar, @haystack);
SET @word = TRIM(@needle);
IF LENGTH(@haystack) = 0 THEN
RETURN 0;
ELSEIF @comma = 0 THEN
IF LENGTH(@needle) = 0 THEN
RETURN 0;
ELSEIF @comp = 0 THEN
/* one word search term */
RETURN TRUNCATE(levenshtein_ratio(dm(@word), dm(@haystack)), 0);
ELSE
SET @tmp = @haystack;
SET @comp = LOCATE(@splitChar, @tmp);
/*SET @log = CONCAT('tmp3[', @tmp, '];');*/
SET @test = TRIM(SUBSTR(@tmp, 1, @comp));
SET @cs2 = 1;
SET @levenshtein = 0;
REPEAT
SET @tmp = TRIM(SUBSTR(@tmp, @comp));
SET @comp = LOCATE(@splitChar, @tmp);
/*SET @log = CONCAT(@log, @needle, '/', @test, ';');*/
SET @levenshtein = @levenshtein + levenshtein_ratio(dm(@needle), dm(@test));
IF @comp = 0 THEN
/*SET @log = CONCAT(@log, @needle, '/', @tmp, ';');*/
SET @levenshtein = @levenshtein + levenshtein_ratio(dm(@needle), dm(@tmp));
END IF;
SET @cs2 = @cs2 + 1;
SET @test = TRIM(SUBSTR(@tmp, 1, @comp));
UNTIL LENGTH(@test) = 0
END REPEAT;
/*SET @log = CONCAT(@log, 'count:',@cs2, ' levenshtein:', @levenshtein);*/
RETURN TRUNCATE(@levenshtein/@cs2,0);
END IF;
END IF;
IF @comp = 0 THEN
SET @tmp = @needle;
SET @comma = LOCATE(@splitChar, @tmp);
/*SET @log = CONCAT('tmp4[', @tmp, '];');*/
SET @test = TRIM(SUBSTR(@tmp, 1, @comma));
SET @cs1 = 1;
SET @levenshtein = 0;
REPEAT
SET @tmp = TRIM(SUBSTR(@tmp, @comma));
SET @comma = LOCATE(@splitChar, @tmp);
/*SET @log = CONCAT(@log, @haystack, '/', @test, ';');*/
SET @levenshtein = @levenshtein + levenshtein_ratio(dm(@haystack), dm(@test));
IF @comma = 0 THEN
/*SET @log = CONCAT(@log, @haystack, '/', @tmp, ';');*/
SET @levenshtein = @levenshtein + levenshtein_ratio(dm(@haystack), dm(@tmp));
END IF;
SET @cs1 = @cs1 + 1;
SET @test = TRIM(SUBSTR(@tmp, 1, @comma));
UNTIL LENGTH(@test) = 0
END REPEAT;
RETURN TRUNCATE(@levenshtein/@cs1, 0);
ELSE
SET @log = '';
SET @word = TRIM(SUBSTR(@needle, 1, @comma));
SET @tmp = @haystack;
SET @test = TRIM(SUBSTR(@tmp, 1, @comp));
SET @cs = 0;
SET @levenshtein = 0;
REPEAT
IF levenshtein(@needle, @haystack) = 0 THEN
RETURN 100;
END IF;
SET @bv = 0; /* Bigger value */
SET @tmp = @haystack;
SET @comp = LOCATE(@splitChar, @tmp);
/*SET @log = CONCAT(@log, 'tmp1[', @tmp, '];');*/
SET @test = TRIM(SUBSTR(@tmp, 1, @comp));
SET @needle = TRIM(SUBSTR(@needle, @comma));
SET @comma = LOCATE(@splitChar, @needle);
REPEAT
SET @tmp = TRIM(SUBSTR(@tmp, @comp));
SET @comp = LOCATE(@splitChar, @tmp);
/*SET @log = CONCAT(@log, @word, '/', @test, ':', levenshtein_ratio(dm(@word), dm(@test)),'\r\n');*/
SET @cv = levenshtein_ratio(dm(@word), dm(@test));
IF @cv > @bv THEN
SET @bv = @cv;
END IF;
IF @comp = 0 THEN
/*SET @log = CONCAT(@log, @word, '/', @tmp, ':', levenshtein_ratio(dm(@word), dm(@tmp)), '\r\n');*/
SET @cv = levenshtein_ratio(dm(@word), dm(@tmp));
IF @cv > @bv THEN
SET @bv = @cv;
END IF;
END IF;
SET @test = TRIM(SUBSTR(@tmp, 1, @comp));
IF LENGTH(@test) = 0 THEN
/*SET @log = CONCAT(@log, ' > Bigger ratio:', @bv, '\r\n');*/
SET @levenshtein = @levenshtein + @bv;
SET @cs = @cs + 1;
SET @bv = 0;
END IF;
UNTIL LENGTH(@test) = 0
END REPEAT;
IF @comma = 0 THEN
SET @tmp = @haystack;
SET @comp = LOCATE(@splitChar, @tmp);
/*SET @log = CONCAT(@log, 'tmp2[', @tmp, '];');*/
SET @test = TRIM(SUBSTR(@tmp, 1, @comp));
SET @bv = 0;
REPEAT
SET @tmp = TRIM(SUBSTR(@tmp, @comp));
SET @comp = LOCATE(@splitChar, @tmp);
/*SET @log = CONCAT(@log, @needle, '/', @test, ':', levenshtein_ratio(dm(@needle), dm(@test)),'\r\n');*/
SET @cv = levenshtein_ratio(dm(@needle), dm(@test));
IF @cv > @bv THEN
SET @bv = @cv;
END IF;
IF @comp = 0 THEN
/*SET @log = CONCAT(@log, @needle, '/', @tmp, '\r\n');*/
SET @cv = levenshtein_ratio(dm(@needle), dm(@tmp));
IF @cv > @bv THEN
SET @bv = @cv;
END IF;
END IF;
SET @test = TRIM(SUBSTR(@tmp, 1, @comp));
IF LENGTH(@test) = 0 THEN
/*SET @log = CONCAT(@log, ' > Bigger ratio:', @bv, '\r\n');*/
SET @levenshtein = @levenshtein + @bv;
SET @cs = @cs + 1;
SET @bv = 0;
END IF;
UNTIL LENGTH(@test) = 0
END REPEAT;
END IF;
SET @word = TRIM(SUBSTR(@needle, 1, @comma));
UNTIL LENGTH(@word) = 0
END REPEAT;
RETURN TRUNCATE(@levenshtein/@cs, 0);
/*RETURN CONCAT(@log, @levenshtein, '/', @cs, '=', TRUNCATE(@levenshtein/@cs, 0));*/
END IF;
END;
$$
DELIMITER ;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment